How to listen to a resizing event of a nested Window within a process with C# and the user32.dll library

I have an application which has a nested window, the resizing event of which I need to listen to.
I tried the following code.

    class Win32WindowEvents
{
        [DllImport("user32.dll")]
        static extern IntPtr SetWinEventHook(uint eventMin, uint eventMax, IntPtr hmodWinEventProc, WinEventDelegate lpfnWinEventProc, uint idProcess, uint idThread, uint dwFlags);

        [DllImport("user32.dll")]
        static extern bool UnhookWinEvent(IntPtr hWinEventHook);

        private Win32Window Window_toListen;
        private IntPtr _resizeHook ;
        private IntPtr _reorderHook ;
        private WinEventDelegate _resizeProc;
        private WinEventDelegate _reorderProc;

        public Win32WindowEvents(Win32Window window)  
// the Win32Window is a class that has various fields about the nested children window
// it also has the hwnd handle which is then fed into the SetWinEventHook() function.
        {
            StopListening();
            _resizeProc = new WinEventDelegate(WinEventProc_Resizing);
            _reorderProc = new WinEventDelegate(WinEventProc_Reordering);

            Window_toListen = window;

            _resizeHook = SetWinEventHook((uint)EventTypes.EVENT_SYSTEM_MOVESIZEEND, (uint)EventTypes.EVENT_SYSTEM_MOVESIZEEND, window.hWnd,
                    _resizeProc, 0, 0, (uint)(WinHookParameter.OUTOFCONTEXT));
        }

        private void WinEventProc_Resizing(IntPtr hWinEventHook, uint eventType, IntPtr hwnd, int idObject, int idChild, uint dwEventThread, uint dwmsEventTime)
        {
            if (Math.Abs(timelogged - dwmsEventTime) < 1000) return;
            timelogged = dwmsEventTime;
            Resizing(Window_toListen.Process, Window_toListen, (EventTypes)eventType);
        }
        public void StopListening()
        {          
                UnhookWinEvent(_resizeHook);

        }

}

The hook only listen to the resize/move event of the application window, but not that of the children nested window.

I have the handle hwnd of this children nested window and I need to have an event which triggers, when this children nested window is resized. How to achieve that?

  • SetWinEventHook != SetWindowsHookExW Win Events are triggered due to a deliberate choice to inform you of something for accessibility reasons. It wouldn’t surprise me if support for nested windows was limited.

    – 

  • @JeremyLakeman thanks for the response! So should I use SetWindowsHookEx?

    – 

Leave a Comment