A global hook only helps if you need to consume all available event. You can do it using P/Invoke and Windows Hooks. See
http://msdn.microsoft.com/en-us/library/ms632589(v=vs.85).aspx[
^].
If you want to write a system-global hook, you need to write a native DLL which installs the hook. I suggest you create C++ DLL or a mixed-mode DLL with C++/CLI.
However, the hooks would not help you if you want to trigger mouse inputs event. You need to simulate mouse input, use Windows API
SendInput
, see
http://msdn.microsoft.com/en-us/library/ms646310(v=vs.85).aspx[
^].
I must warn against using simulation of input in development of UI. The UI should not use such tricks: all functionality can be done using just .NET library. If you explained your ultimate goal and requirements, you could get a better advice in this direction.
Simulation of input should be used only if your really need "system" tricks in your application. Examples include running of keyboard and/or mouse macro, virtual keyboard and the like.
[EDIT — based on comment by OP]
I still don't understand how exactly simulation of the click can help to deal with blink. Probably you simply want to quickly re-use some code created previously without clear plan on what would be next. It looks like you're doing a momolythic application and have access to all your control. I think you don't need any hooks or even low-level simulation at all.
Most likely, a simple call to
Control.OnMouseClick
will do the trick. It will work if you just need to trigger the event. See
http://msdn.microsoft.com/en-us/library/system.windows.forms.control.onmouseclick.aspx[
^].
As this method is protected, you need to call it in derived control (sorry, I don't know what control type do you use).
You can simulate just the click or with separate
MouseUp
and
MouseDown
. It depends on which event do you handle. Something like this:
class MyClickingControl : Panel {
void SimulateClicks(int count, Point where, int clickDelay, int downDelay) {
MouseEventArgs eventArgs =
new MouseEventArgs(MouseButtons.Left, 1, where.X, where.Y, 0);
for (int index = 0; index < count; index++) {
this.OnMouseClick(eventArgs);
System.Threading.Thread.Sleep(clickDelay);
}
for (int index = 0; index < count; index++) {
this.OnMouseDown(eventArgs);
System.Threading.Thread.Sleep(downDelay);
this.OnMouseUp(eventArgs);
System.Threading.Thread.Sleep(clickDelay);
}
for (int index = 0; index < count; index++) {
this.OnMouseDown(eventArgs);
this.OnMouseClick(eventArgs);
System.Threading.Thread.Sleep(downDelay);
this.OnMouseUp(eventArgs);
System.Threading.Thread.Sleep(clickDelay);
}
}
}
[END EDIT]
—SA