我有一个MFC应用程序,该应用程序会自动将其工作空间(窗口位置,打开的窗格,大小等)保存并还原到Windows注册表(例如HKCU\Software\foo\bar\Workspace)。它工作正常。

现在,我有兴趣在加载任何其他窗口(用户要求)之前显示启动屏幕。此初始屏幕必须与显示应用程序主窗口的屏幕相同。

我注意到在注册表中有一个值HKCU\Software\foo\bar\Workspace\WindowPlacement\MainWindowRect,我猜它包含有关左上角点和窗口大小的信息。有了这些信息,我就能获得正确的屏幕编号(如果对操作方法感兴趣,请参阅this other post)。

如何获得并解释该MainWindowRect值?

最佳答案

据我所知,MainWindowRect恰好是RECT结构的直接内存转储。一个简单的转换就足以获得窗口的矩形。

这里是获取值的完整代码段以及相关的监视器:

// Assumes SetRegistryKey has been already called
if (const auto hKey = AfxGetApp()->GetSectionKey("Workspace\\WindowPlacement")) {
  DWORD dwReturn[32];
  DWORD dwBufSize = sizeof(dwReturn);
  if (RegQueryValueEx(hKey, "MainWindowRect", 0, 0, (LPBYTE)dwReturn, &dwBufSize) == ERROR_SUCCESS) {
    const auto rectWindow = *(RECT*)dwReturn;

    // Get monitor index from window's rect
    const auto hMonitor = MonitorFromRect(&rectWindow, MONITOR_DEFAULTTONEAREST);
    const auto iMonitorIndex = GetMonitorIndex(hMonitor); // see linked post for GetMonitorIndex implementation
  }
  RegCloseKey(hKey);
}

09-26 20:45