博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
C#中发送消息给指定的窗口到消息循环
阅读量:4303 次
发布时间:2019-05-27

本文共 3111 字,大约阅读时间需要 10 分钟。

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

 

        }

    }

 在协同标绘窗口里拦截消息的函数:

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"://对象添加

                            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;

            } 

            <br data-filtered="filtered">}

 

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。 看例子

1

2

3

4

5

6

7

8

9

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,它非常适合寻找子窗口。

 

// 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);
                }
            }

 

转载地址:http://cdlws.baihongyu.com/

你可能感兴趣的文章
Gradle学习 - xml操作
查看>>
java+sql server 数据库实现英才大学图书管理系统 第一部分 连接数据库及界面实现
查看>>
面试——操作系统
查看>>
字节c++后端实习生一二三面面经
查看>>
从零开始的数据挖掘课复习(预习)
查看>>
数据挖掘期末考题针对复习
查看>>
python32bit乐玩插件注册
查看>>
python32bit大漠插件注册
查看>>
Jupyter 快捷键
查看>>
机器学习模型的保存和读取
查看>>
如何跨模块共享全局变量?
查看>>
学习Python常用英语词汇,你学会了吗?
查看>>
html+css,vs code前端必备插件
查看>>
css文本格式化标签
查看>>
超链接标签-链接分类
查看>>
微信小程序项目使用npm安装vant-weapp的正确步骤
查看>>
pycharm+anaconda window平台环境搭建
查看>>
uni-app屏幕实现左右上限滑动事件判断
查看>>
Vue计算属性computed
查看>>
Vue 侦听器watch
查看>>