wpf - translate winform button/mouse event to wpf events

46 篇文章 0 订阅

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

 

  • System.Windows.Forms.MouseEventArgs

 

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. 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值