2011-09-11 33 views
0

我试图自动动态显示对话框。 我需要传递文本到它的文本字段,然后按下一个按钮。 到目前为止我尝试过的。与使用句柄C的表单进行交互#

[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 

Source

但是当我尝试投它来控制我得到空。

foreach (IntPtr i in childWindows) 
{ 
    Control c = Control.FromHandle(i); 
} 

所以任何机构可以告诉什么是wrong.I我假设我将投手柄来控制,然后用控件属性交互(例如:文本)。

+2

您是否考虑过使用UI自动化框架(如White)来隐藏处理Windows消息的复杂性? (http://code.google.com/p/white-project/)你可以在这里找到一个例子:http://scip.be/index.php?Page=ArticlesNET19&Lang=NL –

+0

我try.I得到例外。尝试投射时无法从Castle.Proxies.Win32ComboBoxProxy投射。 –

回答

2

enter image description here

我一直在使用这样的代码成功地为年内执行单点登录,以提示用户输入其用户名/密码/域的应用程序。唯一要注意的是你需要知道你所针对的对话框的控制结构,但这很容易用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 
+0

Thanks JimStat.I正在尝试选择组合框的值。所以不是选择组合框的值。我试图设置组合框的文本(我的文件路径)。 我正在尝试下面的代码。 SendMessage(ws3,0x000C,0,“我的文件路径”); // ws3是组合框的句柄。 你能弄清楚我错过了什么吗? –

+0

可汗 - 我相信你需要将WM_SETTEXT改为ComboBox控件的子元素“Edit”控件。查看我添加到答案的屏幕截图。 – JimSTAT

+0

因此,在屏幕截图中我添加了答案,您希望使用组合框句柄(001B1174)作为起点获取第一个“编辑”控件(000511CA)的句柄。你可以使用FindWindowEx或GetWindow和GW_CHILD参数来完成。最后你会设置文本类似“SendMessage(hEditControl,WM_SETTEXT,”我的文件路径“)。Fyi - WM_SETTEXT = 0x000C – JimSTAT

0

Control.FromHandle只能在您的流程中由Control子孙实施的控件中工作。我猜这个窗口在你的过程之外。

您需要使用Win32 API方法对其进行修改。