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

后端 未结 2 1975
花落未央
花落未央 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);
    }
    

提交回复
热议问题