Thread: MFC: Need help with WinodwPlacement

  1. #1
    Registered User
    Join Date
    Nov 2001
    Posts
    47

    Question MFC: Need help with WinodwPlacement

    Hi all!,

    i've generated a standard MFC application using the Wizzard under VC++6.0.

    I want to save the WINDOWPLACEMENT when the window closes and load the data when it opens.

    Code:
    /////////////////////////////////////////////////////////////////////////////
    // CMainFrame message handlers
    
    
    void CMainFrame::OnShowWindow(BOOL bShow, UINT nStatus) 
    {
    	CFrameWnd::OnShowWindow(bShow, nStatus);
    	
    	if (bShow && !IsWindowVisible())
    	{
    		WINDOWPLACEMENT *wp;
    		UINT		    nByte;
    		if (AfxGetApp()->GetProfileBinary ("Window", "WP", (LPBYTE*)&wp, &nByte))
    		{
    			wp->length = sizeof(WINDOWPLACEMENT);
    			SetWindowPlacement(wp);
    			delete [] wp;
    		}
    	}
    	// TODO: Add your message handler code here
    	
    }
    
    void CMainFrame::OnClose() 
    {
    	// TODO: Add your message handler code here and/or call default
    	WINDOWPLACEMENT wp;
    
    	wp.length = sizeof(WINDOWPLACEMENT);
    	GetWindowPlacement(&wp);
    	AfxGetApp()->WriteProfileBinary ("Window", "WP", (BYTE *)&wp, sizeof(WINDOWPLACEMENT));
    
    	CFrameWnd::OnClose();
    }
    A Stack Overflow within the OnShowWindow appears when it gots started.

    Please Help!

    Regards,
    Robert

  2. #2
    Registered User
    Join Date
    Nov 2001
    Posts
    47
    i've added some TRACE lines ...

    Code:
    		if (AfxGetApp()->GetProfileBinary (_T("Window"), _T("WP"),  (LPBYTE *)&wp, &nByte))
    		{
    			TRACE("WINDOWPLACEMENT:\n");
    			TRACE("Flags:   %ld\n", wp->flags );
    			TRACE("ShowCMD: %ld\n", wp->showCmd );
    
    			SetWindowPlacement(wp);
    ... the message
    WINDOWPLACEMENT:
    Flags: 0
    ShowCMD: 1

    appears in the debug window for 348 times and the it crashes.

    Any idea what's wrong ?

    -
    Robert

  3. #3
    Registered User
    Join Date
    Jun 2003
    Posts
    70
    I think mainframe's PreCreateWindow(CREATESTRUCT& cs) virtual function is best place to positioned your application window.
    Chintan R Naik

  4. #4
    Registered User
    Join Date
    Nov 2001
    Posts
    47
    thanks , but i'd like to use the SetWindowPlacement function instead of manipulation the CREATESTRUCT

    Meanwhile i've moved the code

    Code:
    	WINDOWPLACEMENT *wp;
    	UINT		    nByte=0;
    	if (AfxGetApp()->GetProfileBinary ("Window", "WP",  (LPBYTE *)&wp, &nByte))
    	{
    		m_pMainWnd->SetWindowPlacement (wp);
    		delete [] wp;
    	}
    to the CMyApp::InitInstance() method just infornt of

    Code:
    	// The one and only window has been initialized, so show and update it.
    	m_pMainWnd->ShowWindow(SW_SHOW);
    	m_pMainWnd->UpdateWindow();
    and the window gets restores correctly.

    -
    Robert

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. WIndows programming?
    By hostensteffa in forum Windows Programming
    Replies: 7
    Last Post: 06-07-2002, 08:52 PM
  2. Release MFC Programs & Dynamic MFC DLL :: MFC
    By kuphryn in forum Windows Programming
    Replies: 2
    Last Post: 05-18-2002, 06:42 PM
  3. Understanding The Future of MFC
    By kuphryn in forum Windows Programming
    Replies: 2
    Last Post: 04-15-2002, 09:08 PM
  4. Beginning MFC (Prosise) Part III - Now What? :: C++
    By kuphryn in forum C++ Programming
    Replies: 5
    Last Post: 03-03-2002, 06:58 PM
  5. MFC is Challenging :: C++
    By kuphryn in forum C++ Programming
    Replies: 8
    Last Post: 02-05-2002, 01:33 AM