Background color of GUI Checkbox and Radio label text

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
User avatar
JoeWinograd
Posts: 2202
Joined: 10 Feb 2014, 20:00
Location: U.S. Central Time Zone

Background color of GUI Checkbox and Radio label text

14 Mar 2021, 15:02

Hi Folks,

I'm trying to change the background color of the label that is displayed next to GUI Checkbox and Radio controls. I thought that GuiControl,+BackgroundRRGGBB,var would do it, but it doesn't. Here's some sample code:

Code: Select all

Gui,Add,Checkbox,vHello Checked,Hello
Gui,Add,Checkbox,vWorld,World
GuiControl,+BackgroundFF0000,Hello
GuiControl,+Background0000FF,World
Gui,Show,w300
Return

GuiEscape:
GuiClose:
ExitApp
I was expecting that code to make the background color of the Hello text to be Red and the background color of the World text to be Blue, but it does not. I also tried putting the +Background code in the Options section of the Gui,Add:

Code: Select all

Gui,Add,Checkbox,vHello Checked +BackgroundFF0000,Hello
Gui,Add,Checkbox,vWorld +Background0000FF,World
It is not flagged as a syntax error, but it also does not work.

Any ideas on how to get a background color on the text label of Checkbox and Radio controls? Thanks, Joe
teadrinker
Posts: 4344
Joined: 29 Mar 2015, 09:41
Contact:

Re: Background color of GUI Checkbox and Radio label text

14 Mar 2021, 16:12

Some options:

Code: Select all

Gui,Add,Checkbox, hwndhHello Checked, Hello
Gui,Add,Checkbox, hwndhWorld, World
OnMessage(0x138, Func("WM_CTLCOLORSTATIC").Bind( {hwnd: hHello, backColor: 0xFFAA55} ; colors in BGR
                                               , {hwnd: hWorld, backColor: 0x55AAFF} ))
Gui,Show,w300
Return

GuiEscape:
GuiClose:
ExitApp

WM_CTLCOLORSTATIC(Hello, World, wp, lp) {
   static Brushes := {}
   hDC := wp
   for k, v in [Hello, World] {
      if (lp = v.hwnd) {
         DllCall("SetBkColor", "Ptr", hDC, "UInt", v.backColor)
         Return Brushes.HasKey(v.hwnd) ? Brushes[v.hwnd] : Brushes[v.hwnd] := DllCall("CreateSolidBrush", "UInt", v.backColor, "Ptr")
      }
   }
}

Code: Select all

Gui,Add,Checkbox, hwndhHello Checked, Hello
Gui,Add,Checkbox, hwndhWorld, World
for k, v in [hHello, hWorld]
   DllCall("UxTheme\SetWindowTheme", "Ptr", v, "Ptr", 0, "Str", "")
OnMessage(0x138, Func("WM_CTLCOLORSTATIC").Bind( {hwnd: hHello, textColor: 0xFF, backColor: 0xFF0000}
                                               , {hwnd: hWorld, textColor: 0xFF0000, backColor: 0xFF} ))
Gui,Show,w300
Return

GuiEscape:
GuiClose:
ExitApp

WM_CTLCOLORSTATIC(Hello, World, wp, lp) {
   hDC := wp
   for k, v in [Hello, World] {
      if (lp = v.hwnd) {
         DllCall("SetTextColor", "Ptr", hDC, "UInt", v.textColor)
         DllCall("SetBkColor"  , "Ptr", hDC, "UInt", v.backColor)
         Return DllCall("GetStockObject", "Int", NULL_BRUSH := 0x5, "Ptr")
      }
   }
}
User avatar
JoeWinograd
Posts: 2202
Joined: 10 Feb 2014, 20:00
Location: U.S. Central Time Zone

Re: Background color of GUI Checkbox and Radio label text

14 Mar 2021, 18:17

teadrinker wrote:Some options:
Hi teadrinker,
Thanks for the code options...both work perfectly! I tested both on W10, then tested the first one on W7 and W8.1. All tests successful! I'm working on a program that runs on XP and later. I don't have access right now to XP and Vista to test. Do you know if the code (either one) will work on XP and Vista? I'll be able to test those at a later time, but I thought you might know off the top of your head. In any case, thanks very much for the code...I really appreciate it! Cheers, Joe
teadrinker
Posts: 4344
Joined: 29 Mar 2015, 09:41
Contact:

