近日,想在自己的软件简单控制其它软件的最大化最小化,想到直接向进程发送指令,结果一直无效,经过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