工具: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. Java读取二进制文件的方式

    public static void readFile(String fileName){ File file = new File(fileName); if(file.exists()){ try ...

  2. Ubuntu伪破解Navicat12方法

    一.去官网下载navicat112_premium_cs_x64 for linux版本二.用tar解压安装包三.navicat解压即可用,直接进入解压后的目录,然后用‘./’运行start_navi ...

  3. linux vps定时备份网站、数据库命令sh

    vps定时备份数据库命令,将下面的代码保存为backsql.sh,然后设置定时任务运行即可. cd /home/admin/backup #切换到备份目录 btimes=$(date +%y%m%d% ...

  4. Java并发编程实战 第6章 任务并行 第7章 取消与关闭

    ExecutorCompletionService CompletionService用来接收一个Executor的执行结果,将已经完成任务,放置在可使用 take 访问的队列上. 大概用法: Exe ...

  5. springboot-mybatis配置(xml)/springboot-jpa配置

    #springboot-mybatis配置(xml) spring.datasource.url=jdbc:mysql://localhost:3306/test spring.datasource. ...

  6. Luogu P2595 [ZJOI2009]多米诺骨牌 容斥,枚举,插头dp,轮廓线dp

    真的是个好(毒)题(瘤).其中枚举的思想尤其值得借鉴. \(40pts\):插头\(dp\),记录插头的同时记录每一列的连接状况,复杂度\(O(N*M*2^{n + m} )\). \(100pts\ ...

  7. flask+gevent的异步框架

    一:flask本身的框架时什么? 基于Wsgi的Web应用框架 二:为什么要实现异步架构? 增加并发处理能力 三:实现异步架构 from gevent import monkey from geven ...

  8. 在vue-cli中使用px2rem,配合lib-flexible使用

    原文地址 附上github源码  看这里 1.下载lib-flexible npm安装 npm i lib-flexible --save 2.引入lib-flexible 在main.js中引入li ...

  9. Gym-100923I-Por Costel and the Pairs(数学,思维)

    链接: https://vjudge.net/problem/Gym-100923I 题意: We don't know how Por Costel the pig arrived at FMI's ...

  10. [每日一讲] Python系列:列表与元组

    参考文档 https://docs.python.org/zh-cn/3.7/tutorial/introduction.html#lists """ DATA STRU ...