Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting mouse coordinates on mouse click

Tags:

c#

mouse

I'm using this code below but it doesn't work like I want it and I have no idea how to actually make it.

What I want it to do is get the mouse coordinates onClick, but this happens after the user confirm a messagebox.

MessageBox > User Click OK > User Click anywhere on screen > Get the coordinates

Should I start a timer at the "OK button"? What I do on timer code to wait for a mouse response?

This is what I have now (which shows the mouse position when I click OK button):

private void button12_Click(object sender, EventArgs e)
{
    if (MessageBox.Show("Pick a position after clicking OK", "OK", MessageBoxButtons.OK, MessageBoxIcon.Exclamation) == DialogResult.OK)
    {
        // user clicked ok
        MouseEventArgs me = (MouseEventArgs)e;
        Point coordinates = me.Location;
        MessageBox.Show("Coordinates are: " + coordinates);
    }
}
like image 923
Gabriel De Freitas Avatar asked Jul 31 '15 04:07

Gabriel De Freitas


2 Answers

You were almost there. The problem is that the EventArgs will give you the position relative to the button at the time of the click.

If you want the cursor position instead of the click, you can use the Cursor class to get its Position property:

private void button12_Click(object sender, EventArgs e)
{
    if (MessageBox.Show("Pick a position after clicking OK", "OK", MessageBoxButtons.OK, MessageBoxIcon.Exclamation) == DialogResult.OK)
    {
        // user clicked ok
        Point coordinates = Cursor.Position;
        MessageBox.Show("Coordinates are: " + coordinates);
    }
}

To get the coordinates after the user closed the MessageBox, you can use a timer. In order to do so, you will have to declare one at the class level, set its Tick event and move your cursor login into it.

The button12_Click method will now start the timer, which will show the cursor position once it expires (In this example, after one second).

private Timer timer; //Declare the timer at class level
public Form1()
{
    InitializeComponent();
    // We set it to expire after one second, and link it to the method below
    timer = new Timer {Interval = 1000}; //Interval is the amount of time in millis before it fires
    timer.Tick += OnTick;
}

private void OnTick(object sender, EventArgs eventArgs)
{
    timer.Stop(); //Don't forget to stop the timer, or it'll continue to tick
    Point coordinates = Cursor.Position;
    MessageBox.Show("Coordinates are: " + coordinates);
}


private void button1_Click(object sender, EventArgs e)
{
    if (MessageBox.Show("Pick a position after clicking OK", "OK", MessageBoxButtons.OK, MessageBoxIcon.Exclamation) == DialogResult.OK)
    {
        timer.Start();
    }
}
like image 144
Pierre-Luc Pineault Avatar answered Oct 03 '22 23:10

Pierre-Luc Pineault


Cursor Position relative to the Screen

System.Windows.Forms.Cursor.Position

Cursor Position relative to a control

var relativePoint = myControl.PointToClient(Cursor.Position);

Global hooks are not supported in the .NET Framework. See Reference

If you want to handle global mouse click events have a look at this article.

Processing Global Mouse and Keyboard Hooks in C#

like image 21
CharithJ Avatar answered Oct 03 '22 23:10

CharithJ