CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 10 of 10
  1. #1
    Join Date
    Aug 2010
    Posts
    47

    [RESOLVED] Where is this window coming from?

    Hi there, I'm new to C++ and am using Visual Studio 2010 Professional. I've been using dialog based apps until now and have been doing ok, but now I need to make an app that handles multiple items at once. So the multiple documents application type seemed like a natural choice.

    I am, however, having trouble figuring something out. After creating my first skeleton app it starts with a blank document open. However, I haven't figured out how it's opening that document, and I don't want the app to start with an open document.

    My skeleton project was created by making the following selections (only specifying changes from the default):
    MFC app (let's name it skeleton)
    then:
    Multiple Document, uncheck tabs
    MFC Standard project style
    then: finish

    Now, if I go into the skeletonView.cpp file that was created for me, and set a breakpoint in it's constructor, I can see that it's being called from:
    if (!ProcessShellCommand(cmdInfo))
    in skeletonApp::InitInstance(). But that's about all I can figure out so far. I can see where this skeletonView is being added to a template, and where the template is being added to the main window, but from my understanding this just lets the main window create instances of the view.

    I'm sure I'm missing something simple. Anyone care to enlighten me?

  2. #2
    Join Date
    Aug 2010
    Posts
    47

    Re: Where is this window coming from?

    Oh, just as an addendum, commenting out the ProcessShellCommand line does make the window go away with seemingly no ill effects, but I don't understand why there would be a directive in there to open a new window in the first place.

    I don't really like doing things that I don't understand, so basically I'd like to find out if commenting it out is the right thing to do or if I need to do something else. (And why?)

    Thanks!

  3. #3
    Join Date
    Feb 2003
    Location
    Iasi - Romania
    Posts
    8,234

    Re: Where is this window coming from?

    After reaching the breakpoint, take a look and click in Call Stack window (press Alt+7 to show it).
    Ovidiu
    "When in Rome, do as Romans do."
    My latest articles: https://codexpertro.wordpress.com/

  4. #4
    Join Date
    Aug 2010
    Posts
    47

    Re: Where is this window coming from?

    Well, that's how I figured out that it was being called from skeletonApp::InitInstance().
    I can see that the code is originally starting in wWinMainCRTStartup and so forth, but I didn't think that it was useful information. The ProcessShellCommand line in InitInstance is the first of my app's code that appears in the stack trace.

    Would it help if I posted a stack trace?

    What it looks like to me is that the cmdInfo contains a new window message (probably intended to start up the main window), and the default code generated by the wizard is passing that along to generate a child window. But I'm not sure of this.

  5. #5
    Join Date
    Feb 2003
    Location
    Iasi - Romania
    Posts
    8,234

    Re: Where is this window coming from?

    Probably you want to know where the view window is created.
    Well, the view class constructor is not the right place to find it.
    That window is created a little bit later by calling Create function.
    So, a better place for the breakpoint is, for example, the overridden function PreCreateWindow, which is called by the MFC framework just before the window is created.
    Then step until reach a call to CWnd::Create.
    Ovidiu
    "When in Rome, do as Romans do."
    My latest articles: https://codexpertro.wordpress.com/

  6. #6
    Join Date
    Aug 2010
    Posts
    47

    Re: Where is this window coming from?

    I put a breakpoint in PreCreateWindow as you suggested, and it looks like Create calls CreateEx which calls PreCreateWindow. Tracing it further back (skipping many steps) we get right back to the ProcessShellCommand command in InitInstance.

    So the same ProcessShellCommand command ends up doing the PreCreateWindow as well as the constructor, which still brings me back to my original problem: Is this ProcessShellCommand just there to spawn a default window or does it have other purposes? Why does the CCommandLineInfo (cmdInfo) contain a new window message?

    I guess what it comes down to is it looks to me like they got a new window message that was intended to spawn the main window, and then they reused it to spawn a child window. But without explicitly telling us they're doing that, that seems... bad. The comments for that line just say "Dispatch commands specified on the command line". But I didn't specify anything about a new document in any command lines that I know of (is there a "commandline input" settting somewhere in VC10?).

    So I see one of two things:
    1) The comment is correct, and I need to find somewhere else to disable the initial new document (say, if there is a commandline setting I need to fiddle with, or overload some function somewhere)
    2) The comment is garbage, and they're just throwing around a message they're getting from somewhere else as a convenient way of causing a new window.

    So I'm inclined to believe option 1), but that puts me back at not understanding where the window is coming from.

  7. #7
    Join Date
    Feb 2003
    Location
    Iasi - Romania
    Posts
    8,234

    Re: Where is this window coming from?

    For a MFC-beginner, the main priority is to learn how to use the framework as it is and extend it by using good wizards.
    Further, can modify some default framework behaviors by overriding MFC classes virtual functions.

    Let the MFC "internals" on the last place.
    Ovidiu
    "When in Rome, do as Romans do."
    My latest articles: https://codexpertro.wordpress.com/

  8. #8
    Join Date
    Aug 2010
    Posts
    47

    Re: Where is this window coming from?

    Uhm, ok. Then let's rephrase my original question and just focus on something more concrete.

    Given a project started with the following steps in the wizard:
    MFC app (let's name it skeleton)
    then:
    Multiple Document, uncheck tabs
    MFC Standard project style
    then: finish

    The app starts with a document open that I do not want. What is the best method of getting rid of this document?

  9. #9
    Join Date
    Feb 2003
    Location
    Iasi - Romania
    Posts
    8,234

    Re: Where is this window coming from?

    Quote Originally Posted by Ankheg View Post
    The app starts with a document open that I do not want. What is the best method of getting rid of this document?
    A little bit clearer now.
    Not the best but the easiest is to remove ProcessShellCommand call.
    Another one is to handle ID_FILE_OPEN command like for example
    Code:
    class CSkeletonApp : public CWinApp
    {
       BOOL m_bOnStart;
       afx_msg void OnFileNew();
    Code:
       // ON_COMMAND(ID_FILE_NEW, &CWinApp::OnFileNew) //   <-- remove this
       ON_COMMAND(ID_FILE_NEW, &CSkeletonApp::OnFileNew) // <-- add this
    END_MESSAGE_MAP()
    
    CSkeletonApp::CSkeletonApp() : m_bOnStart(TRUE)
    {
    }
    
    void CSkeletonApp::OnFileNew()
    {
       if(!m_bOnStart)
       {
          CWinApp::OnFileNew();
       }
       m_bOnStart = FALSE;
    }
    May be other ones...
    Your choice
    Ovidiu
    "When in Rome, do as Romans do."
    My latest articles: https://codexpertro.wordpress.com/

  10. #10
    Join Date
    Feb 2003
    Location
    Iasi - Romania
    Posts
    8,234

    Re: Where is this window coming from?

    Yet another one: return FALSE from CSkeletonDoc::OnNewDocument.
    Ovidiu
    "When in Rome, do as Romans do."
    My latest articles: https://codexpertro.wordpress.com/

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured