Share via

Capturing an unpressed mouse

Heiko 1,311 Reputation points
2022-07-25T16:03:56.557+00:00

I want to let the user choose a color from any point on the screen. The user can choose this option from a menu item. However, this means that the mouse is not pressed down while he is looking for the color point. If the user then clicks inside my application, the PreviewMouseLeftButtonDown event is fired and I can determine the color. However, if he clicks outside my application, I only get the LostMouseCapture event. Sometimes I also get the LostMouseCapture event immediately after selecting the menu item. Possibly because my application window is very small and the mouse pointer is outside the application after selecting the menu item. How can I get the coordinates of the mouse click outside my application?

private void menuItemChooseBackground_Click(object sender, RoutedEventArgs e)  
{  
	Cursor = Cursors.Cross;  
  
	if (!CaptureMouse())  
		MessageBox.Show("false");  
}  
  
private void MainWindow_PreviewMouseLeftButtonDown(object sender, MouseButtonEventArgs e)  
{  
	if (!IsMouseCaptured)  
		return;  
  
	e.Handled = true;  
	Cursor = Cursors.Arrow;  
  
	Point	pt = NativeInterop.GetMousePosition();  
	Color	color = NativeInterop.GetPixelColor((int)pt.X, (int)pt.Y);  
  
	ReleaseMouseCapture();  
	MessageBox.Show("1: " + pt.X + " @ " + pt.Y + " : " + color.ToString());  
}  
  
private void MainWindow_LostMouseCapture(object sender, MouseEventArgs e)  
{  
	Cursor = Cursors.Arrow;  
	MessageBox.Show("Lost capture.");  
}  
Developer technologies | Windows Presentation Foundation
Developer technologies | C#
Developer technologies | C#

An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.

0 comments No comments

Answer accepted by question author

Viorel 127K Reputation points
2022-07-26T08:19:53.98+00:00

According to some documentation, capturing only works if the left mouse button is pressed. Try creating a new window, then handle two events:

private void menuItemChooseBackground_PreviewMouseDown( object sender, MouseButtonEventArgs e )  
{  
    Cursor = Cursors.Cross;  
    Mouse.Capture( this );  
}  
  
private void Window_MouseUp( object sender, MouseButtonEventArgs e )  
{  
    Mouse.Capture( null );  
    Cursor = Cursors.Arrow;  
  
    var position = e.MouseDevice.GetPosition( this );  
    position = PointToScreen( position );  
  
    // a test: showing position  
    Title = position.ToString( );  
}  

Therefore, in this approach, the mouse must be pressed and dragged.

Or maybe you can create semi-transparent top-level windows that occupies the whole screens, without capturing. After clicking the mouse, hide the windows and get the colour.

Was this answer helpful?


1 additional answer

Sort by: Most helpful
  1. Castorix31 91,876 Reputation points
    2022-07-25T16:34:45.483+00:00

    How can I get the coordinates of the mouse click outside my application?

    You can use a WH_MOUSE_LL hook.
    (like one of the samples I had posted : Cursor Coordinates Tracking in C#)

    Was this answer helpful?


Your answer

Answers can be marked as 'Accepted' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.