通过进程ID获取中望CAD窗体并向其发送命令

2023-11-06 19:40

本文主要是介绍通过进程ID获取中望CAD窗体并向其发送命令,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

需求:

        通过自启动多个CAD应用程序,对其进行管理,针对不同CAD程序发送不同命令。

核心功能

using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;namespace StartAndSendMessageCAD
{public class WindowsHandleAndProcess{//SendMessage参数private const int WM_KEYDOWN = 0X100;private const int WM_KEYUP = 0X101;private const int WM_SYSCHAR = 0X106;private const int WM_SYSKEYUP = 0X105;private const int WM_SYSKEYDOWN = 0X104;private const int WM_CHAR = 0X102;public int ProcessId = 0;public IntPtr WindowHandle = IntPtr.Zero;const int MW_CLOSE = 0x0010;[DllImport("User32.dll", EntryPoint = "SendMessage")]private static extern int SendMessage(IntPtr hWnd, int Msg, int wParam, int lParam);[DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)]public static extern bool EnumWindows(EnumThreadWindowsCallback callback, IntPtr extraData);public delegate bool EnumThreadWindowsCallback(IntPtr hWnd, IntPtr lParam);[DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)]public static extern int GetWindowThreadProcessId(IntPtr hwnd, out int processId);[DllImport("user32.dll")]public static extern int GetWindowTextLength(IntPtr hWnd);[DllImport("User32.dll", CharSet = CharSet.Auto)]public static extern int GetWindowText(IntPtr hWnd, StringBuilder text, int nMaxCount);[DllImport("user32.dll", EntryPoint = "FindWindowEx", SetLastError = true)]public static extern IntPtr FindWindowEx(IntPtr hwndParent, IntPtr hwndChildAfter, string lpszClass, string lpszWindow);/// <summary>/// 获取所有指定进程下的应用程序窗口句柄/// </summary>/// <param name="processId">进程ID</param>/// <returns></returns>public Dictionary<string, IntPtr> GetWindowHandle(int processId){var windowHandles = new Dictionary<string, IntPtr>();EnumThreadWindowsCallback windowsCallback = new EnumThreadWindowsCallback(FindMainWindow2);EnumWindows(windowsCallback, IntPtr.Zero);//保持循环GC.KeepAlive(windowsCallback);// 遍历所有顶层窗体bool FindMainWindow2(IntPtr handle, IntPtr extraParameter){string windowName = GetWindowText(handle);int num;GetWindowThreadProcessId(handle, out num);if (num == processId){string windowText = windowName;if (!windowHandles.ContainsKey(windowText)){windowHandles.Add(windowText, handle);}}return true;}return windowHandles;}/// <summary>/// 通过窗体句柄获取窗体Text/// </summary>/// <param name="handle"></param>/// <returns></returns>public string GetWindowText(IntPtr handle){int length = GetWindowTextLength(handle);StringBuilder windowName = new StringBuilder(length + 1);GetWindowText(handle, windowName, windowName.Capacity);return windowName.ToString();}/// <summary>/// 获取应用程序窗口句柄/// </summary>/// <param name="processId">进程ID</param>/// <param name="keyWindowText">CAD窗体Text标识</param>/// <param name="keySecondWindowText">命令栏名称</param>/// <returns></returns>public IntPtr GetWindowHandle(int processId, string keyWindowText, string keySecondWindowText){var windowHandles = GetWindowHandle(processId);foreach (var windowHandle in windowHandles){var name = windowHandle.Key;var intPtr = windowHandle.Value;if (name.Contains(keyWindowText)){var allChildHandle = GetAllChildWindows(intPtr); //获得按钮的句柄foreach (var childWindowHandle in allChildHandle){if (childWindowHandle.Key.Contains(keySecondWindowText)){if (childWindowHandle.Value != IntPtr.Zero){return childWindowHandle.Value;}}}}}return IntPtr.Zero;}/// <summary>/// 通过某个窗体Handle获取其下所有子窗体名称及其handle/// </summary>/// <param name="handle"></param>/// <returns></returns>public Dictionary<string, IntPtr> GetAllChildWindows(IntPtr handle){var windowHandles = new Dictionary<string, IntPtr>();IntPtr childHwnd = FindWindowEx(handle, IntPtr.Zero, null, null);while (childHwnd != IntPtr.Zero){string windowsText = GetWindowText(childHwnd);if (!windowHandles.ContainsKey(windowsText)){windowHandles.Add(windowsText, childHwnd);}var windowHandlesTemp = GetAllChildWindows(childHwnd);foreach (var item in windowHandlesTemp){if (!windowHandles.ContainsKey(item.Key)){windowHandles.Add(item.Key, item.Value);}}childHwnd = FindWindowEx(handle, childHwnd, null, null);}return windowHandles;}/// <summary>/// 发送一个字符串/// </summary>/// <param name="myIntPtr">窗口句柄</param>/// <param name="Input">字符串</param>public void InputStr(IntPtr myIntPtr, string Input){byte[] ch = (ASCIIEncoding.ASCII.GetBytes(Input));for (int i = 0; i < ch.Length; i++){SendMessage(myIntPtr, WM_CHAR, ch[i], 0);}}}
}

如何调用:

 static void Main(string[] args){string exePath = @"C:\Program Files\ZWSOFT\ZWCAD 2023\ZWCAD.EXE";Process process = new Process();process.StartInfo.FileName = exePath;process.StartInfo.CreateNoWindow = true;process.Start();Console.WriteLine("CAD ProcessId is : " + process.Id);Thread.Sleep(10 * 1000);var handleAndProcess = new WindowsHandleAndProcess();var intPtr = handleAndProcess.GetWindowHandle(process.Id,"ZWCAD 20","CommandLine");if (intPtr != IntPtr.Zero){handleAndProcess.InputStr(intPtr, "some\n");}Console.ReadKey();}

示意图:

 

这篇关于通过进程ID获取中望CAD窗体并向其发送命令的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/358659

相关文章

零基础学习Redis(10) -- zset类型命令使用

zset是有序集合,内部除了存储元素外,还会存储一个score,存储在zset中的元素会按照score的大小升序排列,不同元素的score可以重复,score相同的元素会按照元素的字典序排列。 1. zset常用命令 1.1 zadd  zadd key [NX | XX] [GT | LT]   [CH] [INCR] score member [score member ...]

30常用 Maven 命令

Maven 是一个强大的项目管理和构建工具,它广泛用于 Java 项目的依赖管理、构建流程和插件集成。Maven 的命令行工具提供了大量的命令来帮助开发人员管理项目的生命周期、依赖和插件。以下是 常用 Maven 命令的使用场景及其详细解释。 1. mvn clean 使用场景:清理项目的生成目录,通常用于删除项目中自动生成的文件(如 target/ 目录)。共性规律:清理操作

[Linux]:进程(下)

✨✨ 欢迎大家来到贝蒂大讲堂✨✨ 🎈🎈养成好习惯,先赞后看哦~🎈🎈 所属专栏:Linux学习 贝蒂的主页:Betty’s blog 1. 进程终止 1.1 进程退出的场景 进程退出只有以下三种情况: 代码运行完毕,结果正确。代码运行完毕,结果不正确。代码异常终止(进程崩溃)。 1.2 进程退出码 在编程中,我们通常认为main函数是代码的入口,但实际上它只是用户级

Android Environment 获取的路径问题

1. 以获取 /System 路径为例 /*** Return root of the "system" partition holding the core Android OS.* Always present and mounted read-only.*/public static @NonNull File getRootDirectory() {return DIR_ANDR

利用命令模式构建高效的手游后端架构

在现代手游开发中,后端架构的设计对于支持高并发、快速迭代和复杂游戏逻辑至关重要。命令模式作为一种行为设计模式,可以有效地解耦请求的发起者与接收者,提升系统的可维护性和扩展性。本文将深入探讨如何利用命令模式构建一个强大且灵活的手游后端架构。 1. 命令模式的概念与优势 命令模式通过将请求封装为对象,使得请求的发起者和接收者之间的耦合度降低。这种模式的主要优势包括: 解耦请求发起者与处理者

linux 判断某个命令是否安装

linux 判断某个命令是否安装 if ! [ -x "$(command -v git)" ]; thenecho 'Error: git is not installed.' >&2exit 1fi

jenkins 插件执行shell命令时,提示“Command not found”处理方法

首先提示找不到“Command not found,可能我们第一反应是查看目标机器是否已支持该命令,不过如果相信能找到这里来的朋友估计遇到的跟我一样,其实目标机器是没有问题的通过一些远程工具执行shell命令是可以执行。奇怪的就是通过jenkinsSSH插件无法执行,经一番折腾各种搜索发现是jenkins没有加载/etc/profile导致。 【解决办法】: 需要在jenkins调用shell脚

java 进程 返回值

实现 Callable 接口 与 Runnable 相比,Callable 可以有返回值,返回值通过 FutureTask 进行封装。 public class MyCallable implements Callable<Integer> {public Integer call() {return 123;}} public static void main(String[] args

C#关闭指定时间段的Excel进程的方法

private DateTime beforeTime;            //Excel启动之前时间          private DateTime afterTime;               //Excel启动之后时间          //举例          beforeTime = DateTime.Now;          Excel.Applicat

linux中使用rust语言在不同进程之间通信

第一种:使用mmap映射相同文件 fn main() {let pid = std::process::id();println!(