程序关闭后,如果没有处理的话,再次打开程序,其位置都固定在一个位置显示。有时我们想让程序打开时,在上次关闭的位置显示。
实现起来也很简单。
关闭程序时,保存程序的位置信息。
程序打开时加载信息并设置。
程序关闭时,我们可以添加框架的关闭函数,在内部保存窗口位置信息
- void CMainFrame::OnClose()
- {
- WINDOWPLACEMENT* pWndpl = new WINDOWPLACEMENT;
- pWndpl->length = sizeof(WINDOWPLACEMENT);
- //获得窗口位置
- GetWindowPlacement(pWndpl);
- CWinApp* pApp = AfxGetApp();
- //保存窗口位置
- pApp->WriteProfileInt(_T("WINDOWPLACEMENT"), _T("FLAGS"), pWndpl->flags);
- pApp->WriteProfileInt(_T("WINDOWPLACEMENT"), _T("SHOWCMD"), pWndpl->showCmd);
- pApp->WriteProfileInt(_T("WINDOWPLACEMENT"), _T("MINX"), pWndpl->ptMinPosition.x);
- pApp->WriteProfileInt(_T("WINDOWPLACEMENT"), _T("MINY"), pWndpl->ptMinPosition.y);
- pApp->WriteProfileInt(_T("WINDOWPLACEMENT"), _T("MAXX"), pWndpl->ptMaxPosition.x);
- pApp->WriteProfileInt(_T("WINDOWPLACEMENT"), _T("MAXY"), pWndpl->ptMaxPosition.y);
- pApp->WriteProfileInt(_T("WINDOWPLACEMENT"), _T("TOP"), pWndpl->rcNormalPosition.left);
- pApp->WriteProfileInt(_T("WINDOWPLACEMENT"), _T("LEFT"), pWndpl->rcNormalPosition.top);
- pApp->WriteProfileInt(_T("WINDOWPLACEMENT"), _T("RIGHT"), pWndpl->rcNormalPosition.right);
- pApp->WriteProfileInt(_T("WINDOWPLACEMENT"), _T("BOTTOM"), pWndpl->rcNormalPosition.bottom);
- delete pWndpl;
- CFrameWnd::OnClose();
- }
复制代码
程序打开时我们可以在框架的初始化函数中加载保存的信息,并进行设置
- int CMainFrame::OnCreate(LPCREATESTRUCT lpCreateStruct)
- {
- if (CFrameWnd::OnCreate(lpCreateStruct) == -1)
- return -1;
-
- if (!m_wndToolBar.CreateEx(this, TBSTYLE_FLAT, WS_CHILD | WS_VISIBLE | CBRS_TOP
- | CBRS_GRIPPER | CBRS_TOOLTIPS | CBRS_FLYBY | CBRS_SIZE_DYNAMIC) ||
- !m_wndToolBar.LoadToolBar(IDR_MAINFRAME))
- {
- TRACE0("Failed to create toolbar\n");
- return -1; // fail to create
- }
- if (!m_wndStatusBar.Create(this) ||
- !m_wndStatusBar.SetIndicators(indicators,
- sizeof(indicators)/sizeof(UINT)))
- {
- TRACE0("Failed to create status bar\n");
- return -1; // fail to create
- }
- // TODO: Delete these three lines if you don't want the toolbar to
- // be dockable
- m_wndToolBar.EnableDocking(CBRS_ALIGN_ANY);
- EnableDocking(CBRS_ALIGN_ANY);
- DockControlBar(&m_wndToolBar);
- WINDOWPLACEMENT* pWndpl = new WINDOWPLACEMENT;
- pWndpl->length = sizeof(WINDOWPLACEMENT);
-
- CWinApp* pApp = AfxGetApp();
- //保存窗口位置
- pWndpl->flags=pApp->GetProfileInt(_T("WINDOWPLACEMENT"), _T("FLAGS"),0);
- pWndpl->showCmd =pApp->GetProfileInt(_T("WINDOWPLACEMENT"), _T("SHOWCMD"), 0);
- pWndpl->ptMinPosition.x = pApp->GetProfileInt(_T("WINDOWPLACEMENT"), _T("MINX"), 0);
- pWndpl->ptMinPosition.y = pApp->GetProfileInt(_T("WINDOWPLACEMENT"), _T("MINY"), 0);
- pWndpl->ptMaxPosition.x = pApp->GetProfileInt(_T("WINDOWPLACEMENT"), _T("MAXX"), 0);
- pWndpl->ptMaxPosition.y = pApp->GetProfileInt(_T("WINDOWPLACEMENT"), _T("MAXY"), 0);
- pWndpl->rcNormalPosition.left = pApp->GetProfileInt(_T("WINDOWPLACEMENT"), _T("TOP"), 0);
- pWndpl->rcNormalPosition.top = pApp->GetProfileInt(_T("WINDOWPLACEMENT"), _T("LEFT"), 0);
- pWndpl->rcNormalPosition.right = pApp->GetProfileInt(_T("WINDOWPLACEMENT"), _T("RIGHT"), 0);
- pWndpl->rcNormalPosition.bottom = pApp->GetProfileInt(_T("WINDOWPLACEMENT"), _T("BOTTOM"), 0);
- SetWindowPlacement(pWndpl);
- delete pWndpl;
- return 0;
- }
复制代码 这样就可以很方面的实现我们想要 的功能
上位机VC MFC程序开发精典实例大全源码与视频讲解配套下载408例 经历1年的编程与录制点击进入查看
如果您认可,可联系功能定制! 如果您着急,充值会员可直接联系发您资料!
|