CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 11 of 11
  1. #1
    Join Date
    Oct 2005
    Location
    China
    Posts
    137

    window misplaced when it maximized.

    I want to keep my SDI application's window always maximized.
    So I disable the maximize button ,and maximized the window when it initially opened.
    I did it as follows.
    Code:
    BOOL CMainFrame::PreCreateWindow(CREATESTRUCT& cs)
    {
    	if( !CMDIFrameWnd::PreCreateWindow(cs) )
    		return FALSE;
    
     	cs.style &= ~WS_MAXIMIZEBOX;
    
    	return TRUE;
    }
    
    BOOL CMyApp::InitInstance()
    {
    	// The main window has been initialized, so show and update it.
    	pMainFrame->ShowWindow(SW_SHOWMAXIMIZED);
    	pMainFrame->UpdateWindow();
    
    	return TRUE;
    }
    The window were shown with the wrong size.
    It was higher than expected, therefore the status bar hided behind the system's task bar.

    But, if we left the maximize button, the windows size will be right.

    Why?
    How can I keep the window maximized always?
    Thanks.
    Attached Images Attached Images   

  2. #2
    Join Date
    Jul 2002
    Posts
    2,543

    Re: window misplaced when it maximized.

    You can keep the window with WS_MAXIMIZEBOX style, and ignore Maximize - Restore commands:

    Code:
    void CMainFrame::OnSysCommand(UINT nID, LPARAM lParam)
    {
        if ( nID == SC_RESTORE   ||  nID == SC_MAXIMIZE )
        {
            return;
        }
    
        CFrameWnd::OnSysCommand(nID, lParam);
    }

  3. #3
    Join Date
    Oct 2005
    Location
    China
    Posts
    137

    Re: window misplaced when it maximized.

    Quote Originally Posted by Alex F View Post
    You can keep the window with WS_MAXIMIZEBOX style, and ignore Maximize - Restore commands:

    Code:
    void CMainFrame::OnSysCommand(UINT nID, LPARAM lParam)
    {
        if ( nID == SC_RESTORE   ||  nID == SC_MAXIMIZE )
        {
            return;
        }
    
        CFrameWnd::OnSysCommand(nID, lParam);
    }
    Thanks.
    But if ignore the SC_RESTORE, we can not restore the window when it minimized.

  4. #4
    Join Date
    Oct 2005
    Location
    China
    Posts
    137

    Re: window misplaced when it maximized.

    Maybe we can do it by holding back the mouse action.
    Code:
    void CMainFrame::OnNcLButtonDblClk(UINT nHitTest, CPoint point) 
    {
    	// TODO: Add your message handler code here and/or call default
    	if (nHitTest == HTCAPTION   )
    	{
    		return;
    	}
    	CMDIFrameWnd::OnNcLButtonDblClk(nHitTest, point);
    }
    
    void CMainFrame::OnNcLButtonDown(UINT nHitTest, CPoint point) 
    {
    	// TODO: Add your message handler code here and/or call default
    	if (nHitTest == HTMAXBUTTON)
    	{
    		return;
    	}
    
    
    	CMDIFrameWnd::OnNcLButtonDown(nHitTest, point);
    }
    There is one question left.
    Why the task bar is misplaced when we disable the maximize box?

  5. #5
    Join Date
    May 1999
    Location
    ALABAMA, USA
    Posts
    9,917

    Re: window misplaced when it maximized.

    In a nutshell: Window can be in a minimized, maximized state or something in between.
    About WS_MAXIMIZEBOX style:
    The name of the style is slightly misleading. This style allows toggling between maximized and last saved size.

    In your case, window can be maximized or minimized; that is it.
    BUT
    For windows with WS_MAXIMIZEBOX style, system calculate maximum size based upon desktop working area to compensate for a task bar.

    If you remove WS_MAXIMIZEBOX, window can have only set style and minimized. That is if frame is not resizable. System soes not bother to compensate, since you may want to run kiosk type of the application.

    If you want both, you have to tell windows what maximum size you require despite removed WS_MAXIMIZEBOX. To do so, handle WM_GETMINMAXINFO message in a main frame. Retrieve required height using GetSystemMetrics(SM_CYMAXIMIZED). This returns height of the desktop not obscured by task bar.

    In WM_GETMINMAXINFO handler, assign this value to ptMaxSize.y member of the MINMAXINFO structure.

    Code:
    //In a header declare variable to hold height:
    protected:  // control bar embedded members
    	CStatusBar  m_wndStatusBar;
    	CToolBar    m_wndToolBar;
    	int m_iDesktopHeight;
    
    // call GetSystemMetrics from OnCreate  or PreCreateWindow function.
    
    //In a WM_GETMINMAXINFO handler:
    
    void CMainFrame::OnGetMinMaxInfo(MINMAXINFO FAR* lpMMI) 
    {	
    
    
    	// set maximum height
    	lpMMI->ptMaxSize.y = m_iDesktopHeight;
    
    	CMDIFrameWnd::OnGetMinMaxInfo(lpMMI);
    }
    This will set proper height even without WS_MAXIMIZEBOX style set.
    Last edited by JohnCz; November 4th, 2009 at 10:40 PM.
    There are only 10 types of people in the world:
    Those who understand binary and those who do not.

  6. #6
    Join Date
    May 1999
    Location
    ALABAMA, USA
    Posts
    9,917

    Re: window misplaced when it maximized.

    Quote Originally Posted by tjuzhangrui View Post
    Maybe we can do it by holding back the mouse action.
    Nope.
    Mouse has nothing to do with it. see my prevoius post.
    There are only 10 types of people in the world:
    Those who understand binary and those who do not.

  7. #7
    Join Date
    Oct 2005
    Location
    China
    Posts
    137

    Re: window misplaced when it maximized.

    Quote Originally Posted by JohnCz View Post
    In a nutshell: Window can be in a minimized, maximized state or something in between.
    About WS_MAXIMIZEBOX style:
    The name of the style is slightly misleading. This style allows toggling between maximized and last saved size.

    In your case, window can be maximized or minimized; that is it.
    BUT
    For windows with WS_MAXIMIZEBOX style, system calculate maximum size based upon desktop working area to compensate for a task bar.

    If you remove WS_MAXIMIZEBOX, window can have only set style and minimized. That is if frame is not resizable. System soes not bother to compensate, since you may want to run kiosk type of the application.

    If you want both, you have to tell windows what maximum size you require despite removed WS_MAXIMIZEBOX. To do so, handle WM_GETMINMAXINFO message in a main frame. Retrieve required height using GetSystemMetrics(SM_CYMAXIMIZED). This returns height of the desktop not obscured by task bar.

    In WM_GETMINMAXINFO handler, assign this value to ptMaxSize.y member of the MINMAXINFO structure.

    Code:
    //In a header declare variable to hold height:
    protected:  // control bar embedded members
    	CStatusBar  m_wndStatusBar;
    	CToolBar    m_wndToolBar;
    	int m_iDesktopHeight;
    
    // call GetSystemMetrics from OnCreate  or PreCreateWindow function.
    
    //In a WM_GETMINMAXINFO handler:
    
    void CMainFrame::OnGetMinMaxInfo(MINMAXINFO FAR* lpMMI) 
    {	
    
    
    	// set maximum height
    	lpMMI->ptMaxSize.y = m_iDesktopHeight;
    
    	CMDIFrameWnd::OnGetMinMaxInfo(lpMMI);
    }
    This will set proper height even without WS_MAXIMIZEBOX style set.
    Thanks, but in this way , we can restore the window by double clicking it's title-bar, and move it by draging it's title-bar.
    This behavior is different from the window which is maximized by push the maximize button.

  8. #8
    Join Date
    Jul 2002
    Posts
    2,543

    Re: window misplaced when it maximized.

    Code:
    void CMainFrame::OnSysCommand(UINT nID, LPARAM lParam)
    {
        if ( (nID == SC_RESTORE && (! IsIconic()) ) || nID == SC_MAXIMIZE )
        {
            return;
        }
    
        CFrameWnd::OnSysCommand(nID, lParam);
    }
    
    void CMainFrame::OnNcLButtonDblClk(UINT nHitTest, CPoint point)
    {
        // TODO: Add your message handler code here and/or call default
    
        //CFrameWnd::OnNcLButtonDblClk(nHitTest, point);
    }

  9. #9
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Location
    Florida
    Posts
    12,635

    Re: window misplaced when it maximized.

    The pictures show the mainframe maximized but not the view. You need to maximize the view when you create it also.

  10. #10
    Join Date
    Oct 2005
    Location
    China
    Posts
    137

    Re: window misplaced when it maximized.

    Quote Originally Posted by Alex F View Post
    Code:
    void CMainFrame::OnSysCommand(UINT nID, LPARAM lParam)
    {
        if ( (nID == SC_RESTORE && (! IsIconic()) ) || nID == SC_MAXIMIZE )
        {
            return;
        }
    
        CFrameWnd::OnSysCommand(nID, lParam);
    }
    
    void CMainFrame::OnNcLButtonDblClk(UINT nHitTest, CPoint point)
    {
        // TODO: Add your message handler code here and/or call default
    
        //CFrameWnd::OnNcLButtonDblClk(nHitTest, point);
    }
    It works.

    Thanks all for the help.

  11. #11
    Join Date
    Oct 2005
    Location
    China
    Posts
    137

    Re: window misplaced when it maximized.

    Quote Originally Posted by GCDEF View Post
    The pictures show the mainframe maximized but not the view. You need to maximize the view when you create it also.
    Sure. I will do this next.
    Thanks.

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