El 20/04/2010 10:29, Mikel Luri escribi?:
El 20/04/2010 9:56, Mikel Luri escribi?:
El 20/04/2010 7:20, Jd escribi?:
I have an SDI application with multiple views. By default, it creates
a new document when the application starts. I want to modify this
behavior so that a new document is created only when user explicitly
clicks on "New". Or at least mimic this behavior.
I am using Visual Studio 2008 with MFC feature pack.
I googled and found some solution to this problem in an old MS Journal
But unfortunately it doesn't seem to work now, because framework is
sending the command ID_FILE_NEW instead of calling the virtual
Any workarounds or solutions?
In short, I need to differentiate between framework call to
OnFileNew() and User Click on New.
Is this a correct approach/design/choice in first place?
Does that even make sense? I think you cannot have an SDI without a
But you can have a MDI with a 1 document limit though, and you can
prevent a new document from opening at startup with MDI (just as
explained in the link you provide)
Sorry, I've taken a better look at the link and I see it refers to SDI.
Well, maybe they've changed something and that's why it doesn't work.
It's me again.
I've been reading carefully the article you linked to, while doing what
it says in a little test app, and it seems like the command routing
doesn't work as stated in the article, at least while initializing the app.
Specifically, we can see:
BOOL CWinApp::ProcessShellCommand(CCommandLineInfo& rCmdInfo)
BOOL bResult = TRUE;
if (!AfxGetApp()->OnCmdMsg(ID_FILE_NEW, 0, NULL, NULL))
if (m_pMainWnd == NULL)
bResult = FALSE;
So it seems that ProcessShellCommand sends directly a ID_FILE_NEW
command to the App, thus invalidating Paul's workaround.
A possible solution would be to make that particular call to OnCmdMsg
To do so, you can declare a member variable in your app class, say
m_bInit, which would be FALSE until the end of InitInstance, where you
set it to TRUE, and override OnCmdMsg. If m_bInit is FALSE, (and the
command ID is ID_FILE_NEW) you return FALSE, otherwise, you call your
base class' OnCmdMsg.
I've tried it and in seems to work. OnMyNewFile does not get called
until the user asks for a new file.
I'm not sure it's the best solution, though. If I had to do it, I would
probably take the MDI route, setting a 1 document limit and doing the
"cmdInfo.m_nShellCommand = FileNothing;" thing.