- 浏览: 400824 次
- 性别:
- 来自: 上海
文章分类
- 全部博客 (309)
- xaml C# wpf (0)
- scala java inner clas (1)
- Tools UML Eclipse UML2 (1)
- Timer .NET Framework (1)
- perl (6)
- python function paramter (1)
- Python Docstring (1)
- Python how to compare types (1)
- Python (8)
- java (5)
- C# (76)
- C# WPF (0)
- p4 (0)
- WPF (46)
- .net (6)
- xaml (1)
- javascript (40)
- windows (10)
- scala (4)
- winform (1)
- c++ (48)
- tools (12)
- cmd (1)
- os (0)
- CI (0)
- shell (0)
- C (2)
- haskell (49)
- functional (1)
- tool (1)
- gnu (1)
- linux (1)
- kaskell (0)
- svn (0)
- wcf (3)
- android (1)
最新评论
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 - example to enhance ComboBox for AutoComplete
2014-09-19 15:56 1979first let’s see an example ... -
Investigate and troubleshoot possible memory leak issue of .NET application
2014-07-31 10:42 0Hi All, I would like to sh ... -
C# – CoerceValueCallback合并、替换元数据值
2013-08-05 21:59 1929Topic: C# – CoerceValueCallbac ... -
WPF – Virtualization – VirutalizationStackPanel and ItemsPanelTemplate
2013-08-05 21:55 1420Topic: WPF – Virtualization – ... -
wpf – ListView交替背景色
2013-07-02 20:56 6554Wpf – Alternate background col ... -
C# - 简单介绍TaskScheduler
2013-06-29 17:18 12054标题: C# - 简单介绍TaskSchedulerTit ... -
c# - Get enum from enum attribute
2013-06-27 21:32 1251DescriptionAttribute gives the ... -
C# - PInvoke, gotchas on the RegisterClassEx and the CreateWindowEx
2013-06-24 13:49 2576I get an exception message li ... -
c# - Use PInvoke to create simple win32 Application
2013-06-24 11:59 10952In this post, .net platform h ... -
c# - Linq's Select method as the Map function
2013-06-19 18:47 1293If you comes from a functiona ... -
c# - Tips of Linq expression Any to determine if a collection is Empty
2013-06-19 18:29 939When you are @ the linq expres ... -
myth buster - typeof accepting array of types not acceptable
2013-06-19 17:17 817I have seen from some book whe ... -
windows - trying to create WIN32 application with PInvoke
2013-06-19 14:34 0While it is stupid to do such ... -
wpf - BehaviorBase and one use examples
2013-06-18 18:41 1316Behavior is something that we ... -
WPF - Setting foreground color of Entire window
2013-06-13 16:00 1924You might as well as I would s ... -
WPF - Enhanced TabControl - TabControlEx aka Prerendering TabControl
2013-06-13 13:12 5337As an opening word, let's che ... -
wpf - ControlTemplate and AddLogicChild/RemoveLogicalChild
2013-06-10 15:42 1190Recently I was trying to debug ... -
c# - P/Invoke, DllImport, Marshal Structures and Type conversions
2013-06-05 15:25 1716P/Invoke as in the following q ... -
c# - A study on the NativeWindow - encapsulate window handle and procedure
2013-06-05 14:40 6098NativeWindow gives you a way t ... -
WCF - Notify server when client connects
2013-06-03 18:19 1226It is sometimes very importan ...
相关推荐
通用WPF主题控件包rubyer-wpf-master是一款专为Windows Presentation Foundation (WPF) 应用程序设计的开源UI框架。它提供了丰富的主题和控件,旨在帮助开发者快速构建美观且用户友好的应用程序界面。在2.0.0版本中...
**WPF-Blockly** 是一个基于Windows Presentation Foundation (WPF) 的图形化编程工具,它为用户提供了构建和设计程序的直观界面。WPF作为Microsoft .NET Framework的一部分,主要用于构建桌面应用程序,它提供了...
gong-wpf-dragdrop, GongSolutions.WPF.DragDrop 库是WPF的拖动'n'拖放框架 简介GongSolutions.WPF.DragDrop 库是一个易于使用的拖拉'n'拖放框架。特性使用 MVVM: 拖放逻辑可以放在ViewModel中。 代码不需要放在in中...
标题中的“c#/WPF-VLC播放器demo 32/64位”表明这是一个示例项目,展示了如何在C# WPF应用中使用VLC,同时考虑到了不同体系结构的兼容性。这通常是通过使用支持两种架构的VLC库来实现的,因为默认的VLC库可能只针对...
在.NET框架中,Windows Forms(Winform)和Windows Presentation Foundation(WPF)是两种不同的UI开发技术。Winform主要用于创建传统的桌面应用程序,而WPF则提供了更强大的图形渲染能力和丰富的用户体验设计。有时...
在这个名为"WPF-ControlBase-master.zip"的压缩包中,我们可以推测它包含了一个基于WPF的控制库项目,可能是一个开源或者个人开发的项目,用于提供自定义的WPF控件。这些控件可能是对标准WPF控件的扩展或增强,也...
在本文中,我们将深入探讨如何使用WPF(Windows Presentation Foundation)创建一个登录界面,并通过该界面无缝地打开一个基于WinForm的主应用程序窗口。WPF是.NET Framework的一部分,提供了丰富的用户界面(UI)...
</Button> </Grid> </ControlTemplate> ``` 3. **处理状态转换**:RadioButton有多种状态,如Checked、Unchecked、Indeterminate等。为了保持按钮的行为,我们需要使用VisualStateManager来管理这些状态。 ```...
WPF的基本空间历程,使用.net core3.0.1版本
C#开发WPF-Silverlight动画及游戏系列教程-深蓝色右手 C#开发WPF-Silverlight动画及游戏系列教程-深蓝色右手 C#开发WPF-Silverlight动画及游戏系列教程-深蓝色右手
【标题】"WPF-JJDown-v1.234.0" 提示我们这是一个基于Windows Presentation Foundation(WPF)的应用程序,名为JJDown。版本号v1.234.0表明这是该软件的第1次重大更新,第234次次要更新或修复。这通常意味着它经历了...
8. **布局和组件**:Bootstrap-WPF 样式支持常见的 Bootstrap 组件,如卡片(Card)、网格系统(Grid)、导航条(Navbar)、按钮(Button)、表单(Form)等。开发者可以直接在 WPF 应用程序中使用这些组件,以快速...
在本文中,我们将深入探讨如何在Windows Presentation Foundation (WPF) 中使用3D技术来创建交互式的物体事件处理实例,具体以实现一个茶壶模型为例。WPF是Microsoft .NET Framework的一部分,它提供了丰富的功能来...
标题“host wpf in winform”涉及的是将Windows Presentation Foundation(WPF)的元素嵌入到Windows Forms(WinForm)应用程序中的技术。这种混合编程模式允许开发者利用WinForm的稳定性和WPF的丰富图形功能,为...
http://www.codeproject.com/Articles/22952/WPF-Diagram-Designer-Part-1 http://www.codeproject.com/Articles/23265/WPF-Diagram-Designer-Part-2 ...
在.NET框架中,Windows Forms(Winform)和Windows Presentation Foundation(WPF)是两种不同的UI开发技术。尽管它们各自有着独特的特性和优势,但在实际项目中,开发者有时需要将两者结合,实现Winform窗体与WPF...
在.NET框架中,WPF(Windows Presentation Foundation)和WinForm是两种不同的用户界面(UI)开发技术。WPF是微软推出的下一代UI框架,而WinForm是.NET Framework早期的主要UI库。有时,开发者可能会遇到需要在WPF...
<Button.BorderThickness>2</Button.BorderThickness> <Button.CornerRadius>5</Button.CornerRadius> <Button.Content>自定义按钮</Button.Content> </Button> ``` 3. **图像按钮**: 按钮不仅可以显示文本...
通过深入研究WPF-Diagram-Designer的源代码(如WPF-Diagram-Designer-master文件夹中的内容),开发者不仅可以学习到如何在WPF中构建复杂的图形界面,还可以了解到图形编辑器的设计原理和实现细节,对于提升图形应用...