Changing Control Background/Text Color

Post your working scripts, libraries and tools for AHK v1.1 and older
User avatar
Masonjar13
Posts: 1555
Joined: 20 Jul 2014, 10:16
Location: Не Россия
Contact:

Changing Control Background/Text Color

08 May 2018, 13:25

Perhaps there's a better way for doing so, but I stumbled across this post while trying to figure out how to change the bg of a readonly edit control. Seeing as it was written such a long time ago, I updated the syntax and upped the functionality of it.

Code: Select all

; callback for onMessage(0x0138,"WM_CTLCOLOR")
wm_ctlcolor(wParam,lParam,hwnd){
    if(!isObject(ctlColorObj[lParam]))
        return
    hBrush:=dllCall("Gdi32.dll\CreateSolidBrush","UInt",ctlColorObj[lParam].bg,"UPtr")

    if(ctlColorObj[lParam].text)
        dllCall("gdi32.dll\SetTextColor","Ptr",wParam,"UInt",ctlColorObj[lParam].text)
    dllCall("gdi32.dll\SetBkColor","Ptr",wParam,"UInt",ctlColorObj[lParam].bg)
    dllCall("gdi32.dll\SetBkMode","Ptr",wParam,"Int",2)
    return hBrush
}
It uses a global object to determine which controls should be changed in what way. Here's an example:

Code: Select all

global ctlColorObj:={}
onMessage(0x0138,"WM_CTLCOLOR")

gui,add,edit,+readonly +hwndchlhwnd r20 w300,Demo text.
ctlColorObj[chlhwnd]:={bg:0xff00ff,text:0xffffff}
gui,show,,Control Color
return

guiClose:
exitApp

F1::
ctlColorObj[chlhwnd]:={bg:0xff00ff,text:0xffffff}
winSet,redraw,,% "ahk_id " . chlhwnd
return

F2::
ctlColorObj[chlhwnd]:={bg:0x00ff00,text:0x000000}
winSet,redraw,,% "ahk_id " . chlhwnd
return

wm_ctlcolor(wParam,lParam,hwnd){
    if(!isObject(ctlColorObj[lParam]))
        return
    hBrush:=dllCall("Gdi32.dll\CreateSolidBrush","UInt",ctlColorObj[lParam].bg,"UPtr")

    if(ctlColorObj[lParam].text)
        dllCall("gdi32.dll\SetTextColor","Ptr",wParam,"UInt",ctlColorObj[lParam].text)
    dllCall("gdi32.dll\SetBkColor","Ptr",wParam,"UInt",ctlColorObj[lParam].bg)
    dllCall("gdi32.dll\SetBkMode","Ptr",wParam,"Int",2)
    return hBrush
}
The only problem I can't fix is the redraw (guiControl,moveDraw,Control doesn't seem to work), so changing the colors isn't instant (you'll have to click off the window and back onto it).
Hiding the GUI and showing it again will force a redraw. I suppose there may be a window message that could be sent to also force this kind of redraw?
Thanks to burque505 and guest3456 :)
OS: Windows 10 Pro | Editor: Notepad++
My Personal Function Library | Old Build - New Build
burque505
Posts: 1736
Joined: 22 Jan 2017, 19:37

Re: Changing Control Background/Text Color

08 May 2018, 15:49

Hi Masonjar13, this flickers, and sometimes it misses a change, but you don't have to click off and on. Obviously not an ideal solution.

Code: Select all

global ctlColorObj:={}
global preference := {}
onMessage(0x0138,"WM_CTLCOLOR")

gui,add,edit,+readonly +hwndchlhwnd r20 w300,Demo text.
ctlColorObj[chlhwnd]:={bg:0xff00ff,text:0xffffff}
gui,show,,Control Color
return

guiClose:
exitApp

F1::
gui, destroy
preference:={bg:0xff00ff,text:0xffffff}
gosub, NewGui
return
F2::
gui, destroy
preference:={bg:0x00ff00,text:0x000000}
gosub, NewGui
return
return

wm_ctlcolor(wParam,lParam,hwnd){
    if(!isObject(ctlColorObj[lParam]))
        return
    hBrush:=dllCall("Gdi32.dll\CreateSolidBrush","UInt",ctlColorObj[lParam].bg,"UPtr")

    if(ctlColorObj[lParam].text)
        dllCall("gdi32.dll\SetTextColor","Ptr",wParam,"UInt",ctlColorObj[lParam].text)
    dllCall("gdi32.dll\SetBkColor","Ptr",wParam,"UInt",ctlColorObj[lParam].bg)
    dllCall("gdi32.dll\SetBkMode","Ptr",wParam,"Int",2)
    return hBrush
}

NewGui:
gui,add,edit,+readonly +hwndchlhwnd r20 w300,Demo text.
ctlColorObj[chlhwnd]:=preference
gui,show,,Control Color
return
Regards,
burque505
User avatar
Masonjar13
Posts: 1555
Joined: 20 Jul 2014, 10:16
Location: Не Россия
Contact:

Re: Changing Control Background/Text Color

08 May 2018, 19:29

Yeah, destroying a GUI and recreating it over is very inefficient, and significantly more so if it's a complicated GUI. But, you gave me an idea! I had tried gui,cancel,nohide, which didn't work. However, actually hiding it then showing it again does in fact work. Again, not a great solution, more of a workaround, but it does work. I've added it to the main example.

