Gabriel De Freitas
Gabriel De Freitas

Reputation: 143

Getting mouse coordinates on mouse click

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);
    }
}

Upvotes: 5

Views: 19238

Answers (3)

Meisam Rasouli
Meisam Rasouli

Reputation: 321

You have probably defined the click event for the wrong object. If you use the click event for a panel, you will get the mouse coordinates relative to the upper-left corner of that panel. If you use the click event for another form control, you will get the mouse coordinates relative to the upper-left corner of that form control. The below code will give the mouse coordinates relative to the upper-left corner of formobject1, whatever it is.

            formobject1.Click += (sender, e) => { 
            MouseEventArgs e1 = (MouseEventArgs)e;
            System.Drawing.Point cc = e1.Location;
            MessageBox.Show(cc.ToString());
        };

Upvotes: 0

CharithJ
CharithJ

Reputation: 47490

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#

Upvotes: 1

Pierre-Luc Pineault
Pierre-Luc Pineault

Reputation: 9191

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();
    }
}

Upvotes: 6

Related Questions