[RESOLVED] Can a Doc/view be hidden at startup?
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 6 of 6

Thread: [RESOLVED] Can a Doc/view be hidden at startup?

  1. #1
    Join Date
    Feb 2005
    Posts
    2,160

    [RESOLVED] Can a Doc/view be hidden at startup?

    I'm putting the finishing touches on a utility for our package. This utility is a MFC9 SDI app with a CFormView and all is well except that now the boss wants it to run minimized to the "tray" at startup. I added she ShellNotify stuff (I've done this dozens of times but never on a Doc/view app) and that all works well. The only problem is when the app is first run, it flashes a ever-so-brief window on the screen before settling down into its "hidden" state. Several searches have turned up little, but a few (one from CodeGuru itself here:http://www.codeguru.com/cpp/controls...cle.php/c5309/ pointed to the CSingleDocTemplate class as the likely culprit and that it should be overridden with a custom class that implements:

    Code:
    virtual CDocument* OpenDocumentFile(LPCTSTR lpszPathName, BOOL bMakeVisible = TRUE);
    Which you can see defaults to TRUE on the bMakeVisible variable. I tried this and it basically hangs the app before anything is shown (I think maybe because I'm using MFC9 with CFrameWndEx/CWinAppEx and the article that this came from was back in the VC6 days). Perhaps I'm doing something wrong with my override so I'll include it before someone asks:

    Code:
    //XSingleDocTemplate.h
    #pragma once
    #include "afxwin.h"
    
    class CXSingleDocTemplate : public CSingleDocTemplate
    {
    public:
      CXSingleDocTemplate(UINT nIDResource, CRuntimeClass* pDocClass,
    		CRuntimeClass* pFrameClass, CRuntimeClass* pViewClass);
      virtual ~CXSingleDocTemplate(void);
      virtual CDocument* OpenDocumentFile(LPCTSTR lpszPathName, BOOL bMakeVisible = TRUE);
    };
    Code:
    //XSingleDocTemplate.cpp
    #include "StdAfx.h"
    #include "XSingleDocTemplate.h"
    
    CXSingleDocTemplate::CXSingleDocTemplate(UINT nIDResource, CRuntimeClass* pDocClass,
    		CRuntimeClass* pFrameClass, CRuntimeClass* pViewClass):
    CSingleDocTemplate(nIDResource,pDocClass,pFrameClass,pViewClass)		
    {
     
    }
    
    CXSingleDocTemplate::~CXSingleDocTemplate(void)
    {
    }
    
    CDocument* CXSingleDocTemplate::OpenDocumentFile(LPCTSTR lpszPathName, BOOL bMakeVisible)
    {
      return CSingleDocTemplate::OpenDocumentFile(lpszPathName,FALSE);
    }
    It's a minor distraction, but I would like to clear it up cleanly if possible.

  2. #2
    Join Date
    May 2006
    Location
    Dresden, Germany
    Posts
    458

    Re: Can a Doc/view be hidden at startup?

    If I don't want to show a Dov/View MFC App I always set the Apps m_nCmdShow toSW_HIDE before this (in the CWinApp derived class's InitInstance() function) is called:
    Code:
    BOOL CMy0testApp::InitInstance()
    {
    
    	... do the InitInstance stuff here ....
    
    	m_nCmdShow = SW_HIDE; // add this
    
    	pMainFrame->ShowWindow(m_nCmdShow);
    	pMainFrame->UpdateWindow();
    
    	return TRUE;
    }
    This worked fine for me.

    With regards
    Programartist

  3. #3
    ovidiucucu's Avatar
    ovidiucucu is offline Moderator/Reviewer Power Poster
    Join Date
    Feb 2003
    Location
    Iasi - Romania
    Posts
    8,021

    Re: Can a Doc/view be hidden at startup?

    One solution is to override CFrameWnd::ActivateFrame and CWinAppEx::LoadWindowPlacement
    Code:
    class CMainFrame : public CFrameWndEx
    {
    // ...
    // Attributes
    public:
       BOOL m_bForceHidden;
       // ...
    // Overrides
    public:
       virtual void ActivateFrame(int nCmdShow = -1);
       //...
    };
    
    CMainFrame::CMainFrame() : m_bForceHidden(TRUE)
    {
       // ...
    }
    
    void CMainFrame::ActivateFrame(int nCmdShow)
    {
       if(m_bForceHidden)
       {
          nCmdShow = SW_HIDE;
          m_bForceHidden = FALSE;
       }
       CFrameWndEx::ActivateFrame(nCmdShow);
    }
    Code:
    class CMyApp : public CWinAppEx
    {
    // ...
    // Overrides
    public:
       virtual BOOL LoadWindowPlacement(CRect& rectNormalPosition, int& nFflags, int& nShowCmd);
       //...
    };
    
    BOOL CMyApp::LoadWindowPlacement(CRect& rectNormalPosition, int& nFflags, int& nShowCmd)
    {
       BOOL bRet = CWinAppEx::LoadWindowPlacement(rectNormalPosition, nFflags, nShowCmd);
       nShowCmd = SW_HIDE;
       return bRet;
    }
    I have tested this under Visual Studio 2010 Beta.
    Ovidiu Cucu
    "When in Rome, do as Romans do."
    Visit: Microsoft Virtual Academy
    Follow: https://twitter.com/#!/ovidiucucu
    My blog: http://codexpert.ro/blog/author/ovidiu-cucu/

  4. #4
    Join Date
    Feb 2005
    Posts
    2,160

    Re: Can a Doc/view be hidden at startup?

    Quote Originally Posted by ProgramArtist View Post
    If I don't want to show a Dov/View MFC App I always set the Apps m_nCmdShow toSW_HIDE before this (in the CWinApp derived class's InitInstance() function) is called:
    Code:
    BOOL CMy0testApp::InitInstance()
    {
    
    	... do the InitInstance stuff here ....
    
    	m_nCmdShow = SW_HIDE; // add this
    
    	pMainFrame->ShowWindow(m_nCmdShow);
    	pMainFrame->UpdateWindow();
    
    	return TRUE;
    }
    This worked fine for me.

    With regards
    Programartist
    This is what I was doing at first. You still get the frame window popping up for a split second before everything settles down.


    Quote Originally Posted by ovidiucucu View Post
    One solution is to override CFrameWnd::ActivateFrame and CWinAppEx::LoadWindowPlacement
    Code:
    class CMainFrame : public CFrameWndEx
    {
    // ...
    // Attributes
    public:
       BOOL m_bForceHidden;
       // ...
    // Overrides
    public:
       virtual void ActivateFrame(int nCmdShow = -1);
       //...
    };
    
    CMainFrame::CMainFrame() : m_bForceHidden(TRUE)
    {
       // ...
    }
    
    void CMainFrame::ActivateFrame(int nCmdShow)
    {
       if(m_bForceHidden)
       {
          nCmdShow = SW_HIDE;
          m_bForceHidden = FALSE;
       }
       CFrameWndEx::ActivateFrame(nCmdShow);
    }
    Code:
    class CMyApp : public CWinAppEx
    {
    // ...
    // Overrides
    public:
       virtual BOOL LoadWindowPlacement(CRect& rectNormalPosition, int& nFflags, int& nShowCmd);
       //...
    };
    
    BOOL CMyApp::LoadWindowPlacement(CRect& rectNormalPosition, int& nFflags, int& nShowCmd)
    {
       BOOL bRet = CWinAppEx::LoadWindowPlacement(rectNormalPosition, nFflags, nShowCmd);
       nShowCmd = SW_HIDE;
       return bRet;
    }
    I have tested this under Visual Studio 2010 Beta.

    Yay! That was the key. I'd discovered the ActivateFrame() overload after digging through tons of MFC code, but the LoadWindowPlacement() overload was not as obvious (and not part of the list of "normal" overloads). I'd managed to fake it with:

    Code:
      CRect r;
      int flg,shc;
      LoadWindowPlacement(r,flg,shc);
      StoreWindowPlacement(r,flg,SW_HIDE);
    in InitInstance(), but your solution is cleaner. Thanks.

  5. #5
    Join Date
    Jun 2009
    Posts
    1

    Re: [RESOLVED] Can a Doc/view be hidden at startup?

    Hi guys,

    I spent all morning searching for an answer to this same problem. My problem is that I was opening my MFC app as a separate process from a C# app, using Process.Start(ProcessStartInfo). No matter what I did, the MFC app would ignore the WindowStyle that I passed in (I tried passing WindowStyle.Hidden). I traced through all the MFC code and I thought I figured out what it was doing internally, I just didn't know how to fix it. I tried your solution and it worked, but I still couldn't figure out why. I put a breakpoint in my LoadWindowPlacement(...) override, and it hit me. Since I saw that this thread was so recent, I thought I'd let you in on what I found.

    I was looking at CFrameImpl::RestoreWindowPosition(CREATESTRUCT&), which calls LoadWindowPlacement(...). Directly thereafter, if nCmdShow is anything but SW_MAXIMIZE, SW_MINIMIZE, SW_SHOWMINIMIZED, or SW_SHOWMINNOACTIVE, the framework would set nCmdShow to SW_SHOWNORMAL. Therefore, it would seem that you could NEVER start the app out hidden. However, if that were true, then this solution wouldn't work--setting nCmdShow to SW_HIDE still wouldn't help anything because once it returns, RestoreWindowPosition(...) will continue to set nCmdShow to SW_SHOWNORMAL. I stuck my breakpoint in and, sure enough, it did just that.

    However, when I hit F5, I got into my LoadWindowPlacement(...) override again. It was called from CWinAppEx::LoadState(...). BUT, only

    Code:
    if (m_bLoadWindowPlacement)
    A quick search on m_bLoadWindowPlacement in MFC code led me to:

    Code:
    void EnableLoadWindowPlacement(BOOL bEnable = TRUE) { m_bLoadWindowPlacement = bEnable; }
    A simple call to EnableLoadWindowPlacement(FALSE) as the first line in my InitInstance() solved the problem. Try this out, and see if it does the trick for you, without all that extra work. I have to say, it didn't seem like performing such a simple task would require overrides of two different methods like that. The solution would HAVE to be something as simple as this.

  6. #6
    Join Date
    Feb 2005
    Posts
    2,160

    Re: [RESOLVED] Can a Doc/view be hidden at startup?

    Thanks for the tip! Still looks like you have to override CMainFrame::ActivateFrame() unless I missed something.

Posting Permissions

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


Azure Activities Information Page

Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center