Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

A better hook for checking new windows #6

Open
maraf opened this issue May 13, 2019 · 2 comments
Open

A better hook for checking new windows #6

maraf opened this issue May 13, 2019 · 2 comments

Comments

@maraf
Copy link
Member

maraf commented May 13, 2019

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

@maraf maraf changed the title A better to hook to new windows A better hook for checking new windows May 13, 2019
@maraf
Copy link
Member Author

maraf commented May 24, 2019

The event messages received are only those sent to the Shell window associated with the specified window's desktop. Many of the messages are the same as those that can be received after calling the SetWindowsHookEx function and specifying WH_SHELL for the hook type. The difference with RegisterShellHookWindow is that the messages are received through the specified window's WindowProc and not through a call back procedure.

https://docs.microsoft.com/en-us/windows/desktop/api/winuser/nf-winuser-setwindowshookexa

https://blogs.msdn.microsoft.com/calvin_hsia/2016/11/30/its-easy-to-use-windows-hooks-even-from-c/

maraf added a commit that referenced this issue May 27, 2019
@maraf
Copy link
Member Author

maraf commented Aug 15, 2023

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant