简体   繁体   English

SetWinEventHook窗口最大化事件

[英]SetWinEventHook Window Maximized event

I am currently working on a program that uses a functionality that should alert me when an other process's window is maximizing/maximized. 我目前正在开发一个程序,该程序使用的功能应该在其他进程的窗口最大化/最大化时提醒我。 With the maximize event I mean pressing the symbols next to the close button on the top right corner. 对于最大化事件,我的意思是按下右上角关闭按钮旁边的符号。

To accomplish this I use the SetWinEventHook function. 为此,我使用SetWinEventHook函数。 The problem is that I can't find the correct event code to catch this event. 问题是我无法找到正确的事件代码来捕获此事件。 I tried the EVENT_SYSTEM_MOVESIZESTART, EVENT_SYSTEM_MOVESIZEEND, EVENT_SYSTEM_MINIMIZESTART and EVENT_SYSTEM_MINIMIZEEND constants but they all don't seem to trigger on the maximize event. 我尝试了EVENT_SYSTEM_MOVESIZESTART, EVENT_SYSTEM_MOVESIZEEND, EVENT_SYSTEM_MINIMIZESTART and EVENT_SYSTEM_MINIMIZEEND常量,但它们似乎都没有触发maxim事件。 I however can trace other events so my implementation of SetWinEventHook is working. 但是我可以跟踪其他事件,因此我的SetWinEventHook实现正在运行。

Does anyone maby has an idea on how to capture the maximize event from an other process? maby有没有想过如何从其他进程捕获最大化事件?

Thanks in advance. 提前致谢。

With friendly greetings, Bob 亲切的问候,鲍勃

Code example: 代码示例:

// To catch the event
SetWinEventHook(EVENT_MIN, EVENT_MAX, IntPtr.Zero, new WinEventDelegate(WinEventProc), GetProcess(), 0, 0);

// The handler
private void WinEventProc(IntPtr hWinEventHook, uint eventType, IntPtr hwnd, int idObject, int idChild, uint dwEventThread, uint dwmsEventTime)
{
    // TODO: Filter maximize event here   
    if (eventType == ?)
    {
            // Do something
    }   
}

In SetWinEventHook() 's callback, handle the EVENT_OBJECT_LOCATIONCHANGE event and check if the window is in maximized state by calling the GetWindowPlacement() function and comparing the showCmd property of its second argument with the SW_SHOWMAXIMIZED constant. SetWinEventHook()的回调中,处理EVENT_OBJECT_LOCATIONCHANGE事件并通过调用GetWindowPlacement()函数并将其第二个参数的showCmd属性与SW_SHOWMAXIMIZED常量进行比较来检查窗口是否处于最大化状态。

C++ example: C ++示例:

void CALLBACK exampleHook(HWINEVENTHOOK hook, DWORD event, HWND hWnd,
    LONG idObject, LONG idChild, DWORD dwEventThread, DWORD dwmsEventTime)
{
    if (EVENT_OBJECT_LOCATIONCHANGE == event) {
        WINDOWPLACEMENT wp;
        wp.length = sizeof(WINDOWPLACEMENT);
        GetWindowPlacement(hWnd, &wp);

        if (SW_SHOWMAXIMIZED == wp.showCmd) {
            // Window is maximized.
        }
    }
}

Fwiw, I used this approach in my ExplorerHiDpiFix utility. Fwiw,我在ExplorerHiDpiFix实用程序中使用了这种方法。

Windows doesn't fire a maximize event. Windows不会触发最大化事件。 There are other ways, but they're pretty nasty, and I wouldn't recommend them. 还有其他方法,但它们非常讨厌,我不推荐它们。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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