WPF 利用HwndSource拦截Windows消息
来源:互联网 发布:js 判断对象是否存在 编辑:程序博客网 时间:2024/06/05 08:29
WPF提供了一个HwndSource可以使你更快的实现处理Windows消息。
通过HwndSource.FromHwnd得到的HwndSource可以添加(AddHook)移除(Remove)Hook
首先注册SourceInitialized事件,在事件中创建一个HwndSource对象,
然后利用其AddHook方法来将所有的windows消息附加到一个现有的事件中,这个就是WpfHandleWinowMsg。
代码如下:
public partial class MainWindow : Window { public MainWindow() { InitializeComponent(); SourceInitialized += HandleInitialized; } public void HandleInitialized(object o,EventArgs e) { IntPtr wptr = new WindowInteropHelper(this).Handle; HwndSource hs = HwndSource.FromHwnd(wptr); hs.AddHook(new HwndSourceHook(WpfHandleWinowMsg)); } public IntPtr WpfHandleWinowMsg(IntPtr hwnd, int msg, IntPtr wParam, IntPtr lParam, ref bool handled) { //这个函数可以做很多事情,只要是windows消息都会经过,例如注册全局快捷键,修改窗体大小边框,等等 //也可以调API做对应的事情 switch (msg) { case 1: break; case 2: break; default: break; } return IntPtr.Zero; } }
顺带说下Winform,直接重载WndProc函数即可
public partial class Form1 : Form{ int WM_NCLBUTTONDBLCLK = 0xA3; protected override void WndProc(ref Message m) { if (m.Msg == WM_NCLBUTTONDBLCLK) { if (this.Height < 35) { this.Height = h; } else { h = this.Height; this.Height = -10; } return; } base.WndProc(ref m); //让基类去处理 }}
阅读全文
0 0
- WPF 利用HwndSource拦截Windows消息
- WPF中的Windows消息
- WPF收发windows消息
- 拦截Windows消息
- 拦截Windows消息
- 拦截Windows消息
- 用BCB拦截Windows消息
- Windows Mobile 消息拦截编程
- C++ Builder拦截Windows消息
- Android 中利用XPosed拦截系统消息
- Windows消息拦截技术的应用
- Windows消息拦截技术的应用
- windows中使用钩子拦截消息
- Windows消息拦截技术的应用
- 利用Windows消息循环实现消息延迟和消息合并
- C# 重写WndProc 拦截 发送 系统消息 + windows消息常量值(1)--windows消息大全
- Hook技术之消息拦截(Windows Hook )
- WPF中如何拦截系统消息,并进行相应的修改
- RNN-LSTM循环神经网络-03Tensorflow进阶实现
- 资深程序员之路(5)--agile开发
- Win从环境变量开启MySQL之旅
- Android系统扫描音频文件详解
- ES6 的一些新函数(不定时更新)
- WPF 利用HwndSource拦截Windows消息
- Java BigDecimal开方
- nginx环境调试确认
- JAVA基础:子类、父类继承关系 静态块、非静态块、构造函数初始化顺序性、静态非静态方法覆盖
- css设置单行居中,多行居左/居右
- eclipse内存配置
- 二维数组中的查找
- Git 创建github远程仓库并关联本地项目
- QT收费吗?