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

嵌入式的.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)

你可能感兴趣的:(.net,object,null,嵌入式,Class,button)