简体   繁体   中英

Save and restore Aero Snap position on Windows 7

Let's say I have a window and I want to save its position when the window closes and restore it when the window is opened again. The typical way to do this is to call GetWindowPlacement / SetWindowPlacement. This takes care of remember the position and the maximized / minimized state.

On Windows 7, you can dock a window to the side of the screen using the "Aero Snap" feature. My question is how do you save and restore windows that have been "Snapped" so that you can restore the "Snap" state. GetWindowPlacement / SetWindowPlacement does not solve this problem (to my knowledge) and I haven't seen any "Snap" API's in Windows 7.

There is a similar question on here How to detect window was resized by Windows7 but in this case it seems that the OP just wanted the restore position, not the "Snap" state.

解决方法是调用GetWindowRect()来获取实际窗口坐标并将它们复制到WINDOWPLACEMENT::rcNormalPosition的坏坐标上。

The way I solved it was to override CWinAppEx::SaveState, to update the WINDOWPLACEMENT before saving it:

BOOL MyApp:SaveState(LPCTSTR lpszSectionName, CFrameImpl *pFrameImpl)
{
  WINDOWPLACEMENT wp;
  wp.length = sizeof(WINDOWPLACEMENT);
  m_pMainWnd->GetWindowPlacement(&wp);
  if (wp.showCmd == SW_SHOWNORMAL)
  {
    m_pMainWnd->GetWindowRect(&wp.rcNormalPosition);
    m_pMainWnd->SetWindowPlacement(&wp);
  }

  return __super::SaveState(lpszSectionName, pFrameImpl);
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM