如何在多监视器系统上检测最大化事件

时间:2013-07-16 05:03:17

标签: c++ c windows winapi

在Windows上,C / C ++当系统有多个监视器时,如何在WM_WINDOWPOSCHANGING消息上检测“最大化事件”?也就是说,当窗口在任何显示器上最大化时?

(也欢迎其他操作系统的代码!)

2 个答案:

答案 0 :(得分:1)

首先,您需要使用EnumDisplayDevicesEnumDisplaySettingsEx检测所有显示。

// C/C++ solution for enumerating display settings.

// Struct for display properties
struct DisplayProps
{
    int x, y;   // only display position is needed..
};

DISPLAY_DEVICE disp;
DISPLAY_DEVICE adapter;
DEVMODE mode;
DisplayProps the_displays[32];  // Array to store the results.
int i = 0, j = 0, display_count = 0;

// Initialize win32 structs.
memset(&disp, 0, sizeof(DISPLAY_DEVICE));
disp.cb = sizeof(DISPLAY_DEVICE);
memset(&args.adapter, 0, sizeof(DISPLAY_DEVICE));
adapter.cb = sizeof(DISPLAY_DEVICE);
memset(&mode, 0, sizeof(DEVMODE));
mode.dmSize = sizeof(DEVMODE);

memset(the_displays, 0, sizeof(the_displays));

// adapter loop:
while(EnumDisplayDevices(NULL, i, &adapter, 0))
{   // multiple displays on single adapter loop:
    j = 0;
    while(EnumDisplayDevices((TCHAR*)&adapter.DeviceName, j, &disp, 0))
    {   
        // The device must be active, attached to desktop and not a mirroring device.
        if((disp.StateFlags & DISPLAY_DEVICE_ACTIVE)
            && (disp.StateFlags & DISPLAY_DEVICE_ATTACHED_TO_DESKTOP)
            && !(disp.StateFlags & DISPLAY_DEVICE_MIRRORING_DRIVER))
        {
            // read display settings.
            if(EnumDisplaySettingsEx((TCHAR*)&adapter.DeviceName, ENUM_CURRENT_SETTINGS, &mode, 0))
            {
                the_displays[display_count].x = mode.dmPosition.x;
                the_displays[display_count].y = mode.dmPosition.y;
                ++display_count;
            }

        }
        // Reset
        memset(&args.disp, 0, sizeof(DISPLAY_DEVICE));
        disp.cb = sizeof(DISPLAY_DEVICE);
        ++j;
    }
    // Reset
    memset(&args.adapter, 0, sizeof(DISPLAY_DEVICE));
    adapter.cb = sizeof(DISPLAY_DEVICE);
    ++i;
}

然后在窗口过程中的WM_WINDOWPOSCHANGING消息中,我们需要实际检测事件。这需要一个可能未记录的值SWP_STATECHANGED(0x8000)并使用AdjustWindowRectEx

WINDOWPOS * wp = (WINDOWPOS*)lParam;
if( (wp->flags & (SWP_STATECHANGED | SWP_FRAMECHANGED)) != 0)
{
    // Figuring next part was a bit of luck involved: I noticed that AdjustWindowRectEx rect.left
    // matches window position (x and y) on default display when window is maximized
    RECT styleadj = {0, 0, 0, 0};
    // Can't cache/save this because user can change the theme.
    AdjustWindowRectEx(&styleadj,
    WS_OVERLAPPED | WS_CAPTION | WS_SYSMENU | WS_MINIMIZEBOX, /* your window dwStyle */
    false, /* not using menu? */
    WS_EX_APPWINDOW | WS_EX_WINDOWEDGE); /* your window dwExStyle */ 

    for(int i = 0; i < the_display_count; ++i)
    {
        if((wp->x == the_displays[i].x + styleadj.left) && (wp->y == the_displays[i].y + styleadj.left))
        {
            // Event is detected, do what ever you want!
            MessageBox(NULL, "A Maximize event Detected.", "Event Detected!", MB_OK);
        }
    }
}

确认此代码适用于多个显示器(我有两个)以及Windows 7 Aero Glass主题,Aero Basic主题和旧的win 2000主题。 WinXp和8未经测试。 AdjustWindowRectEx确实会在不同的主题上返回不同的值。

答案 1 :(得分:1)

当窗口要最大化时,尤其是作为交互式用户操作的结果,它会收到WM_SYSCOMMAND SC_MAXIMIZE消息。

最大化过程通常涉及WM_GETMINMAXINFO消息,处理哪个......

  

应用程序可以使用此消息覆盖窗口的默认最大化大小和位置,或其默认的最小或最大跟踪大小。

要检测当前监视工作区坐标,最简单的可能是MonitorFromRect API:

HMONITOR hMonitor = MonitorFromRect(&rcCenter, MONITOR_DEFAULTTONEAREST);
if(hMonitor)
{
    MONITORINFO MonitorInfo = { sizeof MonitorInfo };
    GetMonitorInfo(hMonitor, &MonitorInfo);
    // NOTE: MonitorInfo.rcWork is what you look for