QQ登录

只需一步,快速开始

上位机MFC如何获取窗口位置

[ 复制链接 ]
很多应用程序需要记住上一次窗口的位置和状态信息(最大化、最小化和正常),
我们在这里来完成该功能。
下面是存储窗口的位置代码:
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();
}

下面是获取窗口的位置:
BOOL CMainFrame:reCreateWindow(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:reCreateWindow(cs);
}



回复

使用道具 举报

快速回复 返回列表 客服中心 搜索