Click here to Skip to main content
15,896,912 members
Please Sign up or sign in to vote.
2.71/5 (3 votes)
See more:
//I use VC++ using MFC:
C++
/* I want to restore some position and size of windows when I click SIZE_RESTORE button with MoveWindow()?
*/
void CPortViewModbusDlg::OnSize(UINT nType, int cx, int cy)
{
	//There are 3 objects - One Tab has 3 forms, and 1-3 ListCtrls inside one form.

	CDialogEx::OnSize(nType, cx, cy);
		
	CRect r;	
	
	if((nType == SIZE_MAXIMIZED)
	{
	
		if (m_Tab.m_hWnd && cx && cy) // This is main Tab
		{
                        // main Tab is automatically spread to full size of monitor
			m_Tab.ShowWindow(SW_MAXIMIZE); 

                        // m_form1000 is one of child windows of m_Tab.
			if (m_form1000.m_hWnd ; cx ; cy) 

			{				
				GetClientRect(r);
				ClientToScreen(r);// Getting full size of monitor

				m_form1000.MoveWindow(r.left, r.top, cx, cy, TRUE); // I force m_form1000 to spread fully on the m_Tab

				m_form1000.m_List_1100.GetWindowRect(r); // m_List_1100 is ListCtrl upon m_form1000				
								
				// Getting the coordinates of m_List_1100
				m_form1000.m_List_1100.GetClientRect(r);
				m_form1000.m_List_1100.ClientToScreen(r);

				originL1000 = r; // Saving of original coordinates of m_List_1100

				int lcx = cx * 90 / 100;
				int lcy = cy * 80 / 100;
				
				// expand the size of m_List_1100 to get along with m_form1000 size.
				m_form1000.m_List_1100.MoveWindow(r.left, r.top, lcx, lcy);
			}
		
		
	}
	else if ((nType == SIZE_RESTORED) 
	{
		m_Tab.ShowWindow(SW_RESTORE); // This makes m_Tab to be constricted to its original size

		m_form1000.ShowWindow(SW_RESTORE);// This makes error! m_form1000 doesnt remember its original size, m_form1000 not changed.					
	
		// m_List_1100 recall its saved original coordinates. So put 4 positions are put in to the MoveWindow().
		m_form1000.m_List_1100.MoveWindow(originL1000.left, originL1000.top, originL1000.right, originL1000.bottom);
		// This makes same error m_List_1100 cannot find its original positions and size.....

		// I have made many coordinates to try test the functions like GetWindowRect(), GetClientRect(), ScreenToClient(), ClientToScreen()
		
		/* Only one thing I want to know is:
 		
		How do I get the original coordinates of windows before SIZE_MAXIMIZED and How to use the coordinates to MoveWindows()?
		
		Thank you.

		*/

		
	}
}


What I have tried:

Dont be surprised, I wasted 10 days of this problem.
Posted
Updated 2-Jun-16 22:09pm
v3
Comments
Richard MacCutchan 2-Jun-16 3:40am    
Please edit your question and show:
1. the actual code that you use
2. the values of the rect variables
3. the error code when the resize fails
Richard MacCutchan 3-Jun-16 3:58am    
I cannot see where you save the original size and location of your main window. All you really need to do is to save the size and location of your main Window. Whenever you resize your application, you recalculate the client area and adjust the size of your child windows to fit within that space. Most of the time you do not need to do this as Windows will provide the values for you.
Richard MacCutchan 3-Jun-16 3:59am    
You also have a line of code:
if (m_form1000.m_hWnd ; cx ; cy)
What is that supposed to do?

1 solution

Take a look at this example, you can use it as it is:

Saving the window's position:
C++
void CMainFrame::OnClose() // message handler for WM_CLOSE
{
    // Save main window position
    CWinApp* app = AfxGetApp();
    WINDOWPLACEMENT wp;
    GetWindowPlacement(&wp);
    app->WriteProfileInt("Frame", "Status", wp.showCmd);
    app->WriteProfileInt("Frame", "Top",    wp.rcNormalPosition.top);
    app->WriteProfileInt("Frame", "Left",   wp.rcNormalPosition.left);
    app->WriteProfileInt("Frame", "Bottom", wp.rcNormalPosition.bottom);
    app->WriteProfileInt("Frame", "Right",  wp.rcNormalPosition.right);

    CFrameWnd::OnClose();
}


Restoring it:
C++
BOOL CMainFrame::PreCreateWindow(CREATESTRUCT& cs)
{
    //
    // Restore main window position
    //

    CWinApp* app = AfxGetApp();
    int s, t, b, r, l;

    // only restore if there is a previously saved position
    if ( -1 != (s = app->GetProfileInt("Frame", "Status",   -1)) &&
         -1 != (t = app->GetProfileInt("Frame", "Top",      -1)) &&
         -1 != (l = app->GetProfileInt("Frame", "Left",     -1)) &&
         -1 != (b = app->GetProfileInt("Frame", "Bottom",   -1)) &&
         -1 != (r = app->GetProfileInt("Frame", "Right",    -1))
       ) {

        // restore the window's status
        app->m_nCmdShow = s;

        // restore the window's width and height
        cs.cx = r - l;
        cs.cy = b - t;

        // the following correction is needed when the taskbar is
        // at the left or top and it is not "auto-hidden"
        RECT workArea;
        SystemParametersInfo(SPI_GETWORKAREA, 0, &workArea, 0);
        l += workArea.left;
        t += workArea.top;

        // make sure the window is not completely out of sight
        int max_x = GetSystemMetrics(SM_CXSCREEN) -
                        GetSystemMetrics(SM_CXICON);
        int max_y = GetSystemMetrics(SM_CYSCREEN) -
                        GetSystemMetrics(SM_CYICON);
        cs.x = min(l, max_x);
        cs.y = min(t, max_y);
    }

    return CFrameWnd::PreCreateWindow(cs);
}
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900