我正在尝试自动化动态显示的对话框。 我需要将文本传递给它,文本字段,然后按一下按钮。 到目前为止我尝试过的。
[DllImport("user32.dll", EntryPoint = "FindWindow", SetLastError = true)]
static extern IntPtr FindWindowByCaption(IntPtr ZeroOnly, string lpWindowName);
IntPtr handle= FindWindowByCaption(System.IntPtr.Zero, "Caption Of File");
我正确掌握了对话框的句柄。
List<IntPtr> childWindows= GetChildWindows(handle);//To get the child controls in this dialogue box
但是当我尝试将其转换为控制时,我会得到null。
foreach (IntPtr i in childWindows)
{
Control c = Control.FromHandle(i);
}
所以任何身体都可以告诉我什么是错的。我想我会把握来控制然后与控制属性交互(例如:文本)。
答案 0 :(得分:2)
我多年来一直使用这样的代码来执行单点登录到提示用户输入用户名/密码/域的应用程序。唯一的警告是你需要知道你所针对的对话框的控制结构,但这很容易通过Spy ++实现,很少改变。当然,您需要为窗口的控件结构修改此代码。
[DllImport("user32.dll")]
static extern IntPtr FindWindowEx(IntPtr hwndParent, IntPtr hwndChildAfter,
string lpszClass, string lpszWindow);
[DllImport("user32.dll", CharSet = CharSet.Auto)]
public static extern IntPtr SendMessage(IntPtr hWnd, uint msg, int wParam, string lParam);
[DllImport("User32.Dll")]
public static extern IntPtr PostMessage(IntPtr hWnd, uint msg, int wParam, int lParam);
private const uint WM_GETTEXTLENGTH = 0x000E;
private const uint WM_SETTEXT = 0x000C;
private const uint WM_GETTEXT = 0x000D;
private const uint BM_CLICK = 0x00F5;
private const uint WM_CLOSE = 0x0010;
enum GetWindow_Cmd : uint
{
GW_HWNDFIRST = 0,
GW_HWNDLAST = 1,
GW_HWNDNEXT = 2,
GW_HWNDPREV = 3,
GW_OWNER = 4,
GW_CHILD = 5,
GW_ENABLEDPOPUP = 6
}
var dialog FindWindow("optionalClassNameHere", "Log On"); //Get the handle of the window
var w3 = GetWindow(dialog , (uint)GetWindow_Cmd.GW_CHILD); //I use GetWindow to walk the window controls
var wUid = FindWindowEx(w3, IntPtr.Zero, "Edit", "");
var w4 = GetWindow(wUid, (uint)GetWindow_Cmd.GW_HWNDNEXT);
var wPwd = FindWindowEx(w4 , IntPtr.Zero, "Edit", "");
var wOK = FindWindowEx(w3, IntPtr.Zero, "Button", "OK");
SendMessage(wUid, WM_SETTEXT, 0, _WinDomain + "\\" + Username); //Send username to username edit control
SendMessage(wPwd, WM_SETTEXT, 0, Password); //Send password to password edit control
PostMessage(wOK, BM_CLICK, 0, 0); //Send left click(0x00f5) to OK button
答案 1 :(得分:0)
Control.FromHandle只能用于您的进程中由Control后代实现的控件。我猜这个窗口不在你的过程中。
您需要使用Win32 API方法对其进行修改。