Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to check if the mouse moves, while the left mouse button is down, in c#

Tags:

c#

wpf

mouse

I need to find a way to check if the mouse moves in c#, while the left button is down.

like image 933
user1464962 Avatar asked Jun 23 '12 16:06

user1464962


People also ask

Why is my mouse left click not working?

On Windows 10, head to Settings > Devices > Mouse. Under “Select your primary button,” ensure the option is set to “Left.” On Windows 7, head to Control Panel > Hardware and Sound > Mouse and ensure “Switch primary and secondary buttons” isn't checked. The ClickLock feature can also cause strange issues.

When you trigger a mouse What value does it return?

The button property returns a number that indicates which mouse button was pressed when a mouse event was triggered. This property is mostly used together with the onmousedown event. Note: This property is read-only.

How to determine whether the left mouse button is pressed?

The following example shows how to determine whether the left mouse button is pressed by checking if the state of the LeftButton is equal to the MouseButtonState enumeration value Pressed. If the button is pressed, a method is called which updates display elements in the sample.

How to detect a mouse event when the mouse moves?

We can detect a mouse event when the mouse moves over any component such as a label by using the mouseEntered () method and can be exited by using mouseExited () method of MouseAdapter class or MouseListener interface.

How do I fix the mouse-move message?

For the mouse-move message, check whether the left mouse button is down. If it is, recalculate the ellipse and repaint the window. In Direct2D, an ellipse is defined by the center point and x- and y-radii.

How do I change the position of the mouse on ellipse?

Store the position of the mouse click in the ptMouse variable. This position defines the upper left corner of the bounding box for the ellipse. Reset the ellipse structure. Call InvalidateRect. This function forces the window to be repainted. For the mouse-move message, check whether the left mouse button is down.


1 Answers

Here's an example (WPF):

public MainWindow()
{
    InitializeComponent();
    this.MouseMove += new MouseEventHandler(MainWindow_MouseMove);
}

void MainWindow_MouseMove(object sender, MouseEventArgs e)
{
    if (e.LeftButton == MouseButtonState.Pressed)
    {
        //do something
    }
}
like image 142
Tim S. Avatar answered Oct 16 '22 17:10

Tim S.