工具:SpyLite

    
 [DllImport("user32.dll", EntryPoint = "FindWindow")]
private extern static IntPtr FindWindow(string lpClassName, string lpWindowName);//常用来查找最上级窗口 [DllImport("user32.dll", EntryPoint = "FindWindowEx")]
private static extern IntPtr FindWindowEx(IntPtr hwndParent, IntPtr hwndChildAfter, string lpszClass, string lpszWindow);//最适合使用【按类型查找】来找到目标填充栏 [DllImport("user32.dll", EntryPoint = "SendMessageA")]
private static extern int SendMessageA(IntPtr hwnd, int wMsg, IntPtr wParam, string lParam);//向目标填充栏 填充文本(有时候目标有【仅数字】之类的限制,这时候发不符合的文本就不行了) [DllImport("user32.dll", SetLastError = true)]
static extern IntPtr GetWindow(IntPtr hWnd, GetWindow_Cmd uCmd);//类似树结构的查找方式,实测中GW_HWNDNEXT经常找不着,鼠标划过一下目标,又找着了......
const int WM_SETTEXT = 0x000C;
enum GetWindow_Cmd : uint
{
GW_HWNDFIRST = ,
GW_HWNDLAST = ,
GW_HWNDNEXT = ,
GW_HWNDPREV = ,
GW_OWNER = ,
GW_CHILD = ,
GW_ENABLEDPOPUP =
}

  

 string msg = "";
