Click here to Skip to main content
15,889,992 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
I want to create a windows form that when i click on a button it show mouse position and type of left click or right click in a label.I use this code:
C#
this.Cursor = new Cursor(Cursor.Current.Handle);
label1.Text = Cursor.Position.X;
label1.Text+= Cursor.Position.Y;


but this show mouse position only from application form not anywhere,how to change it that return mouse position from desktop no a form?

i view this article:
Get mouse position from desktop not form[^]
but it is hard to me.i am begginer to c#.
thank you in advance.

What I have tried:

return mouse position from desktop not only form.
Posted
Updated 10-May-16 9:56am
v2
Comments
Sergey Alexandrovich Kryukov 10-May-16 8:48am    
Just re-calculate mouse position; look at the form members; you have everything to do so. Why, by the way?
But if you also need to handle mouse events when a cursor is out of the form, it's harder. Do you need it?
—SA
NewWebDesigner 10-May-16 9:11am    
yes,i want to capture mouse clicks.in step one i save it and in step 2 i show where mouse clicked.i update my question.
CHill60 10-May-16 9:15am    
That link just comes back to this question
NewWebDesigner 10-May-16 9:22am    
can you help me what i start search and learn about it.first simple code then advance.
Kornfeld Eliyahu Peter 10-May-16 9:35am    
Are you looking for this?
https://msdn.microsoft.com/en-us/library/system.windows.forms.control.pointtoscreen(v=vs.110).aspx

 
Share this answer
 
There are frequent uses for the following code strategies:

(code here assumes you execute it inside a MouseDown or MouseUp EventHandler)

1. the e.Location and e.X and e.Y parameters supplied to the MouseDown and MouseUp Events always give you the current location of the mouse-down/up and the top,left location of the Control clicked on ...

in co-ordinates relative to the control (or Form) that contains the control.

2. to find out where the mousedown/up occurred in terms of the desktop co-ordinate system:

Point screenLoc = this.PointToScreen(e.Location);

where 'this is the current control/form context you work with.

3. assuming a control may be nested inside another Control, and you want to know where the click occurred in the Form (the outermost container) that contains it:

Point formLoc = this.TopLevelControl.PointToClient(this.PointToScreen(e.Location));

TopLevelControl ... see: [^].
 
Share this answer
 
You're close. In your form, create a MouseUp Event. Then add some code to the event.

C#
private void Form1_MouseUp(object sender, MouseEventArgs e)
{
    label1.Text = (e.X + Location.X) + ", " + (e.Y + Location.Y) + " ClickType:" + e.Button;
}


In the code, MouseEventArgs e is an object with the position of your mouse inside your form.
MouseEventArgs also knows the type of mouse click via the e.Button.
Location or this.Location refers to the Form class that you're in. So Location.X will be the X location of the form relative to the screen you're looking at. Same with Location.Y

So the whole program

C#
using System.Windows.Forms;

namespace MousePosition
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_MouseUp(object sender, MouseEventArgs e)
        {
            label1.Text = (e.X + Location.X) + ", " + (e.Y + Location.Y) + " ClickType:" + e.Button;
        }
    }
}


Have fun coding!

Hogan
 
Share this answer
 
v2
Take a look at this CodeProject article:
Global Mouse and Keyboard Library[^]
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900