As part of some functionality I require in a UserControl I need to capture mouse messages. Capturing part of the screen that is outside of the application. To start the capture initially I had a simple button and it all worked fine.The SetHook works and I can see the WM_xxxx messages and handle them accordingly.
private void Capture_OnClick(object sender, RoutedEventArgs e)
{
getLeftClicks = _getLeftClicks.First;
_hookID = SetHook(_proc);
}
Now I use clicking on an image to start the capture process but I have to insert a Thread.Sleep statement into the handler for it to work.
private void Capture_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
{
if (_hookID != null)
UnhookWindowsHookEx(_hookID);
getLeftClicks = _getLeftClicks.First;
Thread.Sleep(200); // need to wait for this mousedown to finish before trying to hook the mouse
_hookID = SetHook(_proc);
}
What I would like to know is there some Windows property I can examine to see if everything has finished before setting my hook in lieu of the arbitrary 200ms figure I have found that works. If needed I will post more code but the guts of the problem lies here.
Thanks, Jim
Try using the MouseLeftButtonUp event instead of MouseLeftButtonDown.