C# user32.dll 找窗口 填数据
工具:SpyLite
1 [DllImport("user32.dll", EntryPoint = "FindWindow")] 2 private extern static IntPtr FindWindow(string lpClassName, string lpWindowName);//常用来查找最上级窗口 3 4 [DllImport("user32.dll", EntryPoint = "FindWindowEx")] 5 private static extern IntPtr FindWindowEx(IntPtr hwndParent, IntPtr hwndChildAfter, string lpszClass, string lpszWindow);//最适合使用【按类型查找】来找到目标填充栏 6 7 [DllImport("user32.dll", EntryPoint = "SendMessageA")] 8 private static extern int SendMessageA(IntPtr hwnd, int wMsg, IntPtr wParam, string lParam);//向目标填充栏 填充文本(有时候目标有【仅数字】之类的限制,这时候发不符合的文本就不行了) 9 10 [DllImport("user32.dll", SetLastError = true)] 11 static extern IntPtr GetWindow(IntPtr hWnd, GetWindow_Cmd uCmd);//类似树结构的查找方式,实测中GW_HWNDNEXT经常找不着,鼠标划过一下目标,又找着了...... 12 const int WM_SETTEXT = 0x000C; 13 enum GetWindow_Cmd : uint 14 { 15 GW_HWNDFIRST = 0, 16 GW_HWNDLAST = 1, 17 GW_HWNDNEXT = 2, 18 GW_HWNDPREV = 3, 19 GW_OWNER = 4, 20 GW_CHILD = 5, 21 GW_ENABLEDPOPUP = 6 22 }
1 string msg = ""; 2 IntPtr hMainWnd = FindWindow(null, "父窗口名");//此处有待修改,父窗口类型和标题,那个唯一的可能性更大就用哪个 3 IntPtr hNextWnd; 4 IntPtr hLastWnd; 5 if (hMainWnd != IntPtr.Zero) 6 { 7 8 //子 hKPfameWnd 9 hNextWnd = FindWindowEx(hMainWnd, IntPtr.Zero, null, "FPtiankai_new"); 10 if (IntPtr.Zero == hNextWnd) 11 { 12 msg = "SetValueGolden = ::FindWindowEx-1没找到"; 13 14 return msg; 15 16 } 17 hLastWnd = hNextWnd; 18 19 //向下找两层 20 for (int i = 0; i < 2; i++) 21 { 22 hNextWnd = FindWindowEx(hLastWnd, IntPtr.Zero, "WindowsForms10.Window.8.app.0.3598b65_r16_ad1", null); 23 if (IntPtr.Zero == hNextWnd) 24 { 25 msg = "SetValueGolden = ::FindWindowEx-2没找到"; 26 27 return msg; 28 } 29 hLastWnd = hNextWnd; 30 } 31 32 hNextWnd = GetWindow(hLastWnd, GetWindow_Cmd.GW_CHILD);//返回其第一个子窗体 33 if (IntPtr.Zero == hNextWnd) 34 { 35 msg = "SetValueGolden = ::FindWindowEx-3没找到"; 36 return msg; 37 } 38 hLastWnd = hNextWnd; 39 40 /*此时的窗口布局如下 41 3541882 WindowsForms10.STATIC.app.0.3598b65_r16_ad1 标题:北京 这是刚刚找到的 42 4853516 WindowsForms10.STATIC.app.0.3598b65_r16_ad1 标题:北 京 43 1513288 WindowsForms10.STATIC.app.0.3598b65_r16_ad1 标题:ddd 44 4919418 WindowsForms10.Window.8.app.0.3598b65_r16_ad1 标题: 45 4000606 WindowsForms10.Window.8.app.0.3598b65_r16_ad1 标题: 46 1443834 WindowsForms10.EDIT.app.0.3598b65_r16_ad1 标题: 47 1181678 WindowsForms10.STATIC.app.0.3598b65_r16_ad1 标题:¥0.00 48 22088970 WindowsForms10.STATIC.app.0.3598b65_r16_ad1 标题:零圆整 49 10817716 WindowsForms10.STATIC.app.0.3598b65_r16_ad1 标题:¥0.00 50 1247228 WindowsForms10.STATIC.app.0.3598b65_r16_ad1 标题:¥0.00 51 5969752 WindowsForms10.STATIC.app.0.3598b65_r16_ad1 标题:2017年08月08日 52 10425028 WindowsForms10.STATIC.app.0.3598b65_r16_ad1 标题:08919001 53 920118 WindowsForms10.STATIC.app.0.3598b65_r16_ad1 标题:1100164320 54 4852040 WindowsForms10.Window.8.app.0.3598b65_r16_ad1 标题: 55 788912 WindowsForms10.Window.8.app.0.3598b65_r16_ad1 标题: 56 1706660 WindowsForms10.Window.8.app.0.3598b65_r16_ad1 标题: 57 5179046 WindowsForms10.Window.8.app.0.3598b65_r16_ad1 标题: 58 3018896 WindowsForms10.Window.8.app.0.3598b65_r16_ad1 标题: 59 5444734 WindowsForms10.Window.8.app.0.3598b65_r16_ad1 标题: 子窗口——WindowsForms10.EDIT.app.0.3598b65_r16_ad1——目标【1】 60 24187592 WindowsForms10.Window.8.app.0.3598b65_r16_ad1 标题: 同上——目标【2】 61 985234 WindowsForms10.Window.8.app.0.3598b65_r16_ad1 标题: 同上——目标【3】 62 5965524 WindowsForms10.Window.8.app.0.3598b65_r16_ad1 标题: 同上——目标【4】 63 */ 64 65 //同一层找接下来18个控件 66 for (int i = 0; i < 18; i++) 67 { 68 69 hNextWnd = GetWindow(hLastWnd, GetWindow_Cmd.GW_HWNDNEXT);//找的是NEXT 70 if (IntPtr.Zero == hNextWnd) 71 { 72 msg = "SetValueGolden = ::FindWindowEx-i="+i+"没找到"; 73 return msg; 74 } 75 hLastWnd = hNextWnd; 76 } 77 //【hLastWnd不更新 】 78 79 80 //目标1 81 hNextWnd = FindWindowEx(hLastWnd, IntPtr.Zero, "WindowsForms10.EDIT.app.0.3598b65_r16_ad1", null); 82 if (IntPtr.Zero == hNextWnd) 83 { 84 msg = "SetValueGolden = ::FindWindowEx-目标1"; 85 return msg; 86 } 87 SendMessageA(hNextWnd, WM_SETTEXT, IntPtr.Zero, info.Bank + " " + info.BankAccount); 88 89 90 //hLastWnd再更新 91 hNextWnd = GetWindow(hLastWnd, GetWindow_Cmd.GW_HWNDNEXT); 92 if (IntPtr.Zero == hNextWnd) 93 { 94 msg = "SetValueGolden = ::FindWindowEx-4没找到"; 95 return msg; 96 } 97 hLastWnd = hNextWnd; 98 //目标2 99 hNextWnd = FindWindowEx(hLastWnd, IntPtr.Zero, "WindowsForms10.EDIT.app.0.3598b65_r16_ad1", null); 100 if (IntPtr.Zero == hNextWnd) 101 { 102 msg = "SetValueGolden = ::FindWindowEx-目标2"; 103 return msg; 104 } 105 SendMessageA(hNextWnd, WM_SETTEXT, IntPtr.Zero, info.TaxpayerCode); 106 107 //hLastWnd再更新 108 hNextWnd = GetWindow(hLastWnd, GetWindow_Cmd.GW_HWNDNEXT); 109 if (IntPtr.Zero == hNextWnd) 110 { 111 msg = "SetValueGolden = ::FindWindowEx-5没找到"; 112 return msg; 113 } 114 hLastWnd = hNextWnd; 115 //目标3 116 hNextWnd = FindWindowEx(hLastWnd, IntPtr.Zero, "WindowsForms10.EDIT.app.0.3598b65_r16_ad1", null); 117 if (IntPtr.Zero == hNextWnd) 118 { 119 msg = "SetValueGolden = ::FindWindowEx-目标3"; 120 return msg; 121 } 122 SendMessageA(hNextWnd, WM_SETTEXT, IntPtr.Zero, info.Address + " " + info.Telephone); 123 124 //hLastWnd再更新 125 hNextWnd = GetWindow(hLastWnd, GetWindow_Cmd.GW_HWNDNEXT); 126 if (IntPtr.Zero == hNextWnd) 127 { 128 msg = "SetValueGolden = ::FindWindowEx-6没找到"; 129 return msg; 130 } 131 hLastWnd = hNextWnd; 132 //目标4 133 hNextWnd = FindWindowEx(hLastWnd, IntPtr.Zero, "WindowsForms10.EDIT.app.0.3598b65_r16_ad1", null); 134 if (IntPtr.Zero == hNextWnd) 135 { 136 msg = "SetValueGolden = ::FindWindowEx-目标4"; 137 return msg; 138 } 139 SendMessageA(hNextWnd, WM_SETTEXT, IntPtr.Zero, info.TaxpayerName); 140 141 142 } 143 else 144 { 145 msg = "没有找到目标软件"; 146 return msg; 147 }
实测:FindWindowEx 比 GetWindow靠谱,查找文本框等需要填充的控件,尽量使用FindWindowEx按窗体类型查找