ProgressBar get value always got 0 using WinAPI only

I doing for children handles of parent in C# code. I want to get progressbar value like 52% by another using SendMessage.

WPF App

Left is progress value 56 and right is child handle 110C46 at middle label in progressbar

I tried this code:

static void Main(string[] args)
{
    Process app = Process.GetProcessesByName("ProgressBarTest").FirstOrDefault();
    if (app == null || app.MainWindowHandle == IntPtr.Zero) // Exit console app if no found
        return;
    var children = GetChildWindows(app.MainWindowHandle);   // Get all children of parent in another process
    var child = children[3];                                // I known that the child is a ProgressBar. 
    // Get ProgressBar all values using SendMessage
    var value1 = SendMessage(child.hWnd, PBM_GETPOS, IntPtr.Zero, IntPtr.Zero);
    var value2 = SendMessage(child.hWnd, PBM_GETSTATE, IntPtr.Zero, IntPtr.Zero);
    var value3 = SendMessage(child.hWnd, PBM_GETSTEP, IntPtr.Zero, IntPtr.Zero);
    Console.WriteLine("Value1 is " + value1.ToString());
    Console.WriteLine("Value2 is " + value2.ToString());
    Console.WriteLine("Value3 is " + value3.ToString());
    Console.ReadKey();
}

cmd result output:

enter image description here

I got 0 values using 100% correctly progressbar handle. I don’t known why i got return 0 always.

  • I sure that the child handle is a progressbar handle.
  • I checked using Spy++

Spy++App

Full source:

using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;
namespace ProgressBarGetValue
{
    internal class Program
    {
        [DllImport("user32", CharSet = CharSet.Unicode)]
        public static extern IntPtr SendMessage(IntPtr hwnd, uint msg, IntPtr wParam, IntPtr lParam);
        public const int PBM_SETRANGE = 1001;
        public const int PBM_SETSTATE = 10;
        public const int PBM_SETPOS = 1004;
        public const int PBM_SETRANGE32 = 1006;
        public const int PBM_GETRANGE = 1008;
        public const int PBM_GETPOS = 1006;
        public const int PBM_SETBARCOLOR = 1009;
        public const int PBM_SETBKCOLOR = 0x2001;
        public const int PBM_SETMARQUEE = 1003;
        public const int PBM_GETSTATE = 1013;
        public const int PBM_GETSTEP = 1007;
        public const int PBM_STEPIT = 1005;
        public const int PBM_GETBKCOLOR = 0x2001;
        public const int PBM_GETBARCOLOR = 0x2009;
        static void Main(string[] args)
        {
            Process app = Process.GetProcessesByName("ProgressBarTest").FirstOrDefault();
            if (app == null || app.MainWindowHandle == IntPtr.Zero) // Exit console app if no found
                return;
            var children = Children.GetChildWindows(app.MainWindowHandle);   // Get all children of parent in another process
            var child = children[3];                                // I known that the child is a ProgressBar. 
            // Get ProgressBar all values using SendMessage
            var value1 = SendMessage(child.hWnd, PBM_GETPOS, IntPtr.Zero, IntPtr.Zero);
            var value2 = SendMessage(child.hWnd, PBM_GETSTATE, IntPtr.Zero, IntPtr.Zero);
            var value3 = SendMessage(child.hWnd, PBM_GETSTEP, IntPtr.Zero, IntPtr.Zero);
            Console.WriteLine("Value1 is " + value1.ToString());
            Console.WriteLine("Value2 is " + value2.ToString());
            Console.WriteLine("Value3 is " + value3.ToString());
            Console.ReadKey();
        }
    }
    public class Children
    {
        public class ChildInfo
        {
            public IntPtr hWnd { get; }
            public string ClassName { get; }
            public string Name { get; }
            public ChildInfo(IntPtr hwnd, string className, string name)
            {
                hWnd = hwnd;
                ClassName = className;
                Name = name;
            }
        }
        public delegate bool EnumWindowProc(IntPtr hwnd, IntPtr lParam);

        [DllImport("user32", CharSet = CharSet.Unicode)]
        public static extern int GetClassName(IntPtr hWnd, StringBuilder lpClassName, int nMaxCount);
        [DllImport("user32", CharSet = CharSet.Unicode)]
        public static extern int GetWindowText(IntPtr hWnd, StringBuilder lpString, int nMaxCount);
        [DllImport("user32.dll")]
        public static extern bool EnumChildWindows(IntPtr hwndParent, EnumWindowProc lpEnumFunc, IntPtr lParam);
        public static List<ChildInfo> GetChildWindows(IntPtr parentHandle)
        {
            var result = new List<ChildInfo>();

            EnumWindowProc childProc = (hwnd, lParam) =>
            {
                var classNameBuilder = new System.Text.StringBuilder(1024);
                GetClassName(hwnd, classNameBuilder, classNameBuilder.Capacity);

                var nameBuilder = new System.Text.StringBuilder(1024);
                GetWindowText(hwnd, nameBuilder, nameBuilder.Capacity);

                var childInfo = new ChildInfo(hwnd, classNameBuilder.ToString(), nameBuilder.ToString());
                result.Add(childInfo);

                return true;
            };

            EnumChildWindows(parentHandle, childProc, IntPtr.Zero);

            return result;
        }

    }
}

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật