【问题标题】:Is there a notification when a window is moved to another monitor?当窗口移动到另一个监视器时是否有通知?
【发布时间】:2021-04-16 23:54:31
【问题描述】:

我有一个在垂直同步关闭的情况下由IDXGISwapChain1 刷新的窗口。如果可能,我想手动控制窗口的当前速率,使其尽可能接近当前的显示器刷新率。

在将具有不同刷新率的两台显示器连接到 PC 的情况下,我希望在窗口从一台显示器移动到另一台显示器时收到通知,以便我可以相应地更改当前的帧速率。

据我所知,WM_DISPLAYCHANGE 和WM_DPICHANGED 可能无法涵盖这种情况。如果我错了,请纠正我。

更新: 这是我根据 Strive 的回答编写的代码,

LRESULT OnPositionChanged(UINT nMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled)
{
    // Get the monitor handle for the window. 
    HMONITOR currentMonitor = MonitorFromWindow(m_hWnd, MONITOR_DEFAULTTONEAREST);
    // error handling

    // no need to bother if the window stays on the same monitor
    if (_currentMonitor != currentMonitor)
    {
        // Get more information about the monitor
        MONITORINFOEX monitorInfo;
        monitorInfo.cbSize = sizeof(MONITORINFOEX);
        BOOL succeed = GetMonitorInfo(currentMonitor, (LPMONITORINFO)&monitorInfo);
        // error handling

        // Get the current display settings for that monitor, which includes the refresh rate
        DEVMODE devMode;
        devMode.dmSize = sizeof(DEVMODE);
        succeed = EnumDisplaySettings(monitorInfo.szDevice, ENUM_CURRENT_SETTINGS, &devMode);
        // error handling

        // use devMode.dmDisplayFrequency to update my present frame rate
        // ...

        _currentMonitor = currentMonitor;
    }

    return 0L;
}

LRESULT OnDisplayChanged(UINT nMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled)
{
    // WM_DISPLAYCHANGE message is sent to all windows when the display resolution has changed
    // When a WM_DISPLAYCHANGE message is sent, any monitor may be removed from the desktop and thus its HMONITOR becomes invalid or has its settings changed.

    _currentMonitor = NULL;

    return 0L;
}

我想在窗口改变位置后改变帧率,所以我切换到WM_WINDOWPOSCHANGED。

【问题讨论】:

    标签: windows winapi window


    【解决方案1】:

    窗口位置改变后,可以查看WM_WINDOWPOSCHANGING消息。

    发送到一个窗口,其大小、位置或在 Z 顺序中的位置是 由于调用 SetWindowPos 函数或 另一个窗口管理功能。

    然后通过调用EnumDisplaySettings函数获取当前监视器的刷新赫兹。如果刷新频率发生变化,请更改应用程序的刷新频率。

    dmDisplayFrequency :指定频率,以赫兹为单位(每周期 第二)、特定模式下的显示设备。这个值是 也称为显示设备的垂直刷新率。展示 驱动程序使用此成员。例如,它用于 更改显示设置功能。打印机驱动程序不使用它 会员。

    注意:您可以设置一个变量来保存之前显示的刷新频率,然后将该变量与当前显示的频率进行比较。如果不一样,说明Hz变了。

    如果需要确定窗口在哪个监视器上,可以使用MonitorFromWindow函数。

    这是一个代码示例:How to determine which monitor an application is using and how to get a handle to it?

    【讨论】:

      猜你喜欢
      • 2017-11-28
      • 2023-03-24
      • 2013-10-11
      • 1970-01-01
      • 1970-01-01
      • 2022-01-05
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多