首页 > 系统相关 >C#向其实进程子窗体发送指令

C#向其实进程子窗体发送指令

时间:2022-10-20 10:02:12浏览次数:53  
标签:IntPtr C# DllImport int 指令 窗体 extern static public

近日,想在自己的软件简单控制其它软件的最大化最小化,想到直接向进程发送指令,结果一直无效,经过Spy++发现,原来快捷方式在子窗体上,所以需要遍历子窗体在发送指令,以下为参考代码:

 1         [DllImport("user32.dll", SetLastError = true)]
 2         public static extern bool PostMessage(IntPtr hWnd, int Msg, System.Windows.Forms.Keys wParam, IntPtr lParam);
 3 
 4         /// <summary>
 5         /// 发送消息到指定窗口
 6         /// </summary>
 7         /// <param name="hWnd">其窗口程序将接收消息的窗口的句柄。如果此参数为HWND_BROADCAST,则消息将被发送到系统中所有顶层窗口,
 8         /// 包括无效或不可见的非自身拥有的窗口、被覆盖的窗口和弹出式窗口,但消息不被发送到子窗口</param>
 9         /// <param name="msg">指定被发送的消息</param>
10         /// <param name="wParam">指定附加的消息指定信息</param>
11         /// <param name="lParam">指定附加的消息指定信息</param>
12         /// <returns></returns>
13         [DllImport("User32.dll", EntryPoint = "SendMessage")]
14         private static extern IntPtr SendMessage(int hWnd, int msg, IntPtr wParam, IntPtr lParam);
15 
16         [DllImport("User32.dll")]
17         public static extern int FindWindow(string lpClassName, string lpWindowName);
18 
19         [DllImport("User32.dll", EntryPoint = "FindWindowEx")]
20         public static extern IntPtr FindWindowEx(IntPtr hwndParent, IntPtr hwndChildAfter, string lpClassName, string lpWindowName);
21 
22         /// <summary>
23         /// 获取指定窗体的所有子窗体
24         /// </summary>
25         /// <param name="WinHandle">窗体句柄</param>
26         /// <param name="ec">回调委托</param>
27         /// <param name="name">自定义</param>
28         /// <returns></returns>
29         [DllImport("User32.dll")]
30         public static extern int EnumChildWindows(IntPtr WinHandle, EnumChildWindow ecw, string name);
31 
32         /// <summary>
33         /// 用于枚举子窗体是的委托
34         /// </summary>
35         /// <param name="WindowHandle">窗体句柄</param>
36         /// <param name="num">自定义</param>
37         /// <returns></returns>
38         public delegate bool EnumChildWindow(IntPtr WindowHandle, string num);
39 
40         /// <summary>
41         /// 获取窗体类型
42         /// </summary>
43         /// <param name="WinHandle">窗体句柄</param>
44         /// <param name="Type">类型</param>
45         /// <param name="size">缓冲区大小</param>
46         /// <returns></returns>
47         [DllImport("user32.dll")]
48         public static extern int GetClassName(IntPtr WinHandle, StringBuilder Type, int size);
49 
50         /// <summary>
51         /// 获取指定窗体的标题
52         /// </summary>
53         /// <param name="WinHandle">窗体句柄</param>
54         /// <param name="Title">缓冲区取用于存储标题</param>
55         /// <param name="size">缓冲区大小</param>
56         /// <returns></returns>
57         [DllImport("User32.dll")]
58         public static extern int GetWindowText(IntPtr WinHandle, StringBuilder Title, int size);
59 
60         /// <summary>
61         /// 根据句柄获得进程id值
62         /// </summary>
63         /// <param name="handle">句柄</param>
64         /// <param name="pid"></param>
65         /// <returns></returns>
66         [DllImport("user32")]
67         private static extern int GetWindowThreadProcessId(IntPtr handle, out int pid);
68 
69         [DllImport("user32.dll")]
70         public static extern int FindWindowEx(int hwndParent, int hwndChildAfter,
71     string lpszClass, string lpszWindow);
72 
73         [DllImport("user32.dll")]
74         public static extern int GetLastActivePopup(int hWnd);
75         [DllImport("user32.dll")]
76         public static extern int AnyPopup();
77         [DllImport("user32.dll")]
78         public static extern int GetWindowText(int hWnd, StringBuilder lpString, int nMaxCount);
79         [DllImport("user32.dll")]
80         public static extern int EnumThreadWindows(int dwThreadId, CallBack lpfn, int lParam);
81         [DllImport("user32.dll")]
82         public static extern int EnumWindows(CallBack lpfn, int lParam);
83         [DllImport("user32.dll")]
84         public static extern int EnumChildWindows(int hWndParent, CallBack lpfn, int lParam);

 

 1         public const int WM_SYSCOMMAND = 0x112;
 2         public const int WM_SYSCOMMAND1 = 0x111;
 3         const int WM_SYSKEYDOWN = 0x104;
 4         const int WM_SYSKEYUP = 0x105;
 5 
 6         public const int SC_CLOSE = 0xF060;// '关闭窗体
 7         public const int SC_MINIMIZE = 0xF020;// '最小化窗体
 8         public const int SC_MAXIMIZE = 0xF030;// '最大化窗体
 9         public const int SC_RESTORE = 0xF120;// '恢复窗体大小
10         public const int WM_SETTEXT = 0xC;// '设置窗体的Caption
11         public const int WM_GETTEXT = 0xD;// '取得窗体的caption
12         public const int LOGINFORM_MSG = 0x400;
13         public const int WM_KEYDOWN = 0x0100;
14         public const int WM_KEYUP = 0x0101;
15         private const int VK_ESCAPE = 0x1B;

 1         /// <summary>
 2         /// 回调函数代理
 3         /// </summary>
 4         public delegate bool CallBack(int hwnd, int lParam);
 5         [DllImport("user32.dll")]
 6         public static extern int EnumWindows(CallBack lpfn, int lParam);
 7         [DllImport("user32.dll", SetLastError = true)]
 8         public static extern bool PostMessage(IntPtr hWnd, int Msg, System.Windows.Forms.Keys wParam, IntPtr lParam);
 9 
10         /// <summary>
11         /// 发送消息到指定窗口
12         /// </summary>
13         /// <param name="hWnd">其窗口程序将接收消息的窗口的句柄。如果此参数为HWND_BROADCAST,则消息将被发送到系统中所有顶层窗口,
14         /// 包括无效或不可见的非自身拥有的窗口、被覆盖的窗口和弹出式窗口,但消息不被发送到子窗口</param>
15         /// <param name="msg">指定被发送的消息</param>
16         /// <param name="wParam">指定附加的消息指定信息</param>
17         /// <param name="lParam">指定附加的消息指定信息</param>
18         /// <returns></returns>
19         [DllImport("User32.dll", EntryPoint = "SendMessage")]
20         private static extern IntPtr SendMessage(int hWnd, int msg, IntPtr wParam, IntPtr lParam);
21 
22         [DllImport("User32.dll")]
23         public static extern int FindWindow(string lpClassName, string lpWindowName);
24 
25         [DllImport("User32.dll", EntryPoint = "FindWindowEx")]
26         public static extern IntPtr FindWindowEx(IntPtr hwndParent, IntPtr hwndChildAfter, string lpClassName, string lpWindowName);
27 
28         /// <summary>
29         /// 进程窗口回调函数代理
30         /// </summary>
31         public CallBack callBackEnumThreadWindows = new CallBack(ThreadWindowProcess);
32         /// <summary>
33         /// 窗口回调函数代理
34         /// </summary>
35         public CallBack callBackEnumWindows = new CallBack(WindowProcess);
36         /// <summary>
37         /// 子窗口回调函数代理
38         /// </summary>
39         public static CallBack callBackEnumChildWindows = new CallBack(ChildWindowProcess);
40         /// <summary>
41         /// 窗口回调处理函数
42         /// </summary>
43         /// <param name="hwnd"></param>
44         /// <param name="lParam"></param>
45         /// <returns></returns>
46         public static bool WindowProcess(int hwnd, int lParam)
47         {
48             EnumChildWindows(hwnd, callBackEnumChildWindows, 0);
49             return true;
50         }
51         /// <summary>
52         /// 子窗口回调处理函数
53         /// </summary>
54         /// <param name="hwnd"></param>
55         /// <param name="lParam"></param>
56         /// <returns></returns>
57         public static bool ChildWindowProcess(int hwnd, int lParam)
58         {
59             if (hwnd > 0)
60             {
61                 //发送Esc退出全屏指令
62                 long result = SendMessage(hwnd, WM_KEYDOWN, new IntPtr((int)Keys.Escape), new IntPtr(SC_RESTORE)).ToInt64();
63 
64                 result = SendMessage(hwnd, WM_SYSCOMMAND, new IntPtr((int)Keys.Escape), new IntPtr(VK_ESCAPE)).ToInt64();
65             }
66             return true;
67         }
68 
69 //调用方法,直接在监测子过程中处理发送消息
70 EnumWindows(callBackEnumWindows, 0);

