当 window 移动到另一台显示器时是否有通知?

Is there a notification when a window is moved to another monitor?

我有一个 window,在关闭 V-Sync 的情况下由 IDXGISwapChain1 刷新。如果可能的话,我想手动控制 window 的当前速率,使其尽可能接近当前显示器的刷新率。

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

据我所知,

WM_DISPLAYCHANGEWM_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;
}

我想在 window 改变位置后更改帧率,所以我切换到 WM_WINDOWPOSCHANGED

window位置发生变化后,可以查看WM_WINDOWPOSCHANGING消息

Sent to a window whose size, position, or place in the Z order is about to change as a result of a call to the SetWindowPos function or another window-management function.

然后调用EnumDisplaySettings函数获取当前显示器的刷新频率。如果刷新频率发生变化,请更改应用程序的刷新频率。

dmDisplayFrequency :Specifies the frequency, in hertz (cycles per second), of the display device in a particular mode. This value is also known as the display device's vertical refresh rate. Display drivers use this member. It is used, for example, in the ChangeDisplaySettings function. Printer drivers do not use this member.

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

如果需要确定window在哪个显示器上,可以使用MonitorFromWindow函数。

这是一个代码示例: