关于使用C#调用Win32API,抓取第三方句柄,模拟鼠标点击,键盘发送事件(C2Prog.exe)

因为最近工作需要用就把基本知识整理了一下

主要操作这个软件写程序和选配置

 下面例子和Win32以及自己封装的库全在工程文件里面

2023.7.10 :以前写的代码丢了重新写了一下优化了不少 ,所以特此更新一下

以前是1.7的版本目前用的是1.9版本有些不一样需要注意

 这里放最新的连接(C2prog1.9版本的软件也在里面)

和以前的相比加了不少便捷使用的函数进去

这个是1.7版本
链接:https://pan.baidu.com/s/1imOVeULlxe82Ejv0dPNy_Q?pwd=6666 
提取码:6666 
--来自百度网盘超级会员V1的分享

更新时间:2023.7.26
链接:https://pan.baidu.com/s/1naRrKJX0VMIuXSID4d2bHQ?pwd=6666 
提取码:6666

调用示例

 textBox1.Text = classPro.Run19(@"C:\\Program Files (x86)\\C2Prog1.7\\C2Prog.exe", @"D:\Dbug\Template.hex", "F28M35X-C28-512K", "10MHz_4x", "com666","xx").ToString();

注意事项:

里面有两个批处理是用来打开和关闭C2Prog软件的需要放在C2Prog.exe所在的目录下面

 这样放

1安装句柄抓取工具SPY++ 

我用的工具是SPY++

没有这个工具的按照下面方法自己装一下

选中工具菜单下面的"获工具功能"

 1.

 选择单个组件里面的"C++核心功能" 进行安装就行

 

下载这个工具的主要目的为了抓取句柄

看接下来操作

首先打开我们的SPY++工具

点击窗口新建一个 

 

 接下来就会出现程序运行的所有信息

 我们只查找我们需要的,打开我们需要查找句柄的窗体

 点击搜索>查找窗口

 接下来出现这样一个界面

 我们只需要拖动查找工具,就是哪个圆形东西到指定窗体就能拿到窗体的句柄了

 我们拖动到 Program 按钮就拿到了按钮句柄

接下来在C#中来实操一下

0x00010A3C这个就是我们刚才拿到的句柄  0x0201 和0X0202分别是鼠标左键按下和弹起   我们运行后可以看到软件上按钮确实被单击了,你们要自己实践

 // 导入Windows API函数,用于给窗口发送消息
        [DllImport("User32.dll")]
        public static extern int SendMessage(int hWnd, int msg, int wParam, string lParam);

        private void button7_Click(object sender, EventArgs e)
        {
            PostMessage((IntPtr)0x00010A3C, 0x0201, 0x0001, 0); //左键按下
            Thread.Sleep(200);
            PostMessage((IntPtr)0x00010A3C, 0x0202, 0x0001, 0); //弹起
        }

 这里需要注意一下我们的第三方软件句柄是每次重新打开软件都会重新分配的,所以句柄是一个不确定的值,那么我们该如何获取呢?

  // 导入Windows API函数,用于查找窗口句柄
        [DllImport("user32.dll")]
        private static extern IntPtr FindWindow(string lpClassName, string lpWindowName);

        // 导入Windows API函数,用于查找子窗口句柄
        [DllImport("user32.dll")]
        private static extern IntPtr FindWindowEx(IntPtr hWndParent, IntPtr hWndChildAfter, string lpClassName, string lpWindowName);


        /// <summary>
        /// 获取主窗体句柄
        /// </summary>
        /// <param name="APPname">主窗体名称 没有就填null</param>
        /// <param name="APPclass">主窗体类 这个必须填</param>
       public IntPtr Get_Main_handle(string APPname,string APPclass)
        {
           return FindWindow(APPclass, APPname);
        }
        /// <summary>
        /// 获取主窗体下面子窗体句柄
        /// </summary>
        /// <param name="Main_APPname">主窗体名称</param>
        /// <param name="Main_APPclass">主窗体类</param>
        /// <param name="child_APPclass">子窗体类</param>
        /// <param name="child_APPname">子窗体名称 可以填null</param>
        /// <returns>返回找到的句柄</returns>
        public IntPtr Get_child_handle(string Main_APPname, string Main_APPclass, string child_APPclass, string child_APPname)
        {           
            IntPtr stmHandle = FindWindow(Main_APPclass, Main_APPname); 
            //在父窗体下 第0个窗体开始查找 类为SWT_Window0标题为null 代表忽略标题    
           return FindWindowEx(stmHandle, IntPtr.Zero, child_APPclass, child_APPname);
        }

 接下来看看实际运行效果

