C#進程間通訊--API傳遞參數(SendMessage)

注:本文爲我的學習摘錄,原文地址:http://blog.chinaunix.net/uid-26790551-id-3180355.html   
 
咱們不只能夠傳遞系統已經定義好的消息,還能夠傳遞自定義的消息(只須要發送消息端和接收消息端對自定義的消息值統一便可)。下面的發送和接收端同時演示了系統消息和自定義的消息。消息統一採用4位16進制的數。
1.系統消息使用的是0x0100(WM_KEYDOWN);0x0000--0x0400是系統自定義的消息,其中0x0000爲WM_NULL,0x0400爲WM_USER。0x0400之後的數值咱們能夠做爲自定義的消息值。(見附錄)
2.自定義消息Message,代碼以下:
 

點擊(此處)摺疊或打開css

  1. class Message
  2.     {
  3.         public const int USER = 0x0400;
  4.         public const int WM_TEST = USER + 101;
  5.         public const int WM_MSG = USER + 102;
  6.     }

3.接收端和發送端
接收端Form1:
 

點擊(此處)摺疊或打開html

  1. public partial class Form1 : Form
  2.     {
  3.         public Form1()
  4.         {
  5.             InitializeComponent();
  6.         }
  7.         [DllImport("User32.dll", EntryPoint = "SendMessage")]
  8.         private static extern int SendMessage(IntPtr hWnd, int msg, uint wParam, uint lParam);
  9.         //[DllImport("MessageDLL.dll", EntryPoint = "StartSendMessage")]//MessageDLL找不到
  10.         //private static extern int StartSendMessage(IntPtr hWnd);
  11.         //本身觸發自定義消息
  12.         private void button1_Click(object sender, EventArgs e)
  13.         {
  14.             SendMessage(this.Handle, Message.WM_TEST, 100, 200);
  15.         }
  16.         private void button2_Click(object sender, EventArgs e)
  17.         {
  18.             //StartSendMessage(this.Handle);
  19.         }
  20.         //響應和處理自定義消息
  21.         protected override void DefWndProc(ref System.Windows.Forms.Message m)
  22.         {
  23.             string message;
  24.             switch (m.Msg)
  25.             {
  26.                 case Message.WM_TEST://處理消息
  27.                     message = string.Format("收到從應用程序發出的消息!參數爲:{0}, {1}", m.WParam, m.LParam);
  28.                     MessageBox.Show(message);
  29.                     break;
  30.                 //case Message.WM_MSG:
  31.                 // message = string.Format("收到從DLL發出的消息!參數爲:{0}, {1}", m.WParam, m.LParam);
  32.                 // MessageBox.Show(message);
  33.                 // break;
  34.                 default:
  35.                     base.DefWndProc(ref m);
  36.                     break;
  37.             }
  38.         }
  39.         private void Form1_KeyDown(object sender, KeyEventArgs e)
  40.         {
  41.             this.label1.Text = e.KeyValue.ToString();
  42.         }
  43.     }

發送端Form2:button1啓動接收端,button2關閉接收端,button3傳遞系統消息和自定義的消息。
 

點擊(此處)摺疊或打開ide

  1. public partial class Form1 : Form
  2.     {
  3.         [DllImport("User32.dll", EntryPoint = "SendMessage")]
  4.         private static extern int SendMessage(IntPtr hWnd, int msg, int wParam, int lParam);
  5.         //[DllImport("MessageDLL.dll", EntryPoint = "StartSendMessage")]//MessageDLL找不到
  6.         //private static extern int StartSendMessage(IntPtr hWnd);
  7.         //internal string local = "this is a local internal";
  8.         public Form1()
  9.         {
  10.             InitializeComponent();
  11.         }
  12.         //ClassLibrary1.Class1 cla = new ClassLibrary1.Class1();
  13.         ProcessStartInfo startInfo = new ProcessStartInfo();
  14.         Process pro = new Process();
  15.         private void Form1_Load(object sender, EventArgs e)
  16.         {
  17.             startInfo.FileName = @"F:\Projects\WindowsFormsApplication1\WindowsFormsApplication1\bin\Debug\WindowsFormsApplication1.exe";
  18.             pro.StartInfo = startInfo;
  19.         }
  20.         private void button1_Click(object sender, EventArgs e)
  21.         {
  22.             pro.Start();
  23.         }
  24.         private void button2_Click(object sender, EventArgs e)
  25.         {
  26.             pro.Kill();
  27.         }
  28.         private void button3_Click(object sender, EventArgs e)
  29.         {
  30.             IntPtr hWnd = pro.MainWindowHandle;
  31.             int data = Convert.ToInt32(this.textBox1.Text);
  32.             SendMessage(hWnd, 0x0100, data, 0);
  33.             SendMessage(hWnd, Message.WM_TEST, 300, 300);
  34. }

4.界面顯示:
點擊button1:
 
左側窗體爲發送端,右側窗體爲接收端。
左側窗體在文本框中輸入數值,而後點擊button2後,左右窗體顯示以下:
 
 
4.關於消息驅動
如今VS上已經由事件驅動取代了消息驅動,不過像消息驅動仍是在進程間通訊經常使用的。此外 原本試着經過控制檯應用程序向窗體發送消息,沒有發送成功(若是有高人解決了,麻煩告訴me一下下哈)。不過通常消息發送與處理都是窗口之間進行的。
5.附錄:系統消息
 
6.連接資料網站
SendMessage和PostMessage函數:http://blog.csdn.net/sbz0409/article/details/3772965
相關文章
相關標籤/搜索