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. R语言笔记003——set.seed()函数

    set.seed()函数 set.seed()设定生成随机数的种子,让样本可重复. > x<-rnorm() # 生成4个随机数 > x [] 0.6599492 0.5881863 ...

  2. Qt 自定义PushButton

    http://blog.csdn.net/zddblog/article/details/11116191 功能:鼠标弹起并在按键区域内时,按键响应.并实现normal.hover.pressed效果 ...

  3. git终端配置颜色

    默认情况下git是黑白的. git config --global color.status auto git config --global color.diff auto git config - ...

  4. DelphiXE_画图

    1.基本 DelphiXE FireMonkey 如何画图 http://www.delphitop.com/html/FireMonkey/2647.html 2. 3.

  5. aodh M版本新特性 - Remove eventlet from Aodh in favour of threaded approach

    在 Kilo版本, API WSGI application 可以有以下2种部署方式: As a Python command that runs a Werkzeug-based web serve ...

  6. Codeforces 869C The Intriguing Obsession:组合数 or dp

    题目链接:http://codeforces.com/problemset/problem/869/C 题意: 红色.蓝色.紫色的小岛分别有a,b,c个. 你可以在两个不同的岛之间架桥,桥的长度为1. ...

  7. 安全清空废纸篓mac

    在.bash_profile中加入: alias secureempty='sudo srm -rfvsz /Volumes/*/.Trashes/* ~/.Trash/*' 然后做终端输入:secu ...

  8. c++中对齐方式

    使用cout<<setiosflags(ios::right)以后, 再调用 cout<<setiosflags(ios::left);无效的, 你可以在调用之前加上一句 co ...

  9. Android中如何禁止音量调节至静音

    Android中音量按键在调低音量时,如果一直按住Down按钮不放,则系统将音量跳到最小后,又自动调节到静音状态.这个机制和iPhone是不同的,iPhone中无论你怎么按Volume-按钮,只能调到 ...

  10. 遍历Newtonsoft.Json.Linq.JObject

    JObject 遍历: 引用命名空间:using Newtonsoft.Json.Linq; JObject _jObject = JObject.Parse("{'ID':'001','M ...