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 :)