`

wpf - translate winform button/mouse event to wpf events

阅读更多

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. 

 

分享到:
评论

相关推荐

    通用WPF主题控件包rubyer-wpf-master

    通用WPF主题控件包rubyer-wpf-master是一款专为Windows Presentation Foundation (WPF) 应用程序设计的开源UI框架。它提供了丰富的主题和控件,旨在帮助开发者快速构建美观且用户友好的应用程序界面。在2.0.0版本中...

    WPF-Blockly-master.zip

    **WPF-Blockly** 是一个基于Windows Presentation Foundation (WPF) 的图形化编程工具,它为用户提供了构建和设计程序的直观界面。WPF作为Microsoft .NET Framework的一部分,主要用于构建桌面应用程序,它提供了...

    gong-wpf-dragdrop, GongSolutions.WPF.DragDrop 库是WPF的拖动'n'拖放框架.zip

    gong-wpf-dragdrop, GongSolutions.WPF.DragDrop 库是WPF的拖动'n'拖放框架 简介GongSolutions.WPF.DragDrop 库是一个易于使用的拖拉'n'拖放框架。特性使用 MVVM: 拖放逻辑可以放在ViewModel中。 代码不需要放在in中...

    c#/WPF-VLC播放器demo 32/64位

    标题中的“c#/WPF-VLC播放器demo 32/64位”表明这是一个示例项目,展示了如何在C# WPF应用中使用VLC,同时考虑到了不同体系结构的兼容性。这通常是通过使用支持两种架构的VLC库来实现的,因为默认的VLC库可能只针对...

    Winform调用WPF控件

    在.NET框架中,Windows Forms(Winform)和Windows Presentation Foundation(WPF)是两种不同的UI开发技术。Winform主要用于创建传统的桌面应用程序,而WPF则提供了更强大的图形渲染能力和丰富的用户体验设计。有时...

    WPF-ControlBase-master.zip

    在这个名为"WPF-ControlBase-master.zip"的压缩包中,我们可以推测它包含了一个基于WPF的控制库项目,可能是一个开源或者个人开发的项目,用于提供自定义的WPF控件。这些控件可能是对标准WPF控件的扩展或增强,也...

    WPF登录打开WinForm主界面

    在本文中,我们将深入探讨如何使用WPF(Windows Presentation Foundation)创建一个登录界面,并通过该界面无缝地打开一个基于WinForm的主应用程序窗口。WPF是.NET Framework的一部分,提供了丰富的用户界面(UI)...

    wpf radiobutton模版设计成button样式

    </Button> </Grid> </ControlTemplate> ``` 3. **处理状态转换**:RadioButton有多种状态,如Checked、Unchecked、Indeterminate等。为了保持按钮的行为,我们需要使用VisualStateManager来管理这些状态。 ```...

    WPF-Samples-master_WPF基本sample_

    WPF的基本空间历程,使用.net core3.0.1版本

    C#开发WPF-Silverlight动画及游戏系列教程-深蓝色右手 4

    C#开发WPF-Silverlight动画及游戏系列教程-深蓝色右手 C#开发WPF-Silverlight动画及游戏系列教程-深蓝色右手 C#开发WPF-Silverlight动画及游戏系列教程-深蓝色右手

    WPF-JJDown-v1.234.0

    【标题】"WPF-JJDown-v1.234.0" 提示我们这是一个基于Windows Presentation Foundation(WPF)的应用程序,名为JJDown。版本号v1.234.0表明这是该软件的第1次重大更新,第234次次要更新或修复。这通常意味着它经历了...

    bootstrap-wpf-style-master 样式

    8. **布局和组件**:Bootstrap-WPF 样式支持常见的 Bootstrap 组件,如卡片(Card)、网格系统(Grid)、导航条(Navbar)、按钮(Button)、表单(Form)等。开发者可以直接在 WPF 应用程序中使用这些组件,以快速...

    WPF-3D-物体的事件处理实例

    在本文中,我们将深入探讨如何在Windows Presentation Foundation (WPF) 中使用3D技术来创建交互式的物体事件处理实例,具体以实现一个茶壶模型为例。WPF是Microsoft .NET Framework的一部分,它提供了丰富的功能来...

    host wpf in winform

    标题“host wpf in winform”涉及的是将Windows Presentation Foundation(WPF)的元素嵌入到Windows Forms(WinForm)应用程序中的技术。这种混合编程模式允许开发者利用WinForm的稳定性和WPF的丰富图形功能,为...

    WPF Diagram Designer Source

    http://www.codeproject.com/Articles/22952/WPF-Diagram-Designer-Part-1 http://www.codeproject.com/Articles/23265/WPF-Diagram-Designer-Part-2 ...

    Winform与WPF窗体互相调用方法

    在.NET框架中,Windows Forms(Winform)和Windows Presentation Foundation(WPF)是两种不同的UI开发技术。尽管它们各自有着独特的特性和优势,但在实际项目中,开发者有时需要将两者结合,实现Winform窗体与WPF...

    wpf调用WinForm程序

    在.NET框架中,WPF(Windows Presentation Foundation)和WinForm是两种不同的用户界面(UI)开发技术。WPF是微软推出的下一代UI框架,而WinForm是.NET Framework早期的主要UI库。有时,开发者可能会遇到需要在WPF...

    WPFButton样式

    <Button.BorderThickness>2</Button.BorderThickness> <Button.CornerRadius>5</Button.CornerRadius> <Button.Content>自定义按钮</Button.Content> </Button> ``` 3. **图像按钮**: 按钮不仅可以显示文本...

    WPF-Diagram-Designer:WPF图表设计器源代码

    通过深入研究WPF-Diagram-Designer的源代码(如WPF-Diagram-Designer-master文件夹中的内容),开发者不仅可以学习到如何在WPF中构建复杂的图形界面,还可以了解到图形编辑器的设计原理和实现细节,对于提升图形应用...

Global site tag (gtag.js) - Google Analytics