【问题标题】:Find a specific control on a window在窗口中查找特定控件
【发布时间】:2013-08-04 12:10:46
【问题描述】:

我试图在窗口上查找特定 ProgressBar (msctls_progress32) 的值,

我找到了窗口:

[DllImport("User32.dll")]
public static extern IntPtr FindWindow(string strClassName, string strWindowName);

但我无法获得 ProgressBar 的指针:

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

然后,一旦我有了指针,我想通过以下方式获取值:

public const int PBM_GETPOS = 0x0408;
[DllImport("User32.dll")]
public static extern Int32 SendMessage(IntPtr hWnd, uint Msg, IntPtr wParam, IntPtr lParam);

问题是窗口上有多个进度条,我想要指针的进度条在多个#32770(对话框)内

【问题讨论】:

  • 既然您使用的是 C#,为什么不使用 UIAutomation 命名空间中提供的功能呢?容易得多。
  • 你能给我举个例子吗?
  • 我们已经知道这些声明是什么样子的。我们不知道您的代码是什么样的。一个可能的疏忽是您忘记先找到对话框。
  • 我知道我必须先查看所有对话框。但是如果我在窗口上使用FindWindowEx 方法来查找对话框。如果有 7 个没有窗口标题的对话框,我如何获得一个特定的对话框。
  • 所有这些对话框是否处于同一级别,或者它们是彼此的子级?您不必将窗口标题传递给FindWindowEx 函数,它只会返回它找到的第一个匹配窗口。如果对话框是彼此的孩子,那将正常工作。或者,您可能想查看EnumChildWindows

标签: c# window progress-bar findwindow


【解决方案1】:

我使用UIAutomationSendMessageFindWindow 混合回答了这个问题

//Get parent window.
AutomationElement element = AutomationElement.FromHandle(Win32.FindWindow(null, "Form1"));
//Get all descendants
AutomationElementCollection elements =  element.FindAll(TreeScope.Descendants, Condition.TrueCondition);
//loop through descendants
foreach (AutomationElement elementNode in elements)
{
    //if descendant is a progress bar
    if (elementNode.Current.NativeWindowHandle != 0 && elementNode.Current.LocalizedControlType == "progress bar")
    {
        //Show value of the bar.
        MessageBox.Show(Win32.SendMessage((IntPtr)elementNode.Current.NativeWindowHandle, Win32.PBM_GETPOS, IntPtr.Zero, IntPtr.Zero).ToString(), "Bar value");
    }
}

【讨论】:

    猜你喜欢
    • 2010-09-06
    • 2016-10-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-02-20
    • 1970-01-01
    • 2012-01-14
    相关资源
    最近更新 更多