工控编程吧

标题: 上位机MFC如何保存窗口关闭的位置 [打印本页]

作者: qq263946146    时间: 2019-7-24 00:01
标题: 上位机MFC如何保存窗口关闭的位置
程序关闭后,如果没有处理的话,再次打开程序,其位置都固定在一个位置显示。有时我们想让程序打开时,在上次关闭的位置显示。
实现起来也很简单。
关闭程序时,保存程序的位置信息。
程序打开时加载信息并设置。
程序关闭时,我们可以添加框架的关闭函数,在内部保存窗口位置信息
  1. void CMainFrame::OnClose()
  2. {
  3.         WINDOWPLACEMENT* pWndpl = new WINDOWPLACEMENT;
  4.         pWndpl->length = sizeof(WINDOWPLACEMENT);

  5.         //获得窗口位置
  6.         GetWindowPlacement(pWndpl);

  7.         CWinApp* pApp = AfxGetApp();

  8.         //保存窗口位置
  9.         pApp->WriteProfileInt(_T("WINDOWPLACEMENT"), _T("FLAGS"), pWndpl->flags);
  10.         pApp->WriteProfileInt(_T("WINDOWPLACEMENT"), _T("SHOWCMD"), pWndpl->showCmd);
  11.         pApp->WriteProfileInt(_T("WINDOWPLACEMENT"), _T("MINX"), pWndpl->ptMinPosition.x);
  12.         pApp->WriteProfileInt(_T("WINDOWPLACEMENT"), _T("MINY"), pWndpl->ptMinPosition.y);
  13.         pApp->WriteProfileInt(_T("WINDOWPLACEMENT"), _T("MAXX"), pWndpl->ptMaxPosition.x);
  14.         pApp->WriteProfileInt(_T("WINDOWPLACEMENT"), _T("MAXY"), pWndpl->ptMaxPosition.y);
  15.         pApp->WriteProfileInt(_T("WINDOWPLACEMENT"), _T("TOP"), pWndpl->rcNormalPosition.left);
  16.         pApp->WriteProfileInt(_T("WINDOWPLACEMENT"), _T("LEFT"), pWndpl->rcNormalPosition.top);
  17.         pApp->WriteProfileInt(_T("WINDOWPLACEMENT"), _T("RIGHT"), pWndpl->rcNormalPosition.right);
  18.         pApp->WriteProfileInt(_T("WINDOWPLACEMENT"), _T("BOTTOM"), pWndpl->rcNormalPosition.bottom);

  19.         delete pWndpl;

  20.         CFrameWnd::OnClose();
  21. }
复制代码



程序打开时我们可以在框架的初始化函数中加载保存的信息,并进行设置
  1. int CMainFrame::OnCreate(LPCREATESTRUCT lpCreateStruct)
  2. {
  3.         if (CFrameWnd::OnCreate(lpCreateStruct) == -1)
  4.                 return -1;
  5.        
  6.         if (!m_wndToolBar.CreateEx(this, TBSTYLE_FLAT, WS_CHILD | WS_VISIBLE | CBRS_TOP
  7.                 | CBRS_GRIPPER | CBRS_TOOLTIPS | CBRS_FLYBY | CBRS_SIZE_DYNAMIC) ||
  8.                 !m_wndToolBar.LoadToolBar(IDR_MAINFRAME))
  9.         {
  10.                 TRACE0("Failed to create toolbar\n");
  11.                 return -1;      // fail to create
  12.         }

  13.         if (!m_wndStatusBar.Create(this) ||
  14.                 !m_wndStatusBar.SetIndicators(indicators,
  15.                   sizeof(indicators)/sizeof(UINT)))
  16.         {
  17.                 TRACE0("Failed to create status bar\n");
  18.                 return -1;      // fail to create
  19.         }

  20.         // TODO: Delete these three lines if you don't want the toolbar to
  21.         //  be dockable
  22.         m_wndToolBar.EnableDocking(CBRS_ALIGN_ANY);
  23.         EnableDocking(CBRS_ALIGN_ANY);
  24.         DockControlBar(&m_wndToolBar);


  25.         WINDOWPLACEMENT* pWndpl = new WINDOWPLACEMENT;
  26.         pWndpl->length = sizeof(WINDOWPLACEMENT);
  27.        

  28.         CWinApp* pApp = AfxGetApp();
  29.         //保存窗口位置
  30.         pWndpl->flags=pApp->GetProfileInt(_T("WINDOWPLACEMENT"), _T("FLAGS"),0);
  31.         pWndpl->showCmd =pApp->GetProfileInt(_T("WINDOWPLACEMENT"), _T("SHOWCMD"), 0);
  32.         pWndpl->ptMinPosition.x = pApp->GetProfileInt(_T("WINDOWPLACEMENT"), _T("MINX"), 0);
  33.         pWndpl->ptMinPosition.y = pApp->GetProfileInt(_T("WINDOWPLACEMENT"), _T("MINY"), 0);
  34.         pWndpl->ptMaxPosition.x = pApp->GetProfileInt(_T("WINDOWPLACEMENT"), _T("MAXX"), 0);
  35.         pWndpl->ptMaxPosition.y = pApp->GetProfileInt(_T("WINDOWPLACEMENT"), _T("MAXY"), 0);
  36.         pWndpl->rcNormalPosition.left = pApp->GetProfileInt(_T("WINDOWPLACEMENT"), _T("TOP"), 0);
  37.         pWndpl->rcNormalPosition.top = pApp->GetProfileInt(_T("WINDOWPLACEMENT"), _T("LEFT"), 0);
  38.         pWndpl->rcNormalPosition.right = pApp->GetProfileInt(_T("WINDOWPLACEMENT"), _T("RIGHT"), 0);
  39.         pWndpl->rcNormalPosition.bottom = pApp->GetProfileInt(_T("WINDOWPLACEMENT"), _T("BOTTOM"), 0);
  40.     SetWindowPlacement(pWndpl);
  41.         delete pWndpl;

  42.         return 0;
  43. }
复制代码
这样就可以很方面的实现我们想要 的功能
[MFC408]1[/MFC408]
[weixinlianxi]1[/weixinlianxi]






欢迎光临 工控编程吧 (https://www.gkbc8.com/) Powered by Discuz! X3.4