我们不仅可以传递系统已经定义好的消息,还可以传递自定义的消息(只需要发送消息端和接收消息端对自定义的消息值统一即可)。下面的发送和接收端同时演示了系统消息和自定义的消息。消息统一采用4位16进制的数。
1.系统消息使用的是0x0100(WM_KEYDOWN);0x0000--0x0400是系统自定义的消息,其中0x0000为WM_NULL,0x0400为WM_USER。0x0400以后的数值我们可以作为自定义的消息值。(见附录)
2.自定义消息Message,代码如下:
- class Message
- {
- public const int USER = 0x0400;
- public const int WM_TEST = USER + 101;
- public const int WM_MSG = USER + 102;
- }
3.接收端和发送端
接收端Form1:
- public partial class Form1 : Form
- {
- public Form1()
- {
- InitializeComponent();
- }
- [DllImport("User32.dll", EntryPoint = "SendMessage")]
- private static extern int SendMessage(IntPtr hWnd, int msg, uint wParam, uint lParam);
- //[DllImport("MessageDLL.dll", EntryPoint = "StartSendMessage")]//MessageDLL找不到
- //private static extern int StartSendMessage(IntPtr hWnd);
- //自己触发自定义消息
- private void button1_Click(object sender, EventArgs e)
- {
- SendMessage(this.Handle, Message.WM_TEST, 100, 200);
- }
- private void button2_Click(object sender, EventArgs e)
- {
- //StartSendMessage(this.Handle);
- }
- //响应和处理自定义消息
- protected override void DefWndProc(ref System.Windows.Forms.Message m)
- {
- string message;
- switch (m.Msg)
- {
- case Message.WM_TEST://处理消息
- message = string.Format("收到从应用程序发出的消息!参数为:{0}, {1}", m.WParam, m.LParam);
- MessageBox.Show(message);
- break;
- //case Message.WM_MSG:
- // message = string.Format("收到从DLL发出的消息!参数为:{0}, {1}", m.WParam, m.LParam);
- // MessageBox.Show(message);
- // break;
- default:
- base.DefWndProc(ref m);
- break;
- }
- }
- private void Form1_KeyDown(object sender, KeyEventArgs e)
- {
- this.label1.Text = e.KeyValue.ToString();
- }
- }
发送端Form2:button1启动接收端,button2关闭接收端,button3传递系统消息和自定义的消息。
- public partial class Form1 : Form
- {
- [DllImport("User32.dll", EntryPoint = "SendMessage")]
- private static extern int SendMessage(IntPtr hWnd, int msg, int wParam, int lParam);
- //[DllImport("MessageDLL.dll", EntryPoint = "StartSendMessage")]//MessageDLL找不到
- //private static extern int StartSendMessage(IntPtr hWnd);
- //internal string local = "this is a local internal";
- public Form1()
- {
- InitializeComponent();
- }
- //ClassLibrary1.Class1 cla = new ClassLibrary1.Class1();
- ProcessStartInfo startInfo = new ProcessStartInfo();
- Process pro = new Process();
- private void Form1_Load(object sender, EventArgs e)
- {
- startInfo.FileName = @"F:\Projects\WindowsFormsApplication1\WindowsFormsApplication1\bin\Debug\WindowsFormsApplication1.exe";
- pro.StartInfo = startInfo;
- }
- private void button1_Click(object sender, EventArgs e)
- {
- pro.Start();
- }
- private void button2_Click(object sender, EventArgs e)
- {
- pro.Kill();
- }
- private void button3_Click(object sender, EventArgs e)
- {
- IntPtr hWnd = pro.MainWindowHandle;
- int data = Convert.ToInt32(this.textBox1.Text);
- SendMessage(hWnd, 0x0100, data, 0);
- SendMessage(hWnd, Message.WM_TEST, 300, 300);
- }
4.界面显示:
点击button1:
左侧窗体为发送端,右侧窗体为接收端。
左侧窗体在文本框中输入数值,然后点击button2后,左右窗体显示如下:
4.关于消息驱动
现在VS上已经由事件驱动取代了消息驱动,不过像消息驱动还是在进程间通信常用的。此外本来试着通过控制台应用程序向窗体发送消息,没有发送成功(如果有高人解决了,麻烦告诉me一下下哈)。不过一般消息发送与处理都是窗口之间进行的。
5.附录:系统消息
6.链接资料网站
阅读(32937) | 评论(0) | 转发(2) |