[轉] C#中發送消息給指定的窗口,以及接收消息


原文 C#中發送消息給指定的窗口,以及接收消息

public class Note
    {
        //聲明 API 函數 
        [DllImport("User32.dll", EntryPoint = "SendMessage")]
        private static extern IntPtr SendMessage(int hWnd, int msg, IntPtr wParam, IntPtr lParam);
          
        [DllImport("User32.dll", EntryPoint = "FindWindow")]
        private static extern int FindWindow(string lpClassName, string lpWindowName);
        
         //定義消息常數 
        public const int CUSTOM_MESSAGE = 0X400 + 2;//自定義消息

        
       //向窗體發送消息的函數 
      public void SendMsgToMainForm(int MSG)
        {
            int WINDOW_HANDLER = FindWindow(null, "協同標繪");
            if (WINDOW_HANDLER == 0)
            {
                throw new Exception("Could not find Main window!");
            }

            long result = SendMessage(WINDOW_HANDLER, CUSTOM_MESSAGE, new IntPtr(14), IntPtr.Zero).ToInt64();

        }
    } 

 在協同標繪窗口里攔截消息的函數:

protected override void WndProc(ref System.Windows.Forms.Message msg) 
{
            switch (msg.Msg)
            {
                case Note.CUSTOM_MESSAGE: //處理消息
                    {
                    switch (msg.WParam.ToString())
                    {
                        case "11"://對象添加
                            string s = "mdesheng";
                            addOne(s);
                            break;

                        case "12"://對象更新
                            string sn = "m";
                            updateID(sn);
                            break;

                         case "13"://對象刪除
                            deleteID("5");
                            break;

                         case "14"://與會者信息更新
                            updateClientInfor("in_1_張三_在線");
                            break;
                    }

                    }
                       
                        break;
                default:
                    base.WndProc(ref msg);//調用基類函數處理非自定義消息。
                    break;
            } 
            
} private void button1_Click(object sender, EventArgs e) {   Note a = new Note();   a.SendMsgToMainForm(Note.CUSTOM_MESSAGE); }

    FindWindow()函數的用法。要在C#里使用該API,寫出FindWindow()函數的聲明:
  [DllImport("coredll.dll", EntryPoint = "FindWindow")]
  private extern static IntPtr FindWindow(string lpClassName, string lpWindowName);
  

  這個函數有兩個參數,第一個是要找的窗口的類,第二個是要找的窗口的標題,是窗體的Text名字,不是name。在搜索的時候不一定兩者都知道,但至少要知道其中的一個。有的窗口的標題是比較容易得到的,如"計算器",所以搜索時應使用標題進行搜索。但有的軟件的標題不是固定的,如"記事本",如果打開的文件不同,窗口標題也不同,這時使用窗口類搜索就比較方便。如果找到了滿足條件的窗口,這個函數返回該窗口的句柄,否則返回0。 看例子

IntPtr ParenthWnd = new IntPtr(0);
ParenthWnd = FindWindow(null,"Word Mobile");
//判斷這個窗體是否有效 
 if (ParenthWnd != IntPtr.Zero)
{
    MessageBox.Show("找到窗口");
}
else
    MessageBox.Show("沒有找到窗口");

  從上面的討論中可以看出,如果要搜索的外部程序的窗口標題比較容易得到,問題是比較簡單的。可如果窗口的標題不固定或者根本就沒有標題,怎么得到窗口的類呢?如果你安裝了Visual C++,你可以使用其中的Spy,在Spy++中有一個FindWindow工具,它允許你使用鼠標選擇窗口,然后Spy++會顯示這個窗口的類。 


  在Win32 API中還有一個FindWindowEx,它非常適合尋找子窗口。

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM