高手进,关于PostMessage和SendMessage的问题
我做了2个程序A和B,A发消息,B接收A:
[DllImport("User32.dll", EntryPoint = "SendMessage")]
private static extern int SendMessage(
int hWnd, //目标窗口的handle
int Msg, // 消息
int wParam, // 第一个消息参数
ref COPYDATASTRUCT lParam // 第二个消息参数
);
[DllImport("User32.dll", EntryPoint = "FindWindow")]
private static extern int FindWindow(string lpClassName, string
lpWindowName);
public struct COPYDATASTRUCT
{
public IntPtr dwData;
public int cbData;
[MarshalAs(UnmanagedType.LPStr)]
public string lpData;
}
const int WM_COPYDATA = 0x004A;
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
int WINDOW_HANDLER = FindWindow(null, @"FrmReceive");
if (WINDOW_HANDLER != 0)
{
byte[] sarr = System.Text.Encoding.Default.GetBytes(textBox1.Text);
int len = sarr.Length;
COPYDATASTRUCT cds;
cds.dwData = (IntPtr)100;
cds.lpData = textBox1.Text;
cds.cbData = len + 1;
SnedMessage(WINDOW_HANDLER, WM_COPYDATA, 0, ref cds);
}
B:
public struct COPYDATASTRUCT
{
public IntPtr dwData;
public int cbData;
[MarshalAs(UnmanagedType.LPStr)] public string lpData;
}
const int WM_COPYDATA = 0x004A;
public receive()
{
InitializeComponent();
}
protected override void DefWndProc(ref Message m)
{
if (m.Msg == WM_COPYDATA)
{
COPYDATASTRUCT mystr = new COPYDATASTRUCT();
Type mytype = mystr.GetType();
mystr = (COPYDATASTRUCT)m.GetLParam(mytype);
switch(mystr.dwData.ToString())
{
case "100":
textBox1.Text = mystr.lpData;
break;
case "200":
textBox2.Text = mystr.lpData;
break;
}
}
base.DefWndProc(ref m);
}
}
现在用SendMessage测试成功,但把SendMessage改成PostMessage后,消息就无法传递了
想请问下,如果要用PostMessage的话,应该怎么改呢? --------------------编程问答-------------------- PostMessage消息发出后不等消息函数处理完就执行它的下一语句
SendMessage消息发出后阻塞等消息函数处理完执行它的下一语句
其他地方没有区别的 --------------------编程问答--------------------
是的,因为我现在是SERVER发给CLIENT,所以想用PostMessage,但上面代码改成PostMessage后,消息就是发不过 --------------------编程问答-------------------- mark --------------------编程问答-------------------- 试试WM_SETTEXT作为参数 --------------------编程问答--------------------
不行,WM_SETTEXT作为参数是设置标题的.... --------------------编程问答-------------------- SendMessage把消息直接发送到窗口,并调用此窗口的相应消息处理函数,等消息处理函数结束后SendMessage才返回!SendMessage发送的消息不进入系统的消息队列;SendMessage函数有返回值
PostMessage将消息发送到与创建窗口的线程相关联的消息队列后立即返回;PostMessage函数没有返回值;
我用PostMessage把消息发到B后,B怎么去获得这个消息呢? --------------------编程问答-------------------- 顶上去,高手来解答下吧
补充:.NET技术 , C#