Jump to content

Sky Slate Blueberry Blackcurrant Watermelon Strawberry Orange Banana Apple Emerald Chocolate

ControlCol() - Set background and text color Gui Controls

  • Please log in to reply
7 replies to this topic
  • Members
  • 304 posts
  • Last active: Jan 12 2017 09:11 PM
  • Joined: 28 Mar 2011



How to use:

Just call ControlCol(Control, Window, bc="", tc="", redraw=1)

   control - hwnd of your control

   Window - hwnd gui where is placed your control

   bc - BackGround color - RGB

   tc - Text color - RGB

   redraw - [true/false] redraw control after set new colors

ControlCol(Control, Window) - restore default colors


Supported controls:

Button, CheckBox and radio not support text color in this case


Notice that it use WindowProc callback so if your script use that also then the unexpected effects of may occur.


Tested on: AHK v1.1.14.03 U32, W7U




04.04.14 - fix trouble with restoring colors in some cases


02.04.14 - fix trouble with disabling glabel after use






#Singleinstance force
Gui, Add, Text,         hwndh_1, Text
Gui, Add, Edit,         hwndh_2, Edit
Gui, Add, Edit,         hwndh_3, Edit
Gui, Add, UpDown,       hwndh_4, UpDown
Gui, Add, Picture,      hwndh_5, Picture
Gui, Add, Button,       hwndh_6, Button
Gui, Add, CheckBox,     hwndh_7, CheckBox
Gui, Add, Radio,        hwndh_8, Radio
Gui, Add, ListBox,      hwndh_9, ListBox
Gui, Add, Link,         hwndh_10, Link

Gui, Add, Edit, w320 h50 HwndMyTextHwnd,  Here is some text that is given`na custom background color.
Gui, Add, Edit, w320 h50 HwndMyTextHwnd2, Here is some text that is given`na custom background color.
Gui, Add, Edit, w320 h50 HwndMyTextHwnd3, Here is some text that is given`na custom background color.
Gui +LastFound
GuiHwnd := WinExist()
Gui Show
loop, 10
	Random, t, 0, 0xffffff
	Random, b, 0, 0xffffff
	ControlCol(H_%A_Index%, GuiHwnd, t, b)
ControlCol(MyTextHwnd, GuiHwnd, 0xff0000, 0x00ff00)
ControlCol(MyTextHwnd2,GuiHwnd, 0x00ff00, 0x0000ff)
ControlCol(MyTextHwnd3,GuiHwnd, 0x0000ff, 0xff0000)
SetTimer, c, 100

Random, b, 0, 0xffffff
Random, t, 0, 0xffffff
ControlCol(MyTextHwnd3,GuiHwnd, b, t)



ControlCol.ahk v.1.0.2

; ------------------------
; http://www.autohotkey.com/board/topic/104539-controlcol-set-background-and-text-color-gui-controls/
; v1.0.2
; ------------------------
ControlCol(Control, Window, bc="", tc="", redraw=1) {
        ; msgbox
    a := {}
    a["c"]  := Control
    a["g"]  := Window
    a["bc"] := (bc="")?"":(((bc&255)<<16)+(((bc>>8)&255)<<8)+(bc>>16))
    a["tc"] := (tc="")?"":((tc&255)<<16)+(((tc>>8)&255)<<8)+(tc>>16)
    WindowProc("Set", a, "", "")
    If redraw
        SizeOfWINDOWINFO := 60
        VarSetCapacity(WINDOWINFO, SizeOfWINDOWINFO, 0)
        NumPut(SizeOfWINDOWINFO, WINDOWINFO, "UInt")
        DllCall("GetWindowInfo",  "Ptr", Control, "Ptr", &WINDOWINFO)
        DllCall("ScreenToClient", "Ptr", Window,  "Ptr", &WINDOWINFO+20) ; x1,y1 of Client area
        DllCall("ScreenToClient", "Ptr", Window,  "Ptr", &WINDOWINFO+28) ; x2,y2 of Client area
        , "Ptr",  Window             ; A handle to the window to be redrawn. If this parameter is NULL, the desktop window is updated.
        , "UInt", &WINDOWINFO+20    ; A pointer to a RECT structure containing the coordinates, in device units, of the update rectangle. This parameter is ignored if the hrgnUpdate parameter identifies a region.
        , "UInt", 0                    ; A handle to the update region. If both the hrgnUpdate and lprcUpdate parameters are NULL, the entire client area is added to the update region.
        , "UInt", 0x101)            ; One or more redraw flags. This parameter can be used to invalidate or validate a window, control repainting, and control which windows are affected by RedrawWindow.

WindowProc(hwnd, uMsg, wParam, lParam)
    Static Win := {}
    If uMsg between 0x132 and 0x138
    If  Win[hwnd].HasKey(lparam)
        If tc := Win[hwnd, lparam, "tc"]
        DllCall("SetTextColor", "UInt", wParam, "UInt", tc)
        If bc := Win[hwnd, lparam, "bc"]
        DllCall("SetBkColor",   "UInt", wParam, "UInt", bc)
        return Win[hwnd, lparam, "Brush"]  ; Return the HBRUSH to notify the OS that we altered the HDC.
    If (hwnd = "Set")
        a := uMsg
        Win[a.g, a.c] := a
        If (Win[a.g, a.c, "tc"] = "") and (Win[a.g, a.c, "bc"] = "")
            Win[a.g].Remove(a.c, "")
        If not Win[a.g, "WindowProcOld"]
            Win[a.g,"WindowProcOld"] := DllCall("SetWindowLong", "Ptr", a.g, "Int", -4, "Int", RegisterCallback("WindowProc", "", 4), "UInt")
        If Win[a.g, a.c, "Brush"]
            DllCall("DeleteObject", "Ptr", Brush)
        If (Win[a.g, a.c, "bc"] != "")
            Win[a.g, a.c, "Brush"] := DllCall("CreateSolidBrush", "UInt", a.bc)
        ; array_list(a)
    return DllCall("CallWindowProcA", "UInt", Win[hwnd, "WindowProcOld"], "UInt", hwnd, "UInt", uMsg, "UInt", wParam, "UInt", lParam)

Learning one
  • Members
  • 1483 posts
  • Last active: Jan 02 2016 02:30 PM
  • Joined: 04 Apr 2009

Nice, thanks :)

My Website • Recommended: AutoHotkey Unicode 32-bit • Join DropBox, Copy

  • Members
  • 245 posts
  • Last active: Mar 12 2015 02:29 PM
  • Joined: 06 Apr 2012

Nice, thanks

  • Members
  • 1216 posts
  • Last active: Oct 30 2015 05:12 PM
  • Joined: 27 Oct 2012

good stuff on this forum. ;) tested successfully!

  • Members
  • 3 posts
  • Last active: Oct 25 2014 12:24 PM
  • Joined: 30 May 2014


Nice code, works like a charm!


Except on U64. Any idea to make it 64bit compatible?


Thank you!

  • Members
  • 7 posts
  • Last active: Oct 14 2018 10:35 AM
  • Joined: 25 Jan 2013

This is awesome, thank you so much for this. Any chance you can make it so that I can change the listbox highlight color/text highlight color as well?

  • Members
  • 928 posts
  • Last active: Jan 12 2018 09:23 AM
  • Joined: 01 Aug 2012
Yes with ODLB_DrawItem by just me
[AHK] x64 Unicode | [WIN] 10 Pro (Version 1709)
My GitHub Profile | Donations are appreciated if I could help you

  • Members
  • 7 posts
  • Last active: Oct 14 2018 10:35 AM
  • Joined: 25 Jan 2013

Thanks jNizM. It works pretty well. Although I can't set different highlight colors for multiple listboxes?