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

后端 未结 2 1968
花落未央
花落未央 2020-12-09 17:21

I am trying to create a WPF window with WindowStyle=\"None\" (for custom buttons and no title) that cannot be resized. Setting ResizeMode to

相关标签:
2条回答
  • 2020-12-09 17:45

    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);
    }
    
    0 讨论(0)
  • 2020-12-09 17:55

    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... :-)

    0 讨论(0)
提交回复
热议问题