简体   繁体   中英

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

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

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#

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM