Getting mouse coordinates on mouse click
Asked Answered
J

3

5

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);
    }
}
Jongjongleur answered 31/7, 2015 at 4:3 Comment(0)
C
6

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();
    }
}
Cleanthes answered 31/7, 2015 at 4:10 Comment(8)
I may have expressed myself incorrectly. But this gets the mouse position of me clicking the "OK" button on MessageBox. It doesn't let me pick any position of screen AFTER clicking OK button.Jongjongleur
But once you click the OK button, how do you want to wait for the user to pick a position? You can't use a timer because you don't know when he's ready. However, you can position the mouse somewhere and then use ENTER to close the pop-up tho, so basically "Pick a position before pressing OK (with Enter)". If that can't do it, what would you want the "Get the mouse position NOW" event to be?Cleanthes
I agree, this can work. So what I want is impossible? Because I've seen some software with this function. As I said, i have no idea how to make it. Maybe the OK button start a timer and it creates onClick function inside the timer? IdkJongjongleur
@GabrielDeFreitas I'll add an example with a timer, if that can help.Cleanthes
@GabrielDeFreitas If you want to get the position after a second click, anywhere on the screen, you need to use a Mouse Hook, using Windows' DLLs. An example can be found hereCleanthes
Alright thank you! But theres no way to get the coordinates only when I press left mouse button? (inside the timer) Like checking if left_mouse is pressed every 1ms of interval? ---- I see your last message now only. Ill check it out.Jongjongleur
I have no idea how to apply that to my code tho. Kinda advancedJongjongleur
this is like a timeout to get coorditante why if you cant reach the correct point?, any update to get coordinate of pointer on user click (left or righ) or press key like "F112?Thersathersites
U
1

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#

Udometer answered 31/7, 2015 at 4:17 Comment(3)
@GabrielDeFreitas:"User Click anywhere on screen" on your application or anywhere in the screen?Udometer
anywhere in the screen, not on my applicationJongjongleur
@GabrielDeFreitas: codeproject.com/Articles/7294/…Udometer
L
0

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());
        };
Lm answered 5/1, 2023 at 15:0 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.