- 浏览: 389139 次
- 性别:
- 来自: 上海
文章分类
- 全部博客 (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 1937first 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 1891Topic: C# – CoerceValueCallbac ... -
WPF – Virtualization – VirutalizationStackPanel and ItemsPanelTemplate
2013-08-05 21:55 1360Topic: WPF – Virtualization – ... -
wpf – ListView交替背景色
2013-07-02 20:56 6492Wpf – Alternate background col ... -
C# - 简单介绍TaskScheduler
2013-06-29 17:18 11933标题: C# - 简单介绍TaskSchedulerTit ... -
c# - Get enum from enum attribute
2013-06-27 21:32 1201DescriptionAttribute gives the ... -
C# - PInvoke, gotchas on the RegisterClassEx and the CreateWindowEx
2013-06-24 13:49 2534I get an exception message li ... -
c# - Use PInvoke to create simple win32 Application
2013-06-24 11:59 10882In this post, .net platform h ... -
c# - Linq's Select method as the Map function
2013-06-19 18:47 1240If you comes from a functiona ... -
c# - Tips of Linq expression Any to determine if a collection is Empty
2013-06-19 18:29 894When you are @ the linq expres ... -
myth buster - typeof accepting array of types not acceptable
2013-06-19 17:17 781I 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 1275Behavior is something that we ... -
WPF - Setting foreground color of Entire window
2013-06-13 16:00 1880You might as well as I would s ... -
WPF - Enhanced TabControl - TabControlEx aka Prerendering TabControl
2013-06-13 13:12 5291As an opening word, let's che ... -
wpf - ControlTemplate and AddLogicChild/RemoveLogicalChild
2013-06-10 15:42 1143Recently I was trying to debug ... -
c# - P/Invoke, DllImport, Marshal Structures and Type conversions
2013-06-05 15:25 1659P/Invoke as in the following q ... -
c# - A study on the NativeWindow - encapsulate window handle and procedure
2013-06-05 14:40 6045NativeWindow gives you a way t ... -
WCF - Notify server when client connects
2013-06-03 18:19 1182It is sometimes very importan ...
相关推荐
gong-wpf-dragdrop, GongSolutions.WPF.DragDrop 库是WPF的拖动'n'拖放框架 简介GongSolutions.WPF.DragDrop 库是一个易于使用的拖拉'n'拖放框架。特性使用 MVVM: 拖放逻辑可以放在ViewModel中。 代码不需要放在in中...
c#-的WPF---MVVM例子,大家共同学习
一个简单的WPF程序
bootstrap-wpf-style-master 样式代码 。。。。。。。。
C#开发WPF-Silverlight动画及游戏系列教程-深蓝色右手 C#开发WPF-Silverlight动画及游戏系列教程-深蓝色右手 C#开发WPF-Silverlight动画及游戏系列教程-深蓝色右手
wpf--ListBox分组-排序 wpf--ListBox分组-排序
wpf-资源的使用-resource
我最近打算学习WPF ,在寻找MVVM框架的时候发现了PRISM,在此之前还从一些博客上了解了其它的MVVM框架,比如浅谈WPF中的MVVM框架--MVVMFoundation 中提到的MVVMFoundation,再比如 ViewModel从未如此清爽 - 轻量级...
http://www.codeproject.com/Articles/22952/WPF-Diagram-Designer-Part-1 http://www.codeproject.com/Articles/23265/WPF-Diagram-Designer-Part-2 ...
Winform与WPF窗体互相调用方法 包括Winform调用WPF窗体和WPF调用Winform窗体。
WPF的基本空间历程,使用.net core3.0.1版本
用WPF做了一个登录页面,用Winform做了一个主界面,运行时先进行登录页面,再进入主界面。登录账号:apple,登录密码:123456。试运行之前先生成
因项目需要合入vlc播放器,由于工程是64位的,而在网上找的大部分是32位的vlc库和demo无法兼容。现在终于找到一个32/64位的库demo,自己再重新整理了一下分享出来。
Game2048__WPF-Linq-Event C# 项目 源代码
wpf-wcf-wf整合开发实例--有利于理解wpf-wcf-wf的整合
WPF收集程序
这是一个WPF方式生成组织架构图的源代码。可以根据树数据生成树状组织结构图或者族谱图,也可以根据自己实际需要进行扩展或者修改。
WPF--Effect特效 对Effect的进行使用的小例子
wpf-datagrid-access DB,可以更新数据到Access数据库
WPF 与 WinForm 调用系统右键菜单/资源管理器右键菜单/桌面右键菜单示例 内含 WPF 与 WinForm 示例