It is common that we sometimes have to host some winforms controls, things like the you have a WPF control which is called NoitfyIcon where you want to wrap a winform Notify, but to enhance the Winform Notify control.
so the skeleton of wpf NotifyIcon is like this
using System.Windows.Controls; using System.Windows public partial class NotifyIcon : FrameworkElement { private Forms.NotifyIcon notifyIcon; // ... }
One problem we have is that the event from winform word is of type System.Windows.Forms, and it raise the following eventArgs
while in WPF, the event are all RoutedEventArgs from the System.Windows.Input.MouseButtonEventArgs..., clearly you need some translation so that you can capture the Winform event and translate it to some routed event which is understanded by the wpf system.
Here is the code that does the translation.
private static MouseButton ToMouseButton(Forms.MouseButtons button) { switch (button) { case Forms.MouseButtons.Left: return MouseButton.Left; case Forms.MouseButtons.Right: return MouseButton.Right; case Forms.MouseButtons.Middle: return MouseButton.Middle; case Forms.MouseButtons.XButton1: return MouseButton.XButton1; case Forms.MouseButtons.XButton2: return MouseButton.XButton2; } throw new InvalidOperationException(); } private static MouseButtonEventArgs CreateMouseButtonEventArgs( RoutedEvent handler, Forms.MouseButtons button) { return new MouseButtonEventArgs(InputManager.Current.PrimaryMouseDevice, 0, ToMouseButton(button)) { RoutedEvent = handler }; }
As you can see, we are making the transition from System.Windows.Forms.MouseButton to System.Windows.Input.MouseButton;
then through the function CreateMouseButtonEventArgs, we are creating System.Windows.Input.MouseButtonEventArgs with the translated MouseButton object.
Now that we have the method to translate the EventArgs, we shall wire up and translate every event that we are interested in. e.g.
public static readonly RoutedEvent MouseClickEvent = EventManager.RegisterRoutedEvent( "MouseClick", RoutingStrategy.Bubble, typeof(MouseButtonEventHandler), typeof(NotifyIcon)); public static readonly RoutedEvent MouseDoubleClickEvent = EventManager.RegisterRoutedEvent( "MouseDoubleClick", RoutingStrategy.Bubble, typeof(MouseButtonEventHandler), typeof(NotifyIcon)); private void OnMouseDown(object sender, Forms.MouseEventArgs e) { this.RaiseEvent(CreateMouseButtonEventArgs(MouseDownEvent, e.Button)); } private void OnMouseDoubleClick(object sender, Forms.MouseEventArgs e) { this.RaiseEvent(CreateMouseButtonEventArgs(MouseDoubleClickEvent, e.Button)); } private void InitializeNotifyIcon() { this.notifyIcon = new Forms.NotifyIcon(); //... this.notifyIcon.MouseDown += this.OnMouseDown; this.notifyIcon.MouseUp += this.OnMouseUp; this.notifyIcon.MouseClick += this.OnMouseClick; this.notifyIcon.MouseDoubleClick += this.OnMouseDoubleClick; }
As you can see, we extends our class from the Framework element class, so we have to simulate the "MouseClick" and "MouseDoubleClick" event, this is done by the Registered Event DP - "MouseClick" and "MouseDoubleClick"
Once that is done, you can just listen to the to Event of the containing NotifyIcon (winform control) and in each separate handler, with the help of FrameworkElement.RaiseEvent to fire up the event.