WinCE/Mobile 下的自定义消息处理方法

本文介绍如何在嵌入式的.net开发环境中通过PostMessage、CallWindowProc和SetWindowLong方法重写WndProc来过滤自定义消息,并提供了一个实际的范例工程。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

嵌入式的.net 开发,我们无法重写WndProc来过滤自定义消息。我的项目中曾这样实现过,今天有网友问,我就贴在博客中。希望对大家有个帮助。

using System; using System.Linq; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Text; using System.Windows.Forms; using System.Diagnostics; using System.Runtime.InteropServices; namespace MessagedWindowCE { public partial class Form1 : Form { [DllImport("coredll.dll")] private static extern int PostMessage(IntPtr hwnd, int wMsg, int wParam, int lParam); [DllImport("coredll.dll")] private extern static int CallWindowProc(IntPtr lpPrevWndFunc, IntPtr hwnd, uint msg, uint wParam, int lParam); [DllImport("coredll.dll")] public static extern IntPtr SetWindowLong(IntPtr hwnd, int nIndex, IntPtr dwNewLong); public const int GWL_WNDPROC = (-4); public IntPtr OldProc = IntPtr.Zero; public Form1() { InitializeComponent(); } public delegate int WndProcHandler(IntPtr hwnd, uint msg, uint wParam, int lParam); WndProcHandler myproc = null; int MyWndProc(IntPtr hwnd, uint msg, uint wParam, int lParam) { if (msg == 0x410) { MessageBox.Show("yeah"); } return CallWindowProc(OldProc, hwnd, msg, wParam, lParam); } protected override void OnHandleCreated(EventArgs e) { base.OnHandleCreated(e); myproc = new WndProcHandler(MyWndProc); OldProc = SetWindowLong(Handle, GWL_WNDPROC, Marshal.GetFunctionPointerForDelegate(myproc)); } private void button1_Click(object sender, EventArgs e) { PostMessage(Handle, 0x410, 0, 0); } } }

范例工程(2008/.net 2.0 cf)

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值