用 PostMessage、SendNotifyMessage、SendMessageCallback 等異步函數發送系統消息時,參數裡不可以使用指針,因為發送者並不等待消息的處理就返回,接受者還沒處理指針就已經被釋放了。 5、在 Windows 2000/XP 裡,每個消息隊列最多只能存放 10,000 個 Post 的消息,超過的還沒被處理的將不會被處理,直接丟掉。這個值可以改得更大:[HKEY_LOCAL_MACHINE/SOFTWARE/ Microsoft/Windows NT/CurrentVersion/Windows] USERPostMessageLimit,最小可以是 4000。 PostMessage只負責將消息放到消息隊列中,不確定何時及是否處理 SendMessage要等到受到消息處理的返回碼(DWord類型)後才繼續 PostMessage執行後馬上返回 SendMessage必須等到消息被處理後才會返回。 下面通過一個小例子來說明下這2個方法進行參數傳遞的不同點:
//Win32 API 類 using System; using System.Runtime.InteropServices; namespace TestHwnd { public class Win32API { [DllImport("User32.dll", EntryPoint = "FindWindow")] public static extern IntPtr FindWindow(string lpClassName, string lpWindowName); [DllImport("User32.dll", EntryPoint = "FindWindowEx")] public static extern IntPtr FindWindowEx(IntPtr hwndParent, IntPtr hwndChildAfter, string lpClassName, string lpWindowName); /// <summary> /// 自定義的結構 /// </summary> public struct My_lParam { public int i; public string s; } /// <summary> /// 使用COPYDATASTRUCT來傳遞字符串 /// </summary> [StructLayout(LayoutKind.Sequential)] public struct COPYDATASTRUCT { public IntPtr dwData; public int cbData; [MarshalAs(UnmanagedType.LPStr)] public string lpData; } //消息發送API [DllImport("User32.dll", EntryPoint = "SendMessage")] public static extern int SendMessage( IntPtr hWnd, // 信息發往的窗口的句柄 int Msg, // 消息ID int wParam, // 參數1 int lParam //參數2 ); //消息發送API [DllImport("User32.dll", EntryPoint = "SendMessage")] public static extern int SendMessage( IntPtr hWnd, // 信息發往的窗口的句柄 int Msg, // 消息ID int wParam, // 參數1 ref My_lParam lParam //參數2 ); //消息發送API [DllImport("User32.dll", EntryPoint = "SendMessage")] public static extern int SendMessage( IntPtr hWnd, // 信息發往的窗口的句柄 int Msg, // 消息ID int wParam, // 參數1 ref COPYDATASTRUCT lParam //參數2 ); //消息發送API [DllImport("User32.dll", EntryPoint = "PostMessage")] public static extern int PostMessage( IntPtr hWnd, // 信息發往的窗口的句柄 int Msg, // 消息ID int wParam, // 參數1 int lParam // 參數2 ); //消息發送API [DllImport("User32.dll", EntryPoint = "PostMessage")] public static extern int PostMessage( IntPtr hWnd, // 信息發往的窗口的句柄 int Msg, // 消息ID int wParam, // 參數1 ref My_lParam lParam //參數2 ); //異步消息發送API [DllImport("User32.dll", EntryPoint = "PostMessage")] public static extern int PostMessage( IntPtr hWnd, // 信息發往的窗口的句柄 int Msg, // 消息ID int wParam, // 參數1 ref COPYDATASTRUCT lParam // 參數2 ); } } //主窗體,發送消息 using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms; using System.Runtime.InteropServices; namespace TestHwnd { public partial class Main : Form { public IntPtr hwndTest; public int IwndTest; public IntPtr hwndfrmTest; public Main() { InitializeComponent(); } private void button1_Click(object sender, EventArgs e) { Test test = new Test(); test.Show(this); } private void timer1_Tick(object sender, EventArgs e) { string strTest = "25425"; Win32API.COPYDATASTRUCT cds; cds.dwData = (IntPtr)100; cds.lpData = strTest; byte[] sarr = System.Text.Encoding.UTF8.GetBytes(strTest); int len = sarr.Length; cds.cbData = len + 1; Win32API.My_lParam lp=new Win32API.My_lParam(); lp.i=3; lp.s="test"; if(hwndTest!=(IntPtr)0) { if (DateTime.Now.Second % 2 == 0) { Win32API.SendMessage(hwndTest, 0x60, 1, 3);//傳遞2個整型參數成功 } if(DateTime.Now.Second % 3 == 0) { Win32API.SendMessage(hwndTest, 0x61, 5, ref lp);//傳遞整型參數和結構類型成功,這個方法加以改變後可以傳遞對象 } if(DateTime.Now.Second % 5 == 0) { Win32API.SendMessage(hwndTest, 0x62, 5, ref cds);//傳遞整型參數和不定長的字符串成功 } if(DateTime.Now.Second % 7 == 0) { Win32API.PostMessage(hwndTest, 0x63, 5, 6);//傳遞2個整型參數成功 } if(DateTime.Now.Second % 9 == 0) { Win32API.PostMessage(hwndTest, 0x64, 3, ref lp);//傳遞整型參數成功,但是傳遞參數lp失敗,3可以傳遞成功。 } if(DateTime.Now.Second % 11 == 0) { Win32API.PostMessage(hwndTest, 0x65, 3, ref cds);//傳遞整型參數成功,傳遞參數cds失敗,3可以傳遞成功。 } } } } } //子窗體接收消息以及參數 using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms; using System.Runtime.InteropServices; namespace TestHwnd { public partial class Test : Form { Main main; public Test() { InitializeComponent(); } private void Test_Load(object sender, EventArgs e) { main = this.Owner as Main; main.hwndTest = this.Handle; } ///重寫窗體的消息處理函數DefWndProc,從中加入自己定義消息的檢測的處理入口 protected override void DefWndProc(ref Message m) { switch (m.Msg) { //接收自定義消息MYMESSAGE,並顯示其參數 case 0x60: { label1.Text = DateTime.Now.ToString() + "-" + m.WParam.ToInt32().ToString() + "-" + m.LParam.ToInt32().ToString(); } break; case 0x61: { Win32API.My_lParam ml = new Win32API.My_lParam(); Type t = ml.GetType(); ml = (Win32API.My_lParam)m.GetLParam(t); label2.Text = DateTime.Now.ToString() + "-" + m.WParam.ToInt32().ToString() + "-" + ml.i.ToString()+":"+ml.s; } break; case 0x62: { Win32API.COPYDATASTRUCT mystr = new Win32API.COPYDATASTRUCT(); Type mytype = mystr.GetType(); mystr = (Win32API.COPYDATASTRUCT)m.GetLParam(mytype); string str2 = mystr.lpData; label3.Text = DateTime.Now.ToString() + "-" + m.WParam.ToInt32().ToString() + "-" + str2; } break; case 0x63: { label4.Text = DateTime.Now.ToString() + "-" + m.WParam.ToInt32().ToString() + "-" + m.LParam.ToInt32().ToString(); } break; case 0x64: { Win32API.My_lParam ml = new Win32API.My_lParam(); Type t = ml.GetType(); ml = (Win32API.My_lParam)m.GetLParam(t); label5.Text = DateTime.Now.ToString() + "-" + m.WParam.ToInt32().ToString() + "-" + ml.i.ToString()+":"+ml.s; } break; case 0x65: { Win32API.COPYDATASTRUCT mystr = new Win32API.COPYDATASTRUCT(); Type mytype = mystr.GetType(); mystr = (Win32API.COPYDATASTRUCT)m.GetLParam(mytype); string str2 = mystr.lpData; label6.Text = DateTime.Now.ToString() + "-" + m.WParam.ToInt32().ToString() + "-" + str2; } break; default: base.DefWndProc(ref m); break; } } private void button1_Click(object sender, EventArgs e) { main.hwndTest = (IntPtr) (0); this.Close(); } } }