UIAutomation比API的优点是类似于消息处理机制,而不是主要靠模拟鼠标键盘发送消息

首先添加引用UIAutomationClient和UIAutomationTypes,在安装.net3.5的电脑上可用。低版本的VS可以直接引用dll。

using System.Windows.Automation;

//先用传统方法找到hwnd,用UI也能找到,新项目应统一到UI

IntPtr leftview = API.FindWindowEx(wd0, 0, "SysListView32", "CFQS");

IntPtr rightview = API.FindWindowEx(wd0, 0, "SysListView32", "List2");

if (leftview == IntPtr.Zero || rightview == IntPtr.Zero) return;

AutomationElement el = AutomationElement.FromHandle(leftview); //左侧view

AutomationElement er = AutomationElement.FromHandle(rightview); //右侧view

PropertyCondition condition_bk = new PropertyCondition(AutomationElement.ControlTypeProperty, ControlType.DataItem); //查找子窗口的条件 同样是ListViewItem,对左边的只能用ControlType.DataItem,右边的用ControlType.ListItem

AutomationElementCollection bks = el.FindAll(TreeScope.Children, condition_bk); //获得所有满足条件子窗口的集合

//还有一种遍历子窗口的方法 用TreeWalker

//AutomationElement el = AutomationElement.FromHandle(leftview);

//TreeWalker walker = TreeWalker.ContentViewWalker;

//for (AutomationElement child = walker.GetFirstChild(el); child != null; child = walker.GetNextSibling(child))                    //{

//    string name=child.Current.Name

//}

API.mouse_event((int)(MouseEventFlags.Wheel | MouseEventFlags.Absolute), 0, 0, -380, IntPtr.Zero); //让滚动条下滚,仅为视觉效果,不要也行

SelectionItemPattern selPattern = (SelectionItemPattern)item.GetCurrentPattern(SelectionItemPattern.Pattern);

selPattern.Select(); //选择该板块 //实现ListView内部元素选择

// item.Current.Name 可取出Listview内部元素内容

//PropertyCondition condition = new PropertyCondition(AutomationElement.IsEnabledProperty, true); //可通用所有非禁用项作为类型

2016.9.2补充

往控件中输入内容

AutomationElement element = AutomationElement.FromHandle(edbox);

object valuePattern = null;

if (element.TryGetCurrentPattern(ValuePattern.Pattern, out valuePattern)) //是否支持直接写入

{

// Set focus for input functionality and begin.

element.SetFocus();

((ValuePattern)valuePattern).SetValue(fullpath);

}

else //否则模拟键入

{

// Set focus for input functionality and begin.

element.SetFocus();

// Pause before sending keyboard input.

Thread.Sleep(100);

// Delete existing content in the control and insert new content.

SendKeys.SendWait("^{HOME}");   // Move to start of control

SendKeys.SendWait("^+{END}");   // Select everything

SendKeys.SendWait("{DEL}");     // Delete selection

SendKeys.SendWait(fullpath);

}

