How to not let the DocView framework do not create a new document. . .

xiaoxiao2021-03-06  27

When you create a document window, it will always create a new document default, so how do you make it don't create a document? On this issue, I created a mechanism when I started the document view window application, a slight shallow digging, and made a detailed analysis, I hope to help beginners.

In the initInstance () function of the app file, there are following lines: ccommandlineinfo cmdinfo; ParseCommandline (CMDInfo);

IF (! ProcessShellcommand (cmdinfo) Return False;

These lines of code are critical code for the new document when the program is started.

1: Let's take a look at what makes the CCommandLineInfo class: (some source code)

// in Afxwin.h

Class CCommandLineInfo: Public COBJECT

{

PUBLIC:

// sets default values

CCommandLineInfo ();

Bool M_BShowSplash;

BOOL M_BRUNEMBEDDED;

Bool m_brunautomated;

ENUM {FileNew, FileOpen, FilePrint, FilePrintto, FileDDE, AppRegister,

Appunregister, filenothing = -1} m_nshellcommand;

// Not Valid for FileNew

CString M_StrFileName;

.

~ Ccommandlineinfo ();

.

}

Here you must focus on Enum {filenew,...., Filenothing = -1} m_nshellcommand;

The m_nshellcommand defined here is the command type executed by the shell. If m_nshellcommand is set to filenew, then the program will create a new document. If you want to create a new document at the beginning of the document, you must set m_nshellcommand to Fillenothing.

Let's take a look at the constructor of CCommandLineInfo.

// in appcore.cpp

CCommandLineInfo :: ccommandlineinfo ()

{

M_BSHOWSPLASH = TRUE;

m_brunembedded = false;

m_brunautomated = false;

m_nshellcommand = filenew;

}

It is understood that in the constructor, the default will be set to FileNew in the constructor.

2: Let's take a look at ParseCommandline (CMDInfo); function.

Void CWINApp :: ParseCommandline (CCommandlineInfo & rcmdinfo)

{

For (int i = 1; i <__ARGC; i )

{

LPCTSTR PSZPARAM = __targv [i];

Bool bflag = false;

BOOL Blast = ((i 1) == __ARGC);

IF (pszparam [0] == '-' || pszparam [0] == '/')

{

// Remove Flag Specifier

Bflag = true;

pszparam;

}

Rcmdinfo.Parseparam (pszparam, bflag, blast);

}

}

It can be seen that ParseCommandline is mainly analyzed by the input command line parameters, and calls Parseparam for processing. Continue to analyze the Parseparam function, see the following source code: void ccommandlineinfo :: Parseparam (const tchar * pszparam, bool bflag, bool blast)

{

IF (bflag)

{

Uses_Conversion;

Parseparamflag (T2CA (Pszparam);

}

Else

ParseparamNotflag (pszparam);

Parselast (Blast);

}

Other functions don't look, we focus on analyzing the ParseParamFlag () and Parselast () functions.

Void CCommandlineInfo :: PaSEParamflag (const char * pszparam)

{

// Ole Command Switches Are Case Innsitive, While

// shell Command Switches Are Case Sensitive

IF (LSTRCMPA (PSZPARAM, "PT") == 0)

m_nshellcommand = fileprintto;

Else IF (Pszparam, "P") == 0)

m_nshellcommand = fileprint;

Else IF (LSZPARMPIA ("unregister") == 0 ||

LSTRCMPIA (Pszparam, "Unregserver") == 0)

m_nshellcommand = appunregister;

ELSE IF (Pszparam, "DDE") == 0)

{

AfxoleSetUserCtrl (false);

m_nshellcommand = filedde;

}

ELSE IF (Pszparam, "Embedding") == 0)

{

AfxoleSetUserCtrl (false);

m_brunembedded = true;

m_bshowsplash = false;

}

Else IF (LSTRCMPIA (Pszparam, "Automation") == 0)

{

AfxoleSetUserCtrl (false);

m_brunautomated = true;

m_bshowsplash = false;

}

}

Parseparamflag judges the string passing, determines its parameter type, and doing different processing according to the parameter type.

Void CCommandlineInfo :: Parselast (Bool Blast)

{

IF (Blast)

{

IF (m_nshellcommand == filenew&&! m_strfilename.isempty ())

m_nshellcommand = fileopen;

m_bshowsplash =! m_brunembedded &&! m_brunautomated;

}

}

ParselaSt will determine whether it is FileNew to open a new document. If it is open a new document, and if the open document name is not empty, it is assumed that the user wants to open this document and set the command to FileOpen.

Finally, we can summarize the role of ParseCommandline. The role of PARSecommandline is mainly analyzing command line parameters. If there is no command line parameter, ParseCommandLine () assumes that the user wants to create a document, so set a filenew command, if there is one in the command line parameter File name, parseCommandline () assumes that the user wants to open the file, so set a fileopen command. 3: Finally, let's focus on the protagonist of the casing command parsing: processshellcommand (); (partial source code)

Bool Cwinapp :: ProcessShellcommand (CCommandlineInfo & rcmdinfo)

{

Bool Bresult = true;

Switch (rcmdinfo.m_nshellcommand)

{

Case CCommandlineInfo :: filenew:

IF (! AFXGetApp () -> oncmdmsg (id_file_new, 0, null, null)

ONFileNew ();

IF (m_pmainwnd == null)

BRESULT = FALSE;

Break;

Case CCommandlineinfo :: FileOpen:..

Case ccommandlineinfo :: fileprintto:.

Case CCommandlineinfo :: FilePrint:.

Case CCommandLineInfo :: Filedde:.

Case CCommandLineInfo :: appregister:.

Case CCommandLineInfo :: Appunregister:.

.

}

}

The code sees here, everything understands. ProcessShellcommand Analyze M_NShellCommand and different processing according to different types of m_nshellcommand.

Take another two lines of code:

CCommandLineInfo cmdinfo;

Parsecommandline (CMDInfo);

IF (! ProcessShellcommand (cmdinfo) Return False;

1: When CCommandLineInfo cmdinfo is defined, first call the constructor, the M_NSHELLCOMMAND is set to FileNew 2: then execute ParseCommandline (CMDInfo); Analyze the command.

3: Finally, execute ProcessShellcommand (cmdinfo), and processshellcommand () determines that m_nshellcommand is filenew, so calling OnFileNew () creates a new document.

This is also the way to create a new document.

Finally, we look at the problem that I don't want to create a new document when the application starts:

Directly use the following code to replace the original one in the initInstance () function:

CCOMMANDLINFO CMDINFO; cmdinfo.m_nshellcommand = ccommandlineinfo :: Filenothing; ParseCommandLine (cmdinfo);

IF (! ProcessShellcommand (cmdinfo) Return False;

转载请注明原文地址:https://www.9cbs.com/read-69542.html

New Post(0)