我们写了一个单击事件

 可以清晰看到我们的效果 文本框里面已经获取到了窗体的句柄 110BA8

 自己封装了一些常用到的API   链接放最上边了

接下来实现模拟键盘发送 A

  
        // 导入Windows API函数,用于发送键盘消息
        [DllImport("user32.dll")]
        private static extern void keybd_event(byte bVk, byte bScan, uint dwFlags, int dwExtraInfo);
        private void button7_Click(object sender, EventArgs e)
        {
            //模拟按下A
            keybd_event(0X65, 0, 0, 0);
           
            //松开按键A
            keybd_event(0X65, 0, 2, 0);
        }

下面列举一些比较常见的消息  大家可以自己替换

 这段事件弄了不是这个方面的,很多第三方窗体有下拉列表框,文本框,单选框,等等一系列基本都可以用鼠标点击方式配合键盘发送都能实现

这些常见的消息参考网上  C#窗体如何通过keybd_event()函数模拟键盘按键(组合键)产生事件 - 小小邪 - 博客园 (cnblogs.com)

#region bVk参数 常量定义

        public const byte vbKeyLButton = 0x1;    // 鼠标左键
        public const byte vbKeyRButton = 0x2;    // 鼠标右键
        public const byte vbKeyCancel = 0x3;     // CANCEL 键
        public const byte vbKeyMButton = 0x4;    // 鼠标中键
        public const byte vbKeyBack = 0x8;       // BACKSPACE 键
        public const byte vbKeyTab = 0x9;        // TAB 键
        public const byte vbKeyClear = 0xC;      // CLEAR 键
        public const byte vbKeyReturn = 0xD;     // ENTER 键
        public const byte vbKeyShift = 0x10;     // SHIFT 键
        public const byte vbKeyControl = 0x11;   // CTRL 键
        public const byte vbKeyAlt = 18;         // Alt 键  (键码18)
        public const byte vbKeyMenu = 0x12;      // MENU 键
        public const byte vbKeyPause = 0x13;     // PAUSE 键
        public const byte vbKeyCapital = 0x14;   // CAPS LOCK 键
        public const byte vbKeyEscape = 0x1B;    // ESC 键
        public const byte vbKeySpace = 0x20;     // SPACEBAR 键
        public const byte vbKeyPageUp = 0x21;    // PAGE UP 键
        public const byte vbKeyEnd = 0x23;       // End 键
        public const byte vbKeyHome = 0x24;      // HOME 键
        public const byte vbKeyLeft = 0x25;      // LEFT ARROW 键
        public const byte vbKeyUp = 0x26;        // UP ARROW 键
        public const byte vbKeyRight = 0x27;     // RIGHT ARROW 键
        public const byte vbKeyDown = 0x28;      // DOWN ARROW 键
        public const byte vbKeySelect = 0x29;    // Select 键
        public const byte vbKeyPrint = 0x2A;     // PRINT SCREEN 键
        public const byte vbKeyExecute = 0x2B;   // EXECUTE 键
        public const byte vbKeySnapshot = 0x2C;  // SNAPSHOT 键
        public const byte vbKeyDelete = 0x2E;    // Delete 键
        public const byte vbKeyHelp = 0x2F;      // HELP 键
        public const byte vbKeyNumlock = 0x90;   // NUM LOCK 键

        //常用键 字母键A到Z
        public const byte vbKeyA = 65;
        public const byte vbKeyB = 66;
        public const byte vbKeyC = 67;
        public const byte vbKeyD = 68;
        public const byte vbKeyE = 69;
        public const byte vbKeyF = 70;
        public const byte vbKeyG = 71;
        public const byte vbKeyH = 72;
        public const byte vbKeyI = 73;
        public const byte vbKeyJ = 74; 
        public const byte vbKeyK = 75;
        public const byte vbKeyL = 76;
        public const byte vbKeyM = 77;
        public const byte vbKeyN = 78;
        public const byte vbKeyO = 79 ;
        public const byte vbKeyP = 80 ;
        public const byte vbKeyQ = 81 ;
        public const byte vbKeyR = 82 ;
        public const byte vbKeyS = 83 ;
        public const byte vbKeyT = 84 ;
        public const byte vbKeyU = 85 ;
        public const byte vbKeyV = 86 ;
        public const byte vbKeyW = 87 ;
        public const byte vbKeyX = 88 ;
        public const byte vbKeyY = 89 ;
        public const byte vbKeyZ = 90 ;

        //数字键盘0到9
        public const byte vbKey0 = 48 ;    // 0 键
        public const byte vbKey1 = 49 ;    // 1 键
        public const byte vbKey2 = 50 ;    // 2 键
        public const byte vbKey3 = 51 ;    // 3 键
        public const byte vbKey4 = 52 ;    // 4 键
        public const byte vbKey5 = 53 ;    // 5 键
        public const byte vbKey6 = 54 ;    // 6 键
        public const byte vbKey7 = 55 ;    // 7 键
        public const byte vbKey8 = 56 ;    // 8 键
        public const byte vbKey9 = 57 ;    // 9 键


        public const byte vbKeyNumpad0 = 0x60 ;    //0 键
        public const byte vbKeyNumpad1 = 0x61 ;    //1 键
        public const byte vbKeyNumpad2 = 0x62 ;    //2 键
        public const byte vbKeyNumpad3 = 0x63 ;    //3 键
        public const byte vbKeyNumpad4 = 0x64 ;    //4 键
        public const byte vbKeyNumpad5 = 0x65 ;    //5 键
        public const byte vbKeyNumpad6 = 0x66 ;    //6 键
        public const byte vbKeyNumpad7 = 0x67 ;    //7 键
        public const byte vbKeyNumpad8 = 0x68 ;    //8 键
        public const byte vbKeyNumpad9 = 0x69 ;    //9 键
        public const byte vbKeyMultiply = 0x6A ;   // MULTIPLICATIONSIGN(*)键
        public const byte vbKeyAdd = 0x6B ;        // PLUS SIGN(+) 键
        public const byte vbKeySeparator = 0x6C ;  // ENTER 键
        public const byte vbKeySubtract = 0x6D ;   // MINUS SIGN(-) 键
        public const byte vbKeyDecimal = 0x6E ;    // DECIMAL POINT(.) 键
        public const byte vbKeyDivide = 0x6F ;     // DIVISION SIGN(/) 键


        //F1到F12按键
        public const byte vbKeyF1 = 0x70 ;   //F1 键
        public const byte vbKeyF2 = 0x71 ;   //F2 键
        public const byte vbKeyF3 = 0x72 ;   //F3 键
        public const byte vbKeyF4 = 0x73 ;   //F4 键
        public const byte vbKeyF5 = 0x74 ;   //F5 键
        public const byte vbKeyF6 = 0x75 ;   //F6 键
        public const byte vbKeyF7 = 0x76 ;   //F7 键
        public const byte vbKeyF8 = 0x77 ;   //F8 键
        public const byte vbKeyF9 = 0x78 ;   //F9 键
        public const byte vbKeyF10 = 0x79 ;  //F10 键
        public const byte vbKeyF11 = 0x7A ;  //F11 键
        public const byte vbKeyF12 = 0x7B ;  //F12 键

        #endregion

  • 3
    点赞
  • 30
    收藏
    觉得还不错? 一键收藏
  • 3
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值