3

Im trying to find the value of a specific ProgressBar (msctls_progress32) on a window,

I have found the window with:

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

But i cant get the pointer of the ProgressBar with:

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

Then once i have the pointer i want to get the value with:

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

The problem is that there are multiple Progress Bars on the window and the progress bar i want the pointer of is inside multiple #32770 (Dialog)

string.Empty
  • 10,393
  • 4
  • 39
  • 67
  • 1
    Since you're using C#, why not use the functionality provided in the UIAutomation namespace? Much easier. – Cody Gray - on strike Aug 04 '13 at 12:15
  • Could you give me an example. – string.Empty Aug 04 '13 at 12:24
  • We already know what these declarations look like. What we don't know is what your code looks like. One possible oversight is that you are forgetting to find the dialog first. – Hans Passant Aug 04 '13 at 13:02
  • I do know that i have to look inside all the dialogs first. but if i use the `FindWindowEx` method on on a window to find a dialog. how do i get a specific one if there are are 7 dialogs with the no window captions. – string.Empty Aug 04 '13 at 19:23
  • Are all of those dialogs at the same level, or are they children of one another? You don't have to pass a window caption to the `FindWindowEx` function, it will just return the first matching window that it finds. That will work fine if the dialogs are children of each other. Alternatively, you may want to look into [`EnumChildWindows`](http://msdn.microsoft.com/en-us/library/windows/desktop/ms633494.aspx). – Cody Gray - on strike Aug 05 '13 at 06:12
  • there are a few at the same level and some that are descendants. I'm going to try UIAutomation that you mentioned earlier. – string.Empty Aug 05 '13 at 06:32
  • @CodyGray Got it working with a combination of everything. thanks. – string.Empty Aug 05 '13 at 06:43

1 Answers1

1

I answered the question by using UIAutomation mixed with SendMessage and 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");
    }
}
string.Empty
  • 10,393
  • 4
  • 39
  • 67