Lyrk
Lyrk

Reputation: 2000

How does clicking a mouse button end up in a WinForms event?

When we click mouse and an a MouseClick event fires in our .NET program, how does the computer know we clicked on the right point in button boundaries?

I mean what is going on under the hood and why don't we need to implement that?

Upvotes: 6

Views: 749

Answers (3)

CodeCaster
CodeCaster

Reputation: 151594

Basically that's what the Windows UI framework named User32 does for you. See also MSDN: Windows and Messages.

This framework works by letting your application implement a "message pump" which receives messages from Windows. A message can be "the mouse just moved over this window" or "the mouse button was pressed". Note that a "window" is more than just a "window" in the sense of a form, be sure to read the MSDN link. A button also is a window.

So:

  • A mouse is connected to the PC over a hardware bus
  • User moves the mouse
  • Hardware signals get translated to software signals
  • Windows translates these signals to messages
  • Windows dispatches these messages to the appropriate application's message pump
  • The application does with these messages whatever it wants

Now .NET's WinForms wraps this message pump for you, and exposes messages as events you can subscribe to. You can see it in action by overriding WndProc() on your control of coice:

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

More in-depth (from MSDN: About Messages and Message Queues), a Windows message is associated to a window (or "control" in WinForms terms) using a handle. This handle uniquely identifies a control.

This way WinForms knows that a mouse message with handle X is intended for control Y with that handle, so it raises the MouseClick or related event upon receiving that message.

Upvotes: 3

Adil
Adil

Reputation: 148120

Windows uses message-passing model. The operating system communicates with your application window by passing messages to it. A message is simply a numeric code that designates a particular 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 pass 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 mode Details you can read this article, Window Messages

Upvotes: 3

Callum Bradbury
Callum Bradbury

Reputation: 956

Tracking the mouse position & state is handled by the operating system itself, there's a whole bunch of input flags that are sent to applications when the mouse moves/is clicked/etc. The same windows WM_LBUTTONDOWN and WM_LBUTTONUP events that you would use in a C++ or C application (See Here) are intercepted by the .Net framework and converted into the clicked events we all know and love. It's all handled by the framework itself, the specifics of exactly what it does to translate those into 'this specific button was clicked' in an efficient manner I don't know, but at the end of the day it's irrelevant really.

You don't need to implement it because the developers of the .Net Framework and its UI elements realized that everyone would need to implement that functionality for a button, and so did it for us.

Upvotes: 3

Related Questions