How to block double click in overridden WndProc function in Windows Forms?
I have a form created in Windows Forms that drag and drop wherever I click. I did this by overriding the WndProc function, which in turn changes every click as it was a click in the title bar:
//found at: http://stackoverflow.com/questions/3995009/how-to-make-a-window-draggablec-winforms
private const int WM_NCHITTEST = 0x84;
private const int HTCLIENT = 0x1;
private const int HTCAPTION = 0x2;
///
/// Handling the window messages
///
protected override void WndProc(ref Message message)
{
base.WndProc(ref message);
if (message.Msg == WM_NCHITTEST && (int)message.Result == HTCLIENT)
message.Result = (IntPtr)HTCAPTION;
}
The problem is that now when I double click the window becomes full screen, which is not desirable. How can I block this behavior?
source to share
In addition to JaredPar, I would suggest not to create the dragged shape this way, but to handle it in 3 steps
- identify the mouse on the form
- grab the mouse
- define a mouse up event
It's not hard to handle and it's better imo then disable double clicking on the form.
A complete example of how you can do this might look like
source to share
I had the same problem today in C ++. I used JaredPar's solution but with WM_NCLBUTTONDBLCLK (0x00A3) instead of WM_LBUTTONDBLCLK, which did the trick for me! This works because the double-click message is sent from the non-client area (NC), which is the "virtual" title bar (HTCAPTION) in this case.
source to share
I did the same as Jex which works 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);
}
source to share
You seem to have found a solution to the problem by causing another problem that you are trying to solve. If I could suggest something simple, just a better solution for creating a drag-and-drop window:
Add InteropServices to usage 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 MouseDown form and paste this:
if (e.Button == MouseButtons.Left)
{
ReleaseCapture();
SendMessage(Handle, WM_NCLBUTTONDOWN, HT_CAPTION, 0);
}
Done.
source to share
If you just want to stop the default double-click on the window you overridden WndProc
, then intercept the messageWM_LBUTTONDBLCLK
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;
}
source to share