Thanks for the input :)
OS: Windows 10 Pro | Editor: Notepad++
My Personal Function Library | Old Build - New Build
burque505
Posts: 1736
Joined: 22 Jan 2017, 19:37

Re: Changing Control Background/Text Color

08 May 2018, 19:42

Cool! By chance I saw something somewhere earlier today - can't remember, in the forums somewhere - about needing a brief delay with color changes.
It doesn't seem to crash like this. What do you think? EDIT - took out one of the 10ms delays, still works.

Code: Select all

global ctlColorObj:={}
onMessage(0x0138,"WM_CTLCOLOR")

gui,add,edit,+readonly +hwndchlhwnd r20 w300,Demo text.
ctlColorObj[chlhwnd]:={bg:0xff00ff,text:0xffffff}
gui,show,,Control Color
return

guiClose:
exitApp

F1::
gui,cancel
;sleep, 10
ctlColorObj[chlhwnd]:={bg:0xff00ff,text:0xffffff}
sleep, 10
gui,show
return

F2::
gui,cancel
;sleep, 10
ctlColorObj[chlhwnd]:={bg:0x00ff00,text:0x000000}
sleep, 10
gui,show
return

wm_ctlcolor(wParam,lParam,hwnd){
    if(!isObject(ctlColorObj[lParam]))
        return
    hBrush:=dllCall("Gdi32.dll\CreateSolidBrush","UInt",ctlColorObj[lParam].bg,"UPtr")

    if(ctlColorObj[lParam].text)
        dllCall("gdi32.dll\SetTextColor","Ptr",wParam,"UInt",ctlColorObj[lParam].text)
    dllCall("gdi32.dll\SetBkColor","Ptr",wParam,"UInt",ctlColorObj[lParam].bg)
    dllCall("gdi32.dll\SetBkMode","Ptr",wParam,"Int",2)
    return hBrush
}
guest3456
Posts: 3463
Joined: 09 Oct 2013, 10:31

Re: Changing Control Background/Text Color

09 May 2018, 11:03

Masonjar13 wrote: Hiding the GUI and showing it again will force a redraw. I suppose there may be a window message that could be sent to also force this kind of redraw?

Code: Select all

F1::
ctlColorObj[chlhwnd]:={bg:0xff00ff,text:0xffffff}
WinSet, Redraw,, ahk_id %chlhwnd%
return
works for me

burque505
Posts: 1736
Joined: 22 Jan 2017, 19:37

Re: Changing Control Background/Text Color

09 May 2018, 11:13

@guest3456, not only works but no flicker at all. Bravo.
Regards,
burque505
User avatar
Masonjar13
Posts: 1555
Joined: 20 Jul 2014, 10:16
Location: Не Россия
Contact:

Re: Changing Control Background/Text Color

09 May 2018, 17:55

Yep, there it is. I was just tired and didn't go looking for it ;) Thanks guest3456, updated main example
OS: Windows 10 Pro | Editor: Notepad++
My Personal Function Library | Old Build - New Build
burque505
Posts: 1736
Joined: 22 Jan 2017, 19:37

Re: Changing Control Background/Text Color

12 Jan 2019, 11:18

:Bump
I ran across this again and it occurred to me that you might not want the user selecting that text, though I'm sure there are times when you would.
Spoiler
bg_no_edit.gif
bg_no_edit.gif (26.47 KiB) Viewed 2710 times
User avatar
JnLlnd
Posts: 487
Joined: 29 Sep 2013, 21:29
Location: Montreal, Quebec, Canada
Contact:

Re: Changing Control Background/Text Color

13 Sep 2022, 17:37

Thanks Masonjar13 and other for putting this together. It works perfectly with a read-only edit control. Is it possible to adapt this code to also support color change in non read-only edit control?
:thumbup: Author of freeware Quick Access Popup, the powerful Windows folders, apps and documents launcher!
:P Now working on Quick Clipboard Editor
:ugeek: The Automator's Courses on AutoHotkey
just me
Posts: 9482
Joined: 02 Oct 2013, 08:51
Location: Germany

Re: Changing Control Background/Text Color

14 Sep 2022, 04:38

Hi @JnLlnd,

you have to process the WM_CTLCOLOREDIT (0x0133) notification in this case.
User avatar
JnLlnd
Posts: 487
Joined: 29 Sep 2013, 21:29
Location: Montreal, Quebec, Canada
Contact:

Re: Changing Control Background/Text Color

14 Sep 2022, 07:41

Thank you just me. I also found your class Class_CtlColors (viewtopic.php?f=6&t=2197). Very well done!
:thumbup: Author of freeware Quick Access Popup, the powerful Windows folders, apps and documents launcher!
:P Now working on Quick Clipboard Editor
:ugeek: The Automator's Courses on AutoHotkey
junjunjon
Posts: 2
Joined: 18 Nov 2022, 21:12

Re: Changing Control Background/Text Color

23 Nov 2022, 08:41

how can i change any random edit control color with this scpript, can it do that? many thanks.

Return to “Scripts and Functions (v1)”

Who is online

Users browsing this forum: No registered users and 96 guests