How to block double click in overridden WndProc function in Windows Forms?

后端 未结 5 1702
名媛妹妹
名媛妹妹 2021-01-13 20:00

I have a form created in Windows Forms which is draggable wherever I click. I made it by overriding WndProc function which in turn modifies each click as it was a title bar

相关标签:
5条回答
  • 2021-01-13 20:19

    I was having the same problem today in C++. I used JaredPar's solution but with WM_NCLBUTTONDBLCLK (0x00A3) instead of WM_LBUTTONDBLCLK, that did the trick for me! It's working because the double click message is being sent from a non-client (NC) area, which is the "virtual" title bar (HTCAPTION) in this case.

    0 讨论(0)
  • 2021-01-13 20:28

    It seems you found a solution to a problem with caused another problem that you're trying to solve. If I could suggest something simple, just a better solution to make a window drag-able:

    Add InteropServices to the using declarations:

    using System.Runtime.InteropServices;
    

    And for the code:

        public const int WM_NCLBUTTONDOWN = 0xA1;
        public const int HT_CAPTION = 0x2;
    
        [DllImportAttribute("user32.dll")]
        public static extern int SendMessage(IntPtr hWnd,
                         int Msg, int wParam, int lParam);
        [DllImportAttribute("user32.dll")]
        public static extern bool ReleaseCapture();
    

    Then go to the form's MouseDown event and paste this:

            if (e.Button == MouseButtons.Left)
            {
                ReleaseCapture();
                SendMessage(Handle, WM_NCLBUTTONDOWN, HT_CAPTION, 0);
            }
    

    Done.

    0 讨论(0)
  • If you just want to stop the double click from having it's default behavior in the window for which you've overridden the WndProc then intercept the WM_LBUTTONDBLCLK message

    private const int WM_LBUTTONDBLCLK = 0x0203;
    
    ...
    
    protected override void WndProc(ref Message message) {
      if (message.Msg == WM_LBUTTONDBLCLK) {
        return;
      }
    
      base.WndProc(ref message);
    
      if (message.Msg == WM_NCHITTEST && (int)message.Result == HTCLIENT)
        message.Result = (IntPtr)HTCAPTION;
    }
    
    0 讨论(0)
  • 2021-01-13 20:34

    In addition to JaredPar I would suggest don not create draggable form in that way, but handle it in 3 steps

    • identify mouse down on the form
    • capture mouse
    • identify mouse up event

    It is not a complicated to handle, and it's better, imo, then disabling a double click on the form.

    For complete example of how you can do that can have a look on

    Creating a Draggable Borderless Form

    0 讨论(0)
  • 2021-01-13 20:42

    I've done the same as Jex which is working great.

    private const int WM_NCHITTEST = 0x84;
    private const int HTCLIENT = 0x1;
    private const int HTCAPTION = 0x2;
    private const int WM_LBUTTONDBLCLK = 0x00A3;
    
    protected override void WndProc(ref Message m)
        {
            if (m.Msg == WM_LBUTTONDBLCLK)
            {
                return;
            }
            switch (m.Msg)
            {
    
                case WM_NCHITTEST:      
                    base.WndProc(ref m);
                    if ((int)m.Result == HTCLIENT)
                        m.Result = (IntPtr)HTCAPTION;
                    return;
            }
            base.WndProc(ref m);
        }
    
    0 讨论(0)
提交回复
热议问题