2015.4.25利用UIAutomation 替代API函数,解决了ListView无法读数据的难题,顺便实现了鼠标模拟滚轮的更多相关文章

  1. linux 中的进程wait()和waitpid函数,僵尸进程详解,以及利用这两个函数解决进程同步问题

    转载自:http://blog.sina.com.cn/s/blog_7776b9d3010144f9.html 在UNIX 系统中,一个进程结束了,但是他的父进程没有等待(调用wait / wait ...

  2. 12 —— node 获取文件属性 —— 利用 自调用 闭包函数 解决 i 丢失的问题

    闭包的作用 : 保存变量 一,i 丢失的案例 var arr = ['node','vue','mysql'] for(var i=0;i<arr.length;i++){ setTimeout ...

  3. C# 窗体常用API函数 应用程序窗体查找

    常用的处理窗体的API函数如下(注意:API函数必须放在窗体中...): 使用C#语言,要引用DllImport,必须要添加using System.Runtime.InteropServices命名 ...

  4. mfc 调用Windows的API函数实现同步异步串口通信(源码)

    在工业控制中,工控机(一般都基于Windows平台)经常需要与智能仪表通过串口进行通信.串口通信方便易行,应用广泛. 一般情况下,工控机和各智能仪表通过RS485总线进行通信.RS485的通信方式是半 ...

  5. Detours简介 (拦截x86机器上的任意的win32 API函数)

    Detours 当然是用detours,微软明显高腾讯一筹,同上,至今没失败过.写这种HOOK一定要再写个测试程序,不要直接HOOK你的目的程序,例如QQ,因为这样不方面更灵活的测试.说明一下:Det ...

  6. WINDOWS-API:API函数大全

    操作系统除了协调应用程序的执行.内存分配.系统资源管理外,同时也是一个很大的服务中心,调用这个服务中心的各种服务(每一种服务是一个函数),可以帮肋应用程序达到开启视窗.描绘图形.使用周边设备的目的,由 ...

  7. Windows API 函数列表 附帮助手册

    所有Windows API函数列表,为了方便查询,也为了大家查找,所以整理一下贡献出来了. 帮助手册:700多个Windows API的函数手册 免费下载 API之网络函数 API之消息函数 API之 ...

  8. 初识API函数

    我之前是一个只会编写数值计算的程序的OIer,但我并不甘于这种现状,于是我编写了我的第一个使用API函数的C++程序,开发平台是VS2012: // ConsoleApplication.cpp : ...

  9. C#关于外挂汉化的一些思考(API函数FindWindow,FindWindowEx,SendMessage)(转)

    这次我们试着运用C#的API函数去修改别的程序的标题文本(适用范围C#) 其实这是FindWindow,FindWindowEx,SendMessage的应用举例之一 也就是所谓的外挂汉化. 附:Wi ...

随机推荐

  1. java入门了解03

    ASSCII表 . 1.final关键字    (1)含义:最终的意思,修饰方法,类,变量    (2)特点:          A:修饰类,不能被继承          B:修饰的方法,不能被重载  ...

  2. whois老域名挖掘技术

    我一般通过站长工具域名WHOIS查询定向收集一些特定域名,拿来分析网站存活站点. 例如: 查询域名基本信息 WHOIS反查得到大部分域名注册信息 一般大一点的厂商都有几百个域名,我们通过此处收集大量顶 ...

  3. intellij idea build时出现Artifact contains illegal characters的解决

    此处无法创建是因为Artifact的命名为大小写混合,将大写改为小写即可正常创建

  4. Nginx虚拟主机配置模板

    /////////////////////////////写在前头//////////////////////////////////////////Nginx 服务器中文文档:http://www. ...

  5. Codeforces 888D Almost Identity Permutations:错排公式

    题目链接:http://codeforces.com/problemset/problem/888/D 题意: 给定n,k,问你有多少种1到n的排列,满足至少有n-k个a[i] == i. (4 &l ...

  6. sp_rename sqlserver 表 列 索引 类型重命名

    --[语法] sp_rename [ @objname = ] 'object_name' , [ @newname = ] 'new_name' [ , [ @objtype = ] 'object ...

  7. spring boot: 条件注解@Condition

    @Conditional根据满足某一个特定的条件创建一个特定的Bean(基于条件的Bean的创建,即使用@Conditional注解). 比方说,当一个jar包在一个类的路径下的时候,自动配置一个或多 ...

  8. webservice 交错数组

    net webservices public DataSet SelectOPQuestionByWhere(string strWhere, string[][] strArry) { if (!k ...

  9. mysql备份shell脚步

    #!/bin/bash  #Shell Command For Backup MySQL Database Everyday Automatically By Crontab     USER=roo ...

  10. kylin_异常_02_java.lang.NoClassDefFoundError: org/apache/hadoop/hive/conf/HiveConf 解决办法

    一.异常现象 在kylin的web管理界面,设置hive数据源时,报错: 查找kylin的日志时发现,弹出提示框的原因是因为出现错误: ERROR [http-bio-7070-exec-10] co ...