首页 文章

user32.dll FindWindowEx,在远程WPF窗口上按类名查找元素

提问于
浏览
0

我有一个从命令行应用程序启动的WPF应用程序 .

我正在尝试做一些简单的自动化(获取/设置文本,单击某些按钮等) . 我似乎无法在WPF中找到任何子窗口 .

我有WPF和UIA Framework,WinForms和WinAPI的工作模型,但似乎无法让WinAPI和WPF很好地发挥 .

我使用UISpy,WinSpy,Winspector,UIA Verify应用程序查看控件等,但它们似乎没有为WinFms提供相同的WPF信息 .

例如,在WinForms应用程序中,当我通过 Spy 工具查看时,我看到一个ClassName为“WindowsForms10.EDIT.app.0.33c0d9d”的文本框 . UIA自动化验证应用程序是唯一一个确认元素存在并报告“TextBox”的应用程序 .

所以,我的问题是如何找到要传递的正确类名,或者是否有更简单的方法来查找子元素?

// does not work in wpf
IntPtr child = NativeMethods.FindWindowEx(parent, prevElement, "TextBox", null);

// works in winforms
IntPtr child = NativeMethods.FindWindowEx(parent, prevElement, "WindowsForms10.EDIT.app.0.33c0d9d", null);

这是我正在使用的user32.dll导入:

public class NativeMethods
{
    public const int WM_SETTEXT = 0x000C;
    public const int WM_GETTEXT = 0x000D; 
    public const uint CB_SHOWDROPDOWN = 0x014F;
    public const uint CB_SETCURSEL = 0x014E;
    public const int BN_CLICKED = 245;
    public const uint WM_SYSCOMMAND = 0x0112;
    public const int SC_CLOSE = 0xF060;

    [DllImport("user32.dll", SetLastError = true)]
    public static extern IntPtr FindWindow(string lpClassName, string lpWindowName);

    [DllImport("user32.dll", SetLastError = true)]
    public static extern IntPtr FindWindowEx(IntPtr parentHandle, IntPtr childAfter, string className, string windowTitle);

    [DllImport("user32.dll", SetLastError = false)]
    public static extern IntPtr GetDlgItem(IntPtr hDlg, int nIDDlgItem);

    [DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = false)]
    public static extern IntPtr SendMessage(HandleRef hWnd, uint Msg, IntPtr wParam, string lParam);

    [DllImport("user32.dll")]
    public static extern int SendMessage(int hWnd, uint Msg, int wParam, int lParam);

    [DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = false)]
    public static extern int SendMessage(int hWnd, int Msg, int wParam, StringBuilder lParam); 


}

1 回答

  • 3

    如果要自动化WPF,则 must 使用UI自动化,而不是"old thing of the past" windows API :-) .

    这里有关于UI自动化的很好的介绍:Bugslayer: GUI Control to Major Tom

    还有一个名为"White"的有趣的开源项目利用了UI自动化:White on codeplex . 如果你想挖掘,那里有一些样品 .

相关问题