在窗口中查找特定控件

时间:2013-08-04 12:10:46

标签: c# window progress-bar findwindow

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

1 个答案:

答案 0 :(得分:1)

我使用UIAutomation混合了SendMessageFindWindow

来回答了这个问题
//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");
    }
}