IntPtr hMainWnd = FindWindow(null, "父窗口名");//此处有待修改,父窗口类型和标题,那个唯一的可能性更大就用哪个
IntPtr hNextWnd;
IntPtr hLastWnd;
if (hMainWnd != IntPtr.Zero)
{ //子 hKPfameWnd
hNextWnd = FindWindowEx(hMainWnd, IntPtr.Zero, null, "FPtiankai_new");
if (IntPtr.Zero == hNextWnd)
{
msg = "SetValueGolden = ::FindWindowEx-1没找到"; return msg; }
hLastWnd = hNextWnd; //向下找两层
for (int i = ; i < ; i++)
{
hNextWnd = FindWindowEx(hLastWnd, IntPtr.Zero, "WindowsForms10.Window.8.app.0.3598b65_r16_ad1", null);
if (IntPtr.Zero == hNextWnd)
{
msg = "SetValueGolden = ::FindWindowEx-2没找到"; return msg;
}
hLastWnd = hNextWnd;
} hNextWnd = GetWindow(hLastWnd, GetWindow_Cmd.GW_CHILD);//返回其第一个子窗体
if (IntPtr.Zero == hNextWnd)
{
msg = "SetValueGolden = ::FindWindowEx-3没找到";
return msg;
}
hLastWnd = hNextWnd; /*此时的窗口布局如下
3541882 WindowsForms10.STATIC.app.0.3598b65_r16_ad1 标题:北京 这是刚刚找到的
4853516 WindowsForms10.STATIC.app.0.3598b65_r16_ad1 标题:北 京
1513288 WindowsForms10.STATIC.app.0.3598b65_r16_ad1 标题:ddd
4919418 WindowsForms10.Window.8.app.0.3598b65_r16_ad1 标题:
4000606 WindowsForms10.Window.8.app.0.3598b65_r16_ad1 标题:
1443834 WindowsForms10.EDIT.app.0.3598b65_r16_ad1 标题:
1181678 WindowsForms10.STATIC.app.0.3598b65_r16_ad1 标题:¥0.00
22088970 WindowsForms10.STATIC.app.0.3598b65_r16_ad1 标题:零圆整
10817716 WindowsForms10.STATIC.app.0.3598b65_r16_ad1 标题:¥0.00
1247228 WindowsForms10.STATIC.app.0.3598b65_r16_ad1 标题:¥0.00
5969752 WindowsForms10.STATIC.app.0.3598b65_r16_ad1 标题:2017年08月08日
10425028 WindowsForms10.STATIC.app.0.3598b65_r16_ad1 标题:08919001
920118 WindowsForms10.STATIC.app.0.3598b65_r16_ad1 标题:1100164320
4852040 WindowsForms10.Window.8.app.0.3598b65_r16_ad1 标题:
788912 WindowsForms10.Window.8.app.0.3598b65_r16_ad1 标题:
1706660 WindowsForms10.Window.8.app.0.3598b65_r16_ad1 标题:
5179046 WindowsForms10.Window.8.app.0.3598b65_r16_ad1 标题:
3018896 WindowsForms10.Window.8.app.0.3598b65_r16_ad1 标题:
5444734 WindowsForms10.Window.8.app.0.3598b65_r16_ad1 标题: 子窗口——WindowsForms10.EDIT.app.0.3598b65_r16_ad1——目标【1】
24187592 WindowsForms10.Window.8.app.0.3598b65_r16_ad1 标题: 同上——目标【2】
985234 WindowsForms10.Window.8.app.0.3598b65_r16_ad1 标题: 同上——目标【3】
5965524 WindowsForms10.Window.8.app.0.3598b65_r16_ad1 标题: 同上——目标【4】
*/ //同一层找接下来18个控件
for (int i = ; i < ; i++)
{ hNextWnd = GetWindow(hLastWnd, GetWindow_Cmd.GW_HWNDNEXT);//找的是NEXT
if (IntPtr.Zero == hNextWnd)
{
msg = "SetValueGolden = ::FindWindowEx-i="+i+"没找到";
return msg;
}
hLastWnd = hNextWnd;
}
//【hLastWnd不更新 】 //目标1
hNextWnd = FindWindowEx(hLastWnd, IntPtr.Zero, "WindowsForms10.EDIT.app.0.3598b65_r16_ad1", null);
if (IntPtr.Zero == hNextWnd)
{
msg = "SetValueGolden = ::FindWindowEx-目标1";
return msg;
}
SendMessageA(hNextWnd, WM_SETTEXT, IntPtr.Zero, info.Bank + " " + info.BankAccount); //hLastWnd再更新
hNextWnd = GetWindow(hLastWnd, GetWindow_Cmd.GW_HWNDNEXT);
if (IntPtr.Zero == hNextWnd)
{
msg = "SetValueGolden = ::FindWindowEx-4没找到";
return msg;
}
hLastWnd = hNextWnd;
//目标2
hNextWnd = FindWindowEx(hLastWnd, IntPtr.Zero, "WindowsForms10.EDIT.app.0.3598b65_r16_ad1", null);
if (IntPtr.Zero == hNextWnd)
{
msg = "SetValueGolden = ::FindWindowEx-目标2";
return msg;
}
SendMessageA(hNextWnd, WM_SETTEXT, IntPtr.Zero, info.TaxpayerCode); //hLastWnd再更新
hNextWnd = GetWindow(hLastWnd, GetWindow_Cmd.GW_HWNDNEXT);
if (IntPtr.Zero == hNextWnd)
{
msg = "SetValueGolden = ::FindWindowEx-5没找到";
return msg;
}
hLastWnd = hNextWnd;
//目标3
hNextWnd = FindWindowEx(hLastWnd, IntPtr.Zero, "WindowsForms10.EDIT.app.0.3598b65_r16_ad1", null);
if (IntPtr.Zero == hNextWnd)
{
msg = "SetValueGolden = ::FindWindowEx-目标3";
return msg;
}
SendMessageA(hNextWnd, WM_SETTEXT, IntPtr.Zero, info.Address + " " + info.Telephone); //hLastWnd再更新
hNextWnd = GetWindow(hLastWnd, GetWindow_Cmd.GW_HWNDNEXT);
if (IntPtr.Zero == hNextWnd)
{
msg = "SetValueGolden = ::FindWindowEx-6没找到";
return msg;
}
hLastWnd = hNextWnd;
//目标4
hNextWnd = FindWindowEx(hLastWnd, IntPtr.Zero, "WindowsForms10.EDIT.app.0.3598b65_r16_ad1", null);
if (IntPtr.Zero == hNextWnd)
{
msg = "SetValueGolden = ::FindWindowEx-目标4";
return msg;
}
SendMessageA(hNextWnd, WM_SETTEXT, IntPtr.Zero, info.TaxpayerName); }
else
{
msg = "没有找到目标软件";
return msg;
}

