[V2-β7] How can I retrieve the "background" option of a Progress Bar? Topic is solved

Get help with using AutoHotkey (v2 or newer) and its commands and hotkeys
hoangthi
Posts: 16
Joined: 08 Oct 2020, 11:00

[V2-β7] How can I retrieve the "background" option of a Progress Bar?

Post by hoangthi » 05 Aug 2022, 10:42

Hello, I've just added a simple GUI with Progress bar which has the option BackgroundColor, and variable vProgress.

Code: Select all

 
 myGui.Add("Progress", "x0 h33 w300 Background202124 Disabled vProgress" . ID)	
 
I can easily change the background option to another color by this command

Code: Select all

ProgressControl := myGui[Progress]
ProgressControl.Opt("+Background4f5156")
The problem is, how can I retrieve this "background" option of the ProgressBar?
I tried ProgressControl.BackColor and ProgressControl.Opt but none of them returns the value I needed.
Please give me a suggestion.
Peace!

CptRootBeard
Posts: 26
Joined: 16 Nov 2020, 14:47
Contact:

Re: [V2-β7] How can I retrieve the "background" option of a Progress Bar?  Topic is solved

Post by CptRootBeard » 05 Aug 2022, 15:50

You have a couple options here IMO - you can set up a background property on the progress object itself, like so:

Code: Select all

myGui := Gui()
ProgressControl := myGui.Add("Progress", "x0 h33 w300 Disabled")

;;Initialize the 'private' property that will shadow the dynamic prop below
ProgressControl._backgroundColor := ""

;;Define a setter that applies the options AND sets the 'private' property to shadow the input value,
;;the getter will then return that private property's value
ProgressControl.defineProp("BackgroundColor", {
	get: (self)=>self._backgroundColor,
	set: (self, newColor)=>(self.opt("+Background" . newColor), self._backgroundColor := newColor)
})

;;Set our initial background color, then report it back
ProgressControl.BackgroundColor := 0x202124

;;I'll use show here so you can see that the property works as described
myGui.Show()
msgbox(Format("{:#X}", ProgressControl.BackgroundColor), "Progress Bar Background Color")

;;Change our background color, then report it back
ProgressControl.BackgroundColor := 0x4f5156
msgbox(Format("{:#X}", ProgressControl.BackgroundColor), "Progress Bar Background Color (Again)")
(In this case, you'll always know what color the background is because you were the one that set the color to begin with.)

More robustly, you can use SendMessage to return the control's actual background color (in BGR, not RGB) - this would work for any Win32 progress bar control, not just one created in AHK.
Links are included in the code comments to learn more:

Code: Select all

;;Reference the following links:
;;	MSDocs for Control Messages:
;;	https://docs.microsoft.com/en-us/windows/win32/controls/pbm-getbkcolor

;;	Forum Post (from jeeswg, ca 2017) Re: the Values of Win32 messages:
;;	https://www.autohotkey.com/boards/viewtopic.php?t=39218

myGui := Gui()
ProgressControl := myGui.Add("Progress", "x0 h33 w300 Background202124 Disabled")

;;Define the message constant
PBM_GETBKCOLOR := 0x40E

;;Use SendMessage to get the background color
;;Per the msdocs for this message, wparam and lparam need to be excluded/zero
bkgColor := SendMessage(PBM_GETBKCOLOR, , , ProgressControl.hwnd)

;;Format it in hex to compare it to the initial set value (0x202124) - notice the value returned is in BGR format
msgbox(Format("{:#X}", bkgColor), "Progress Bar Background Color")

;;Now change the background color and test again
ProgressControl.Opt("+Background4f5156")

;;The result here should be 0x56514f (BGR version of 0x4f5156)
bkgColor := SendMessage(PBM_GETBKCOLOR, , , ProgressControl.hwnd)
msgbox(Format("{:#X}", bkgColor), "Progress Bar Background Color (Again)")
You could also combine the two approaches, wrapping SendMessage into a getter and wrapping .Opt (or PBM_SETBKCOLOR) into the setter. In that case, you wouldn't need the ._BackgroundColor property.
I'll leave that as an exercise for you or someone else, but it would be the cleanest approach.

hoangthi
Posts: 16
Joined: 08 Oct 2020, 11:00

Re: [V2-β7] How can I retrieve the "background" option of a Progress Bar?

Post by hoangthi » 06 Aug 2022, 02:28

CptRootBeard wrote:
05 Aug 2022, 15:50
You have a couple options here IMO - you can set up a background property on the progress object itself, like so:

(In this case, you'll always know what color the background is because you were the one that set the color to begin with.)

More robustly, you can use SendMessage to return the control's actual background color (in BGR, not RGB) - this would work for any Win32 progress bar control, not just one created in AHK.
Links are included in the code comments to learn more:

You could also combine the two approaches, wrapping SendMessage into a getter and wrapping .Opt (or PBM_SETBKCOLOR) into the setter. In that case, you wouldn't need the ._BackgroundColor property.
I'll leave that as an exercise for you or someone else, but it would be the cleanest approach.
I really appreciate it. I'd go for option 1, what I get is what I set before.

The option 2 is to use PBM_GETBKCOLOR, which is also getting the background color of the progress bar. As far as I know, this color is provided by the operating system, not by AutoHotkey itself.

This provides scalability for every application, however, I had a bad experience reading color from the system, I remember that the returned color values are different between 8-bit color and 10-bit color display mode, or maybe that was a mistake, I will try this later.
Many thanks

AHK_user
Posts: 515
Joined: 04 Dec 2015, 14:52
Location: Belgium

Re: [V2-β7] How can I retrieve the "background" option of a Progress Bar?

Post by AHK_user » 11 Aug 2022, 11:58

With this line you add the property BackGroundColor to all Guicontrols:

Code: Select all

Gui.Control.Prototype.DefineProp("BackGroundColor", { Get: (guiControl) => (c := SendMessage(0x40E, , , guiControl.hwnd), c:= (c & 255) << 16 | (c & 65280) | (c >> 16),Format("{:#X}",c)), Set: (guiControl, newColor) => (guiControl.opt("+Background" . newColor))})

lexikos
Posts: 9592
Joined: 30 Sep 2013, 04:07
Contact:

Re: [V2-β7] How can I retrieve the "background" option of a Progress Bar?

Post by lexikos » 11 Aug 2022, 22:45

PBM_GETBKCOLOR does not read a colour from "the system". It retrieves the colour value that the control itself is using, which is the exact value that AutoHotkey provided it with PBM_SETBKCOLOR (iirc).

These colour values are exclusively 8 bits per channel.

Post Reply

Return to “Ask for Help (v2)”