Daniel
Daniel

Reputation: 1124

How to prevent MouseUp event after DoubleClick on Window TitleBar in WPF?

If user double-clicks window title bar to maximize window, the window will maximize on MouseDown event of the second click, but then the MouseUp event on that same second click will be registered by one of the controls in the window that are now under the cursor and trigger something user didn't want. How can it be prevented?

Upvotes: 1

Views: 604

Answers (1)

l2rek
l2rek

Reputation: 32

This should work just fine:

Solution:

  //WNDPROC INTEROP
        private const int WM_NCLBUTTONDBLCLK = 0x00A3;

        protected override void OnSourceInitialized(EventArgs e)
        {
            base.OnSourceInitialized(e);
            HwndSource source = PresentationSource.FromVisual(this) as HwndSource;
            source.AddHook(WndProc);
        }

   private IntPtr WndProc(IntPtr hwnd, int msg, IntPtr wParam, IntPtr lParam, ref bool handled)
    {
        //TITLE BAR DOUBLE CLICK
        if (msg == WM_NCLBUTTONDBLCLK)
        {
            Console.WriteLine("titlebar double click  " + this.WindowState);

            //WINDOW ABOUT TO GET MAXIMIZED
            if (msg == WM_NCLBUTTONDBLCLK) 
                handleNextMouseup = true;

            else if (msg == 0x202) //  mouseup     
            {        
                if (handleNextMouseup) 
                    handled = true;      
                
                handleNextMouseup = false;     
            } 
        }

        return IntPtr.Zero;
    }

another possible solution:

  //WNDPROC INTEROP
        private const int WM_NCLBUTTONDBLCLK = 0x00A3;

        protected override void OnSourceInitialized(EventArgs e)
        {
            base.OnSourceInitialized(e);
            HwndSource source = PresentationSource.FromVisual(this) as HwndSource;
            source.AddHook(WndProc);
        }

        private IntPtr WndProc(IntPtr hwnd, int msg, IntPtr wParam, IntPtr lParam, ref bool handled)
        {
            //TITLE BAR DOUBLE CLICK
            if (msg == WM_NCLBUTTONDBLCLK)
            {
                Console.WriteLine("titlebar double click  " + this.WindowState);

                //WINDOW ABOUT TO GET MAXIMIZED
                if (this.WindowState == WindowState.Normal)
                {
                    fakehittestvisible = true;

                }
            }

            return IntPtr.Zero;
        }


        private bool fakehittestvisible = false;

        //ON PREVIEW MOUSE UP ABORT IF MIXIMIZING HAPPENED
        private void this_PreviewMouseUp(object sender, MouseButtonEventArgs e)
        {
            if (fakehittestvisible)
            {
                e.Handled = true;
                fakehittestvisible = false;
                return;
            }

            fakehittestvisible = false;
            Console.WriteLine("this mouse up state is " + this.WindowState);
        }

Upvotes: 1

Related Questions