如何恢复窗口位置和大小? [英] How do I restore window position and size?

查看:323
本文介绍了如何恢复窗口位置和大小?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

//我使用MFC使用VC ++:

//I use VC++ using MFC:

/* 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.

		*/

		
	}
}





我尝试了什么:



不要惊讶,我浪费了10天这个问题。



What I have tried:

Dont be surprised, I wasted 10 days of this problem.

推荐答案

看看这个示例,您可以按原样使用它:



保存窗口的位置:

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

Saving the window's position:
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:

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);
}


这篇关于如何恢复窗口位置和大小?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