c# 调用API函数 SendMessage,没有成功(附有代码)
在Form1.cs文件中:
using System.Runtime.InteropServices;
[DllImport( "user32.dll ", EntryPoint = "SendMessageA ")]
private static extern int SendMessage(IntPtr hWnd ,int wMsg, int wParam, int lParam);
private void button1_Click(object sender, EventArgs e)
{
SendMessage(this.button1.Handle, TestMessage.TEST1, 100, 101);
} //TestMessage.TEST1 为定义的消息常量,调试显示正常
protected override void DefWndProc(ref System.Windows.Forms.Message m)
{
switch(m.Msg)
{
case TestMessage.TEST1:
string message = string.Format( "收到消息:参数{0}, {1} ", m.LParam, m.WParam);
MessageBox.Show(message);
break;
default:
base.DefWndProc(ref m);
break;
}
}
编译通过,但调试发现没有进入 case TestMessage.TEST1:
是怎么回事啊?
谢谢!
------解决方案--------------------PostMessage试试
------解决方案--------------------1.DefWndProc 是一个方法,它的作用是向默认处理函数发送制定消息,而不是处理消息.也就是说某种程度上就是SendMessage.
而处理消息的是WndProc.
2.SendMessage发送的handle应该是form的handle,而不是控件,改成this.Handle