3

我试图在窗口上找到特定 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(对话框)内

4

1 回答 1

1

UIAutomation我通过使用混合和SendMessage和回答了这个问题FindWindow

//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");
    }
}
于 2013-08-05T06:55:26.637 回答