实测:FindWindowEx 比 GetWindow靠谱,查找文本框等需要填充的控件,尽量使用FindWindowEx按窗体类型查找

C# user32.dll 找窗口 填数据的更多相关文章

  1. C# user32.dll找窗口时,使用GetClass方法解决 【带有系统自动编译的窗体类后缀名】 问题

    [DllImport("user32.dll", SetLastError = true, CharSet = CharSet.Auto)] static extern int G ...

  2. 调用user32.dll显示其他窗口

    /// 该函数设置由不同线程产生的窗口的显示状态 /// </summary> /// <param name="hWnd">窗口句柄</param& ...

  3. C#中可直接调用WIN32的API函数--USER32.DLL

    Win32的API函数可以直接在C#中直接调用,在做WinForm时还是很有帮助的.有时候直接调用Win32的API,可以很高效的实现想要的效果. using System; using System ...

  4. Winform API "user32.dll"中的函数

    命名空间:System.Runtime.InteropServices /// <summary> /// 该函数检索一指定窗口的客户区域或整个屏幕的显示设备上下文环境的句柄,以后可以在G ...

  5. 【转】c# 调用windows API(user32.dll)

    using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.R ...

  6. user32.dll

    user32.dll中的所有函数 using System; using System.Collections.Generic; using System.Linq; using System.Tex ...

  7. 【WinAPI】User32.dll注释

    #region User32.dll 函数 /// <summary> /// 该函数检索一指定窗口的客户区域或整个屏幕的显示设备上下文环境的句柄,以后可以在GDI函数中使用该句柄来在设备 ...

  8. 【整理】c# 调用windows API(user32.dll)

    User32.dll提供了很多可供调用的接口,大致如下(转自http://blog.csdn.net/zhang399401/article/details/6978803) using System ...

  9. C# user32.dll

    #region User32.dll 函数 /// <summary> /// 该函数检索一指定窗口的客户区域或整个屏幕的显示设备上下文环境的句柄,以后可以在GDI函数中使用该句柄来在设备 ...

随机推荐

  1. 简单Delphi程序设计

  2. docker inspect命令查看镜像详细信息

    使用 inspect 命令查看镜像详细信息,包括制作者.适应架构.各层的数字摘要等. # docker inspect --help Usage: docker inspect [OPTIONS] N ...

  3. 007-zabbix Server 4.0 监控TCP的12种状态

    大家对TCP三次握手比较熟悉了,都知道当发生DOSS攻击时,客户端发送SYN给服务端后,服务端响应SYN+ACK,此时客户端就不回应服务端ACK啦(如果正常建立三次握手客户端会回应ACK,表示三次握手 ...

  4. JVM内存溢出处理方法

    OOM(Out of Memory)异常常见有以下几个原因: 1)老年代内存不足:java.lang.OutOfMemoryError:Javaheapspace 2)永久代内存不足:java.lan ...

  5. 利用zookeeper部署kafka集群

    1.准备工作: iptables -F      #关闭防火墙 systemctl stop firewalld.service #关闭防火墙 准备三台虚拟机并放入/etc/hosts下 192.16 ...

  6. C++打开属性对话框并保持其处于打开状态

    #include <Windows.h> #include <shlobj_core.h> #pragma comment(lib,"Shell32.lib" ...

  7. TJOI2017DNA

    P3763 [TJOI2017]DNA 字符串匹配,字符集大小为\(4\),认为相差不超过\(3\)即合法. 对每一种字符分开考虑不同产生的贡献. 对于串\(S\),如果当前位置相同则\(S_i=1\ ...

  8. oracle查询语句执行顺序

    完整的查询语句类似是这样的: select ..., ROWNUM from table where <where clause> group by <columns> hav ...

  9. 【leetcode】Global and Local Inversions

    题目如下: We have some permutation A of [0, 1, ..., N - 1], where N is the length of A. The number of (g ...

  10. JS中for循环嵌套

    for 循环 for语句也是一种前测试循环语句,但它具有在 执行循环之前初始化变量 和 定义循环后要执行的代码的能力 for循环中专门提供了位置来放置循环的三个表达式 定义一个循环需要做实现这三步: ...