Skip to content

A better hook for checking new windows #6

@maraf

Description

@maraf

Article:
https://www.codeproject.com/Tips/1057230/Windows-Resize-and-Move

Sources:
WindowsMover-src.zip

private readonly int windowMessage_ShellHook;
public WindowsHookForm()
{
    windowMessage_ShellHook = WindowsApi.RegisterWindowMessage("SHELLHOOK");
    WindowsApi.RegisterShellHookWindow(this.Handle);
}
public event Action<IntPtr> WindowCreatedEvent;  
public event Action<IntPtr> WindowActivatedEvent;
public event Action<IntPtr> WindowDestroyedEvent;

protected override void WndProc(ref Message message)
{
    if(message.Msg == windowMessage_ShellHook)
    {
        WindowsApi.ShellEvents shellEvent = (WindowsApi.ShellEvents)message.WParam.ToInt32();
        IntPtr windowHandle = message.LParam;

        switch(shellEvent)
        {
            case WindowsApi.ShellEvents.HSHELL_WINDOWCREATED:
                if(WindowCreatedEvent != null)
                    WindowCreatedEvent(windowHandle);
                break;
                
            case WindowsApi.ShellEvents.HSHELL_WINDOWACTIVATED:
                if(WindowActivatedEvent != null)
                    WindowActivatedEvent(windowHandle);
                break;

            case WindowsApi.ShellEvents.HSHELL_WINDOWDESTROYED:
                if(WindowDestroyedEvent != null)
                    WindowDestroyedEvent(windowHandle);
                break;
        }
    }
    base.WndProc(ref message);
}

Additional source:
https://docs.microsoft.com/en-us/windows/desktop/api/winuser/nf-winuser-registershellhookwindow

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions