Search code examples
c#wpfeventsrouted-eventsuievent

How are UI events generated in WPF?


I am trying to understand how UI related RoutedEvents are generated in WPF.

For instance, how does pressing the left mouse button on a control starts the tunneling of PreviewMouseDown?

I couldn't find a clear answer myself. My intuition is that the messages received by the window (e.g. WM_MOUSEFIRST) are processed and events are raised as a consequence, but this is just my guess.


Solution

  • Eventually it must be WM_MOUSEFIRST and similar Windows events that trigger actions in WPF. No doubt. That is how Windows works.

    Inside WPF, it uses EventManager and GlobalEventManager to handle events. A Window and all subsequent base classes registers itself at the EventManager (like Window and FrameworkElement here).

    The EventManager handles the actual events and takes care of the routing.

    The actual mouse events are handled by HwndMouseInputProvider and some Window events by Window itself.