Hooking into Windows message loop in WPF window adds white border on the inside Hooking into Windows message loop in WPF window adds white border on the inside wpf wpf

Hooking into Windows message loop in WPF window adds white border on the inside


When you add your hook, you should only handle the messages you need to, and ignore the others. I believe you are handling certain messages twice, since you call DefWindowProc, but never set the handled parameter to true.

So in your case, you'd use:

private IntPtr WndProc(IntPtr hwnd, int msg, IntPtr wParam, IntPtr lParam, ref bool handled) {    if (msg == (uint)WM.NCHITTEST) {        handled = true;        var htLocation = DefWindowProc(hwnd, msg, wParam, lParam).ToInt32();        switch (htLocation) {            case (int)HitTestResult.HTBOTTOM:            case (int)HitTestResult.HTBOTTOMLEFT:            case (int)HitTestResult.HTBOTTOMRIGHT:            case (int)HitTestResult.HTLEFT:            case (int)HitTestResult.HTRIGHT:            case (int)HitTestResult.HTTOP:            case (int)HitTestResult.HTTOPLEFT:            case (int)HitTestResult.HTTOPRIGHT:                htLocation = (int)HitTestResult.HTBORDER;                break;        }        return new IntPtr(htLocation);    }    return IntPtr.Zero;}

Also, I'd probably add the hook in an OnSourceInitialized override, like so:

protected override void OnSourceInitialized(EventArgs e) {    base.OnSourceInitialized(e);    IntPtr mainWindowPtr = new WindowInteropHelper(this).Handle;    HwndSource mainWindowSrc = HwndSource.FromHwnd(mainWindowPtr);    mainWindowSrc.AddHook(WndProc);}


You can try from anywhere in a WPF App

                ComponentDispatcher.ThreadFilterMessage += new ThreadMessageEventHandler(ComponentDispatcherThreadFilterMessage);

and:

    // ******************************************************************    private static void ComponentDispatcherThreadFilterMessage(ref MSG msg, ref bool handled)    {        if (!handled)        {            if (msg.message == WmHotKey)            {                HotKey hotKey;                if (_dictHotKeyToCalBackProc.TryGetValue((int)msg.wParam, out hotKey))                {                    if (hotKey.Action != null)                    {                        hotKey.Action.Invoke(hotKey);                    }                    handled = true;                }            }        }    }

Hope it helps... :-)