class ButtonNoFocus : Button
{
protected override void WndProc(ref Message m)
{
const int WM_MOUSEACTIVATE = 0x0021;
const int MA_NOACTIVATEANDEAT = 4;
if (m.Msg == WM_MOUSEACTIVATE)
{
m.Result = new IntPtr(MA_NOACTIVATEANDEAT);
DoMouseDown(new MouseEventArgs(MouseButtons.Left, 0, 0, 0, 0));
}
else
base.WndProc(ref m);
}
public new event MouseEventHandler MouseDown;
private void DoMouseDown(MouseEventArgs mouseEventArgs)
{
var e = MouseDown;
if (e != null)
MouseDown(this, mouseEventArgs);
}
}
This will prevent focus and raise a fake MouseDown event when clicked. Note: Click event will not fire as the control must take focus for the Click to happen; however, if want to capture the mouse and manually track this should work.
UPD: It is better to return MA_NOACTIVATE (3) as it will not bring focus to the control, but will still raise the usual MouseDown event.