工具: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. RHEL7使用systemctl管理服务

    1.   systemctl命令 说明 systemctl    start   [unit type] 启动服务 systemctl    stop   [unit type] 停止服务 syste ...

  2. 006-Zabbix agent on Zabbix server is unreachable for 5 minutes

    环境描述:        环境介绍:CentOS6.5   zabbix3.2.6(zabbix客户端与服务端在一台主机)   1.在安装完zabbix之后,添加客户端,客户端配置(zabbix_ag ...

  3. Codeforces Round #575 (Div. 3) E. Connected Component on a Chessboard(思维,构造)

    E. Connected Component on a Chessboard time limit per test2 seconds memory limit per test256 megabyt ...

  4. Binary Numbers AND Sum CodeForces - 1066E (前缀和)

    You are given two huge binary integer numbers aa and bb of lengths nn and mmrespectively. You will r ...

  5. faiss的简单使用

    简介 faiss是为稠密向量提供高效相似度搜索和聚类的框架.由Facebook AI Research研发. 具有以下特性. 1.提供多种检索方法 2.速度快 3.可存在内存和磁盘中 4.C++实现, ...

  6. Nginx 502 Bad Gateway 的错误的解决方案

    我用的是nginx反向代理Apache,直接用Apache不会有任何问题,加上nginx就会有部分ajax请求502的错误,下面是我收集到的解决方案. 一.fastcgi缓冲区设置过小 出现错误,首先 ...

  7. MyEclipse使用教程:使用主题自定义工作台外观

    [MyEclipse CI 2019.4.0安装包下载] 您可以通过选择Window>Preferences>General>Appearance来使用主题自定义的工作台外观,可以从 ...

  8. 一、redis安装、配置、命令

    一.安装 参见:redis中文官方文档 “http://www.redis.cn/download.html” 下载.解压.编译Redis $ wget http://download.redis.i ...

  9. Css min-height max-height min-width max-height

    Css min-height应用地方解释我们有时设置一个对象盒子时候避免对象没有内容时候不能撑开,但内容多少不能确定所以又不能固定高度,这个时候我们就会需要css来设置min-height最小高度撑高 ...

  10. 【NOIP2016提高A组模拟10.15】算循环

    题目 分析 一步步删掉循环, 首先,原式是\[\sum_{i=1}^n\sum_{j=1}^m\sum_{k=i}^n\sum_{l=j}^m\sum_{p=i}^k\sum_{q=j}^l1\] 删 ...