PostMessage与SendMessage的区别

Posted Herzog

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了PostMessage与SendMessage的区别相关的知识,希望对你有一定的参考价值。

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();
         }
     }    
 } 
复制代码

 

以上是关于PostMessage与SendMessage的区别的主要内容,如果未能解决你的问题,请参考以下文章

PostMessage与SendMessage的区别

delphi 中 postmessage 和sendmessage用法

SendMessage, postmessage模拟鼠标左键

sendmessage和postmessage的区别

如何使用 PostMessage/SendMessage User32 单击菜单项

sendmessage、postmessage和直接调用该对话框的方法,有何区别,那个好一点?