Not that sure why this question got a bounty, the accepted answer ought to be already pretty close to a solution. Except that you ought to use MouseUp instead of MouseDown, your user typically expects a click action to take effect when he releases the button. Which provides the back-out "oops, didn't mean to click it, move the mouse so it gets ignored" option.
Nevertheless, for the built-in Winforms controls, like PictureBox, this is configurable with the Control.SetStyle() method. Add a new class to your project and paste the code shown below. Compile. Drop the new control from the top of the toolbox:
using System;
using System.Windows.Forms;
class MyPictureBox : PictureBox {
public MyPictureBox() {
this.SetStyle(ControlStyles.StandardDoubleClick, false);
}
}
Do beware however that this won't work for the .NET classes that wrap an existing native Windows control. Like TextBox, ListBox, TreeView, etc. The underlying configuration for that is the WNDCLASSEX.style member, CS_DBLCLKS style flag. The code that sets that style flag is baked into Windows and cannot be changed. You'd need different kind of surgery to turn a double-click back into a single click. You can do so by overriding the WndProc() method, I'll give an example for TextBox:
using System;
using System.Windows.Forms;
class MyTextBox : TextBox {
protected override void WndProc(ref Message m) {
// Change WM_LBUTTONDBLCLK to WM_LBUTTONCLICK
if (m.Msg == 0x203) m.Msg = 0x201;
base.WndProc(ref m);
}
}
Just change the class name if you want to do it for other controls. Commandeering Winforms to make it work the way you want never takes much code, just Petzold :)
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…