How does a mouse click end with a WinForms event?

When we press the mouse button and the MouseClick event is fired in our .NET program, how does the computer know that we clicked on the right point within the borders of the buttons?

I mean, what is happening under the hood and why do not we need to implement this?

+6
source share
3 answers

Basically, that for a Windows user interface infrastructure called User32 . See Also MSDN: Windows and Messages .

This framework works by allowing your application to implement a “message pump” that receives messages from Windows. The message may be “mouse just moved over this window” or “mouse button pressed”. Note that a “window” is more than just a “window” in the sense of form; be sure to read the MSDN link. The button also represents a window.

So:

  • The mouse is connected to the PC via the hardware bus.
  • User moves mouse
  • Hardware signals switch to software signals
  • Windows converts these signals to messages
  • Windows sends these messages to the appropriate application message pump
  • The app does whatever it wants with these messages.

Now .NET WinForms wraps this message for you and provides messages as events that you can subscribe to. You can see it in action overriding WndProc() to your control of the ring :

 protected override void WndProc(ref Message m) { Console.WriteLine(m); base.WndProc(ref m); } 

In more detail (from MSDN: About messages and message queues ), a Windows message is associated with a window (or "control" in WinForms terms) using a handle. This handle uniquely identifies the control.

Thus, WinForms knows that the mouse message message with the X descriptor is designed to control Y with this descriptor, so it raises MouseClick or the event associated with it after receiving this message.

+3
source

Tracking the position and state of the mouse is processed by the operating system itself, there are a whole bunch of input flags that are sent to applications when the mouse is clicked / pressed / etc. The same WM_LBUTTONDOWN and WM_LBUTTONUP event windows that you would use in a C ++ or C ( See Here ) application are intercepted by the .Net framework and converted to events that we all know and love. All this is processed by the card itself, by the specifics of exactly what it does to transfer them to “this particular button, clicked” in an effective way, I don’t know, but at the end of the day it does not matter.

You do not need to implement it, because the developers of the .NET Framework and its interface elements realized that everyone would need to implement this functionality for the button, and it happened for us as well.

+3
source

Windows uses the messaging model. The operating system communicates with your application window, passing messages to it. A message is simply a digital code that indicates a specific event. For example, if the user presses the left mouse button, the window receives a message with the following message code.

Some messages have data associated with them. For example, the WM_LBUTTONDOWN message includes the x coordinate and y coordinate of the mouse cursor.

To send a message to a window, the operating system calls the window procedure registered for that window. (And now you know what the window procedure is for.), For the Details mode, you can read this article, Messages in the window

+3
source

All Articles