Click here to Skip to main content
15,557,873 members
Please Sign up or sign in to vote.
1.80/5 (3 votes)
See more:
Hello to all those interested in programming :

How can i do get x,y of mouse When the mouse clicked anywhere on the page(desktop) or display its


Please help me now I do not worked with Hook
Posted
Comments
Sergey Alexandrovich Kryukov 20-Sep-13 19:13pm    
What is the UI library or application type you are using? You need to tag it when asking questions in UI.
—SA
Sergey Alexandrovich Kryukov 20-Sep-13 19:51pm    
And this is not related to the hooks.
—SA

Please see my comment to the question. The answer can be different depending on the UI library, but ideas are the same.

This information is passed in the even arguments of the mouse events, such as a click event. It may look like this:
C#
myControlOrFrameworkElement.Click += (sender, eventArgs) => {
    Point clickLocation = new Point(eventArgs.X, eventArgs.Y);
    ClickHandler(eventArgs.Button, clickLocation);
};

You will be able to find the relevant types and help on them in the documentation of the UI library you are using.

[EDIT]

As OP wanted more detailed code and clarified the UI library used, I'll explain it to the very end:

C#
using System.Windows.Forms;
using Point = System.Drawing.Point; // see below
//...

partial public class MyForm : Form {

   Panel myPanel = new Panel();


   public MyForm() { // constructor
       //...
       // suppose you have some control you want to click on (let's say, Panel)
       // usually, people generate this code with the designer, 
       // but it is called from this point anyway:
       // you set properties of myPanel and then:
       this.Controls.Add(myPanel);
       // add event handler to the Invocation List of the event instance myPanel.Click:
       myPanel.Click += (sender, eventArgs) => {
           Point clickLocation = new Point(eventArgs.X, eventArgs.Y);
           ClickHandler(eventArgs.Button, clickLocation);
       }; myPanel.Click
   } // MyWindow

   //where:
   void ClickHander(MouseButtons whatIsClicked, Point whereClicked) {
       // for example:
       MessageBox.Show(
           string.Format("Clicked: {0} at ({1},{2})", whatIsClicked, whereClicked);
           this.Text);
   } //ClickHander

} //class MyForm


I hope now it's clear. In principle, you can use just one file with this class, only add the entry point Main. It should be the same as you already have when you create a Forms project from scratch; will see it in auto-generated code:
C#
class Program {

    static void Main() {
        //...
        Application.Start(new MyForm());
    } //Main

} //class Program


—SA
 
Share this answer
 
v3
Comments
amirpooya 20-Sep-13 19:34pm    
Please explain more...
I did not Understand I Should use yours sorce in Which event ?
Sergey Alexandrovich Kryukov 20-Sep-13 19:51pm    
1) Look at my comment to the question. You did not provide the details to tell you what event. 2) but the name of event (member) is "Click"; isn't it obvious from my code sample?
What exactly you don't understand? Are you sure you are familiar with events, event handlers (delegate type members), and actually handling of the events. Or do you usually use the designer not understanding how it works? If you knew all that you would not have your questions. So, do you want me to explain something? What?
—SA
amirpooya 20-Sep-13 19:58pm    
I am thank you very much but if you put an Sample application
I understand there because my English is not very good
Sergey Alexandrovich Kryukov 20-Sep-13 20:10pm    
Sorry, you did not yet answer my question. Or you don't care what application type to use?
—SA
amirpooya 20-Sep-13 20:17pm    
What was Your order this Question :(
application type ؟!
If you want to detect a mouse-click outside your WinForm application's windows (Forms), for example: on the Desktop (which you mention specifically):

You must use a Global Event Hook. There are many resources on CodeProject for implementing Global Mouse Hooks; the "classic one," updated many times over years, is by George Mamaladze: [^].

Within your WinForm application's windows (Forms):

1. every object that has a visual representation (technically, a DisplayRectange, and a ClientRectangle ... in other words a form of a window) in the UI at run-time (Control, UserControl, the Form itself) can implement a Click EventHandler, or EventHandlers for any of the various MouseDown/MouseUp/MouseMove Events.

If the mouse is clicked inside the bounds of A Form, or Control/UserControl contained within the Form, and there is a mouse-related Event "wired-up" to an EventHandler you have defined: that(those) EventHandler(s) will have the code you wrote executed.

2. In a Click EventHandler you do not get information about where the mouse was clicked in the EventArgs structure the handler receives; in the MouseDown/Up/Move events, however, you do get information in the MouseEventArgs structure passed to the EventHandler.

3. When you have a MouseEventArgs instance (in the variable always named 'e) in an EventHandler for Mouse/Down/Up/Move, the information about where the mouse went down is contained in: e.Location, e.X, and e.Y:

4. Those co-ordinates are always the co-ordinates of the mouse Event in the co-ordinate system of the Control, or Form, in whose visible area you performed the Click or other other mouse event; so: location 0,0 always means the upper-left corner of the Control or Form's client rectangle. In other words, the co-ordinates are always relative to the Control or Form's location: they are not screen co-ordinates.

5. The Location co-ordinates of a Control or a Form are always relative to their container: for a Form, the container is the screen itself (unless you have made the terrible mistake of putting a Form inside another Form or Control).

For a Control/UserControl: the container could be a Form, or another Control (you can have nested Controls).

6. If you want to know the screen co-ordinates of where a mouse Event took place:

a. you can use the MousePosition method available "everywhere" (on all Forms, Controls), in all Events, to get the screen co-ordinates of where the Mouse is at the current moment [^].

b. other methods to take a position in the co-ordinate space of a Control, or Form, and translate it to the co-ordinate space of the containing Form, or other Control, or into screen co-ordinates are made available in WinForms by the PointToScreen and PointToClient methods which can be used stand-alone or can be used as a method all Controls [^].

I suggest you try the following experiment:

1. create a Windows Forms application.

2. put a single Panel on the main Form, 'panel1

3. "wire-up" the MouseDown EventHandler for 'panel1, and use this code
private void panel1_MouseDown(object sender, MouseEventArgs e)
{
    Point formLocation = this.Location;

    Point panelLocationInForm = panel1.Location;

    Point mouseDownLocationInPanel = e.Location;

    // ?
    Point Point0 = MousePosition;

    // ?
    Point Point1 = panel1.PointToScreen(e.Location);

    // ?
    Point Point2= this.PointToScreen(e.Location);
}
Now, put a breakpoint in the code on the closing open-brace of the MouseDown EventHandler, and examine the values in the different variables:

When you can understand the principles involved, and predict what those values will be in each case: you will understand mouse location in general, and you'll be ready to understand PointToClient.

Hard work by you, at this point in time to master the fundamental aspects of position, and Event "location" in WinForms will enable you to solve any type of problem in the future :)
 
Share this answer
 
v2
Comments
amirpooya 21-Sep-13 23:22pm    
hi BillWoodruff :)
thank you very much dear friend for your perfect answer
thanh all who have helped this question(:

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