日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程语言 > C# >内容正文

C#

C#中发送消息给指定的窗口到消息循环

發(fā)布時(shí)間:2023/12/18 C# 35 豆豆
生活随笔 收集整理的這篇文章主要介紹了 C#中发送消息给指定的窗口到消息循环 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

public?class?Note

????{

????????//聲明 API 函數(shù)

????????[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);

?????????

?????????//定義消息常數(shù)

????????public?const?int?CUSTOM_MESSAGE = 0X400 + 2;//自定義消息

?

?????????

???????//向窗體發(fā)送消息的函數(shù)

??????public?void?SendMsgToMainForm(int?MSG)

????????{

????????????int?WINDOW_HANDLER = FindWindow(null,?"協(xié)同標(biāo)繪");

????????????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();

?

????????}

????}

?在協(xié)同標(biāo)繪窗口里攔截消息的函數(shù):

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

protected?override?void?WndProc(ref?System.Windows.Forms.Message msg)

{

????????????switch?(msg.Msg)

????????????{

????????????????case?Note.CUSTOM_MESSAGE:?//處理消息

????????????????????{

????????????????????switch?(msg.WParam.ToString())

????????????????????{

????????????????????????case?"11"://對(duì)象添加

????????????????????????????string?s =?"mdesheng";

????????????????????????????addOne(s);

????????????????????????????break;

?

????????????????????????case?"12"://對(duì)象更新

????????????????????????????string?sn =?"m";

????????????????????????????updateID(sn);

????????????????????????????break;

?

?????????????????????????case?"13"://對(duì)象刪除

????????????????????????????deleteID("5");

????????????????????????????break;

?

?????????????????????????case?"14"://與會(huì)者信息更新

????????????????????????????updateClientInfor("in_1_張三_在線");

????????????????????????????break;

????????????????????}

?

????????????????????}

????????????????????????

????????????????????????break;

????????????????default:

????????????????????base.WndProc(ref?msg);//調(diào)用基類函數(shù)處理非自定義消息。

????????????????????break;

????????????} 

????????????<br data-filtered="filtered">}

?

private?void?button1_Click(object?sender, EventArgs e)

{

  Note a =?new?Note();

  a.SendMsgToMainForm(Note.CUSTOM_MESSAGE);

}

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

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

1

2

3

4

5

6

7

8

9

IntPtr ParenthWnd =?new?IntPtr(0);

ParenthWnd = FindWindow(null,"Word Mobile");

//判斷這個(gè)窗體是否有效

?if?(ParenthWnd != IntPtr.Zero)

{

????MessageBox.Show("找到窗口");

}

else

????MessageBox.Show("沒有找到窗口");

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


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

在C#中向windows窗體發(fā)送消息示例

// define
[DllImport("User32.dll")]
??????? public static extern IntPtr FindWindow(String lpClassName, String lpWindowName);

??????? [DllImport("User32.dll")]
??????? static extern IntPtr FindWindowEx(IntPtr hwndParent, IntPtr hwndChildAfter, string lpszClass, string lpszWindow);

??????? [DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = false)]
??????? static extern IntPtr SendMessage(IntPtr hWnd, uint Msg, IntPtr wParam, IntPtr lParam);

??????? const int WM_LBUTTONDOWN = 0x201;
??????? const int WM_LBUTTONUP = 0x0202;

??// sample, u should use spy++ to find windows class name and control?class name

??????? IntPtr hwndWin = FindWindow("TfrmMain", "window title");
??????????? if (hwndWin.Equals(IntPtr.Zero) == false)
??????????? {
??????????????? IntPtr hwndBtn = FindWindowEx(hwndWin, IntPtr.Zero, "TButton", "control text");
??????????????? if (hwndBtn.Equals(IntPtr.Zero) == false)
??????????????? {
??????????????????? SendMessage(hwndBtn, WM_LBUTTONDOWN, IntPtr.Zero, IntPtr.Zero);
??????????????????? SendMessage(hwndBtn, WM_LBUTTONUP, IntPtr.Zero, IntPtr.Zero);
??????????????? }
??????????? }

?

總結(jié)

以上是生活随笔為你收集整理的C#中发送消息给指定的窗口到消息循环的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。