另外一种方法是直接向进程发送指令

            Process[] pros = Process.GetProcesses(); //获取本机所有进程
            for (int i = 0; i < pros.Length; i++)
            {
                if (pros[i].ProcessName == "EzvizStudio") //名称为ProcessCommunication的进程
                {
                    IntPtr hWnd = pros[i].MainWindowHandle; //获取ProcessCommunication.exe主窗口句柄
                    IntPtr ExitWnd = FindWindowEx(hWnd, new IntPtr(0), null, "");
                    if (ExitWnd != IntPtr.Zero)
                    {
                        long result = SendMessage((int)ExitWnd, WM_KEYDOWN, new IntPtr((int)Keys.Escape), new IntPtr(SC_RESTORE)).ToInt64();

                        result = SendMessage((int)hWnd, WM_SYSCOMMAND, new IntPtr((int)Keys.Escape), new IntPtr(VK_ESCAPE)).ToInt64();
                    }

                    //SendMessage(hWnd, 0x0100, (IntPtr)data, (IntPtr)0); //点击该按钮,以文本框数据为参数,向Form1发送WM_KEYDOWN消息
                    // ProcessExtensions.SendKey(pros[i], Keys.Escape);
                    break;
                }
            }

另外可以直接向系统发送快捷指令

long result = SendMessage(WINDOW_HANDLER, WM_SYSCOMMAND, new IntPtr((int)Keys.Escape), new IntPtr(VK_ESCAPE)).ToInt64();

 

标签:IntPtr,C#,DllImport,int,指令,窗体,extern,static,public
From: https://www.cnblogs.com/kingkie/p/16808716.html

相关文章

  • arc145前三题
    为什么只有前三题呢。。。第四题想了一个小时没思路(主要是半个小时的时候发现看错题了),然后看粉兔博客发现要用Cantor集一类的神奇玩意,手贱看了E题发现还是线性基。于是就ru......
  • php 调用c# .NET 写的webservice(亲测通过)
    先上结果图——C# 代码:usingSystem;usingSystem.Collections.Generic;usingSystem.Linq;usingSystem.Web;usingSystem.Web.Services;usingSystem.Web.Services......
  • git 阻止在某个分支上面提交commit
    比如在开发中不希望master分支被commit做提交,那么我们可以这样做找到.git/hook/文件夹然后在里面复制一个pre-commit出来cd.git/hooks/cp然后编辑它的第二行类似于这样......
  • Vue 中 created 和 mounted 的区别
    大多数人在谈论生命周期钩子时会感到困惑的一件事是 ​​created​​​ 和 ​​mounted​​之间的区别。有着相似的名称,觉得应该做同样的事情,但还是有一些细微的差别。首......
  • leetcode 最长回文子串
    constcountSubstrings=(s)=>{conststrLen=s.length;letnumOfPalindromicStr=0;//初始化一个二维数组letdp=Array.from(Array(strLen),()=>A......
  • 实验2 C语言控制语句应用编程
    task1.c源代码:1#include<stdio.h>2#include<time.h>3#include<stdlib.h>4#defineN55intmain(){6intnumber;7inti;89s......
  • JDBC-快速入门和DriverManager注册驱动
    JDBC-快速入门步骤:1.导入驱动jar包(依赖也可以)<dependency><groupId>mysql</groupId><artifactId>mysql-connector-java</artifactId><version>8.0.......
  • Javascript--变量内函数句柄
    <!DOCTYPEhtml><html><head><metacharset="utf-8"><title>菜鸟教程(runoob.com)</title></head><body><p>创建和使用对象方法。</p><p>对象方法作为一个函数定义存储......
  • mysql函数 字符长度限制_MySQL中使用group_concat()函数数据字符过长报错的问题解决方
    selectGROUP_CONCAT(uid)asuids,spread_uidfromeb_user_spreadwhereuid<>spread_uidGROUPBYspread_uid使用GROUP_CONCAT函数将字符串连接起来,数据量大的时候,会......
  • Vue前端的 Excel 导入和导出功能
    Excel解析为JSON基本内容组件效果和结构组件内容是很简单的结构和视图,直接查看如下的页面效果和代码即可:  <template><inputtype="file"ref="exc......