Re: Background color of GUI Checkbox and Radio label text

14 Mar 2021, 22:19

Winapi SetWindowTheme started with Windows Vista, so the second code may not work on XP.
User avatar
JoeWinograd
Posts: 2202
Joined: 10 Feb 2014, 20:00
Location: U.S. Central Time Zone

Re: Background color of GUI Checkbox and Radio label text

14 Mar 2021, 22:49

Thanks very much!
User avatar
Jim Dunn
Posts: 478
Joined: 01 Sep 2020, 20:21
Location: NSW

Re: Background color of GUI Checkbox and Radio label text

14 Mar 2021, 23:23

teadrinker wrote:
14 Mar 2021, 22:19
Winapi SetWindowTheme started with Windows Vista, so the second code may not work on XP.
I have a VMware XP Professional virtual machine here, which I use for testing.

Both scripts seemed to run fine on that, changing the background colour of the label text (and the text colour with the second script).

I keep that VM updated as far as is possible for XP, so there might be no guarantee the scripts would work on every XP box - but they worked on that one. ;)
Last edited by Jim Dunn on 15 Mar 2021, 07:48, edited 1 time in total.
teadrinker
Posts: 4344
Joined: 29 Mar 2015, 09:41
Contact:

Re: Background color of GUI Checkbox and Radio label text

15 Mar 2021, 01:08

Perhaps Windows XP just has no window themes.
User avatar
Jim Dunn
Posts: 478
Joined: 01 Sep 2020, 20:21
Location: NSW

Re: Background color of GUI Checkbox and Radio label text

15 Mar 2021, 01:39

teadrinker wrote:
15 Mar 2021, 01:08
Perhaps Windows XP just has no window themes.
Well, I noticed the documentation page for SetWindowTheme does say "Vista onwards", but XP has UxTheme.dll, and a quick google found people using SetWindowTheme for XP in C/VB code.

I seem to recall people offering unofficial 'patched' versions of that dll to allow custom, third-party theming on XP, because the dll wouldn't, by default, permit themes not signed by MS.
teadrinker
Posts: 4344
Joined: 29 Mar 2015, 09:41
Contact:

Re: Background color of GUI Checkbox and Radio label text

15 Mar 2021, 08:21

Thanks for your investigation. I like your attention to detail! :)
User avatar
jNizM
Posts: 3183
Joined: 30 Sep 2013, 01:33
Contact:

Re: Background color of GUI Checkbox and Radio label text

15 Mar 2021, 08:47

There is also a whole class about colored controls -> https://www.autohotkey.com/boards/viewtopic.php?t=2197
[AHK] v2.0.5 | [WIN] 11 Pro (Version 22H2) | [GitHub] Profile
User avatar
JoeWinograd
Posts: 2202
Joined: 10 Feb 2014, 20:00
Location: U.S. Central Time Zone

Re: Background color of GUI Checkbox and Radio label text

15 Mar 2021, 20:33

Hi Jim,
Thanks for the research...much appreciated.

Hi jNizM,
Thanks for pointing me to that class. I've used just me's fantastic LV_Colors class for many years with great success. I'll give the CtlColors one a spin.

Hi teadrinker,
I was able to test both methods today on XP and Vista...both worked perfectly on both systems! Thanks again!

Regards, Joe
User avatar
JoeWinograd
Posts: 2202
Joined: 10 Feb 2014, 20:00
Location: U.S. Central Time Zone

Re: Background color of GUI Checkbox and Radio label text

24 Mar 2021, 16:12

teadrinker wrote:Some options
Hi teadrinker,
I've been using the second option, passing both the text color and background color. Of course. "hello" and "world" were simply examples for posting...the actual program allows specification of many colors, where I want the lighter colors to have a black background and the darker colors to have a white background. Most of the time, your code works great, giving a result like this (an example with only eight of the colors):

background works.png
background works.png (8.21 KiB) Viewed 1046 times

However, sometimes the background color fails (interestingly, I've never had the text color fail), giving a result like this:

background fails.png
background fails.png (7 KiB) Viewed 1046 times

It seems like a timing problem, and if I exit the program and run it again, the background color nearly always works. The entire program is more than 5,000 lines of code, so posting it for troubleshooting doesn't make sense (and I haven't been able to reproduce the problem in a short code snippet), but I'm wondering if, off the top of your head, you might have some ideas on why the background would fail occasionally and remedies for it. Thanks again, Joe
teadrinker
Posts: 4344
Joined: 29 Mar 2015, 09:41
Contact:

Re: Background color of GUI Checkbox and Radio label text

24 Mar 2021, 16:30

Hi, Joe
Try adding WinSet, Redraw:

Code: Select all

Gui, +LastFound
Gui,Add,Checkbox, hwndhHello Checked, Hello
Gui,Add,Checkbox, hwndhWorld, World
for k, v in [hHello, hWorld]
   DllCall("UxTheme\SetWindowTheme", "Ptr", v, "Ptr", 0, "Str", "")
OnMessage(0x138, Func("WM_CTLCOLORSTATIC").Bind( {hwnd: hHello, textColor: 0xFF, backColor: 0xFF0000}
                                               , {hwnd: hWorld, textColor: 0xFF0000, backColor: 0xFF} ))
Gui,Show,w300
Sleep, 100
WinSet, Redraw
Return

GuiEscape:
GuiClose:
ExitApp

WM_CTLCOLORSTATIC(Hello, World, wp, lp) {
   hDC := wp
   for k, v in [Hello, World] {
      if (lp = v.hwnd) {
         DllCall("SetTextColor", "Ptr", hDC, "UInt", v.textColor)
         DllCall("SetBkColor"  , "Ptr", hDC, "UInt", v.backColor)
         Return DllCall("GetStockObject", "Int", NULL_BRUSH := 0x5, "Ptr")
      }
   }
}
See this discussion.
User avatar
JoeWinograd
Posts: 2202
Joined: 10 Feb 2014, 20:00
Location: U.S. Central Time Zone

Re: Background color of GUI Checkbox and Radio label text

24 Mar 2021, 16:45

teadrinker wrote:Try adding WinSet, Redraw
Thanks for the idea (and the link to the other thread). I'll give it a spin and will let you know how it goes. Cheers, Joe
User avatar
JoeWinograd
Posts: 2202
Joined: 10 Feb 2014, 20:00
Location: U.S. Central Time Zone

Re: Background color of GUI Checkbox and Radio label text

04 Aug 2021, 13:16

Hi @teadrinker,

Sorry for taking so long to respond, but it has, in fact, taken me this long to find something that works...I think. :)

I tried WinSet back in March, but it did not solve the problem consistently. In addition to your Sleep,100 suggestion, I tried a half-dozen other values...some lower...some higher...with same result...works a lot of the time, but not all of the time (also tried no sleep, which caused the background to fail immediately and consistently). Also, this approach created an annoying flicker, although I may have been willing to live with it if it had solved the problem.

I then hit on the idea of simply doing a second Gui,Show. That worked...until it didn't. :( Then went to a third Gui,Show, then five, then ten. It was getting so crazy, I created a variable for the Gui,Show count, which I tried as high as 20. Turns out that 10 was the best during testing, but still got the occasional failure.

Over the past two days, I think I found the answer...via dumb luck. I got the usual failure, then happened to click on one of the colors with no background and, voilà, the background color appeared. Clicked on several other colors...each time, the background color appeared. Seems that giving it focus created the background. So, I put a simple loop in my program with a GuiControl,Focus,%Color% for each color. After the loop, I added a GuiControl,Focus for the item that I actually want to have focus (and the default). The performance is fine (can't see the focus loop doing its thing), there is no flicker, and, so far...no failures...in two days, across several machines, including W7, W8.1, W10, and W11.

It's too early to declare victory, but I have high hopes. Will post back here if any failures occur. Thanks again to everyone who helped in this thread. Regards, Joe

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: mikeyww, silelunu, Spawnova and 346 guests