Jump to content

Sky Slate Blueberry Blackcurrant Watermelon Strawberry Orange Banana Apple Emerald Chocolate
Photo

Colored Controls (no bitmaps needed)


  • Please log in to reply
21 replies to this topic
derRaphael
  • Members
  • 872 posts
  • Last active: Mar 19 2013 04:42 PM
  • Joined: 23 Nov 2007
here is an example which was originally published in germany forum first...
its not perfect yet but seems to work on a wider variety of windows.

Posted Image
Posted Image

#NoEnv
; Example for colored backgrounds for multiple controls
; based upon the RCB example from helpfile

; Description: It works with a redirection of the CallWindowProc to a UserDefinedFunction
;              Within the script. Right after execution the Call will be directed to the 
;              origin address.

; HTML Colors in BGR-format
Black = 0x000000
Green = 0x008000
Silver = 0xC0C0C0
Lime = 0x00FF00
Gray = 0x808080
Olive = 0x800080
White = 0xFFFFFF
Yellow = 0x00FFFF
Maroon = 0x000080
Navy = 0x800000
Red = 0x0000FF
Blue = 0xFF0000
Purple = 0x800080
Teal = 0x808000
Fuchsia = 0xFF00FF
Aqua = 0xFFFF00

Gui, Margin, 10, 10
Gui, Font, s12
Gui, Color, Black
Gui, Add, Text, vTXT1 HwndTX1ID w300, A little Text`nwith colored Background
Control_Colors("TXT1", "Set", Teal, White)
Gui, Add, CheckBox, HwndCB1ID wp vCB1 gCB1Clicked, A Checkbox
Control_Colors("CB1", "Set", Silver)
Gui, Add, Radio, HwndRB1ID xp y+0 wp vRB1 gRB1Clicked, Radiobutton used as Checkbox
RB1 := False
Control_Colors("RB1", "Set", Silver)
Gui, Add, Edit, vROED1 HwndED1ID ReadOnly wp, This is a Readonly Edit!
Control_Colors("ROED1", "Set", Red, Yellow)
Gui, Add, Text, vDTXT1 HwndTX2ID Disabled wp, This Text is disabled.
Control_Colors("DTXT1", "Set", Maroon, Black)
Gui, Add, Edit, vED1 HwndED2ID r2 wp, A normal editfield
Control_Colors("ED1", "Set", Navy, White)
Gui, Add, Button, vBTChange gBTChange wp, Change that Colors!

Gui, +LastFound
GuiID := WinExist()
Control_Colors(GuiID, "RCB", 0, 0)
Gui, Show, , Colors
Return

GuiEscape:
GuiClose:
	ExitApp

BTChange:
	Control_Colors(TX1ID, "Set", Red, Teal)
	Control_Colors(ED1ID, "Set", Maroon, Yellow)
	Control_Colors(TX2ID, "Set", Aqua, White)
	Control_Colors(ED2ID, "Set", Silver, Navy)
	Gui, +LastFound
	WinSet, Redraw
	GuiControl, Hide, BTChange
	Gui, Show, AutoSize
Return

CB1Clicked:
	GuiControlGet, CB1
	Control_Colors(CB1ID, "Set", (CB1 ? Yellow : Silver))
	WinSet, Redraw, , ahk_id %CB1ID%
Return

RB1Clicked:
RB1 ^= True
Control_Colors(RB1ID, "Set", (RB1 ? Green : Silver))
WinSet, Redraw, , ahk_id %RB1ID%
GuiControl, , RB1, %RB1%
Return

Control_Colors(Hwnd, Msg, wParam, lParam = 0)
{
   Critical

   If !(Hwnd+0) {
     GuiControlGet, nHwnd, Hwnd, %Hwnd%
     Hwnd := nhwnd
   }

   Static OldWinProc := ""          ; origin Windowprocedure
   Static NewWinProc := ""          ; new Windowprocedure
   Static SetValue := "Set"         ; take over Values
   Static Register := "RCB"         ; RegisterCallBack
   Static ValueList := ""           ; Values

   ; Aufruf als Fensterprozedur?
   If (A_EventInfo <> NewWinProc) {
      If (Msg = SetValue) {
         If (RegExMatch(ValueList, "m)^" . (Hwnd +0) . "\|")) {
            ValueList := RegExReplace(ValueList
                                     , "m)^" . (Hwnd + 0) . "\|.*$"
                                     , (Hwnd + 0) . "|"
                                     . (wParam + 0) . "|"
                                     . (lParam + 0))
         } Else {
            ValueList .= (Hwnd + 0) . "|"
                      .  (wParam + 0) . "|"
                      .  (lParam + 0) .  "`r`n"
         }
         Return
      }
      If (Msg = Register) {
         If (NewWinProc = "") {
            NewWinProc := RegisterCallback("Control_Colors","",4)
            OldWinProc := DllCall("SetWindowLong"
                                 , UInt, Hwnd
                                 , Int, -4
                                 , Int, NewWinProc
                                 , UInt)
         }
         Return
      }
      Return
   }
   ; 0x0133 : WM_CTLCOLOREDIT
   ; 0x0138 : WM_CTLCOLORSTATIC
   If (Msg = 0x0133 Or Msg = 0x0135 Or Msg = 0x0138) {
      If (RegExMatch(ValueList, "m)^"
                     . (lParam + 0) . "\|(?P<BG>\d+)\|(?P<TX>\d+)$"
                     , C)) {
         DllCall("SetTextColor", UInt, wParam, UInt, CTX)
         DllCall("SetBkColor", UInt, wParam, UInt, CBG)
         Return, DllCall("CreateSolidBrush", UInt, CBG)
      }
   }
   Return DllCall("CallWindowProcA"
                  , UInt, OldWinProc
                  , UInt, Hwnd
                  , UInt, Msg
                  , UInt, wParam
                  , UInt, lParam)
}

i just expanded the example from helpfile about RCB / denick got curious and expanded the function to a more mature function (which i slightly modified to work bit more generic)

originally published here: http://de.autohotkey...opic.php?t=2570

greets
DerRaphael

All scripts, unless otherwise noted, are hereby released under CC-BY

CannedCheese
  • Members
  • 120 posts
  • Last active: Sep 24 2014 04:31 PM
  • Joined: 21 May 2008
Don't know why no one has commented on this yet, it looks sweet. This is the first time I've actually tried, but I'm guessing this is required to even write colored text onto the default gui background color?

Like for
Gui, Font, S8 Bold cRed, Futurist Condensed
Gui, Add, CheckBox,0x1000 x420 y230 w50 h30 Unchecked

isn't going to write in red text if I recall correctly. Looking forward to giving your script a try very soon.

[edit] Ug, guess the code I posted would list colored text if the 0x1000 switch wasn't used. Any chance your code addresses this type of modification to the control? Is it possible to use this to get colored text in a combobox? (dropdownlists will work fine if you set the font color but comboboxes don't post in the correct color in my experience).

Ice_Tea
  • Members
  • 131 posts
  • Last active: Aug 25 2010 11:11 AM
  • Joined: 12 Jan 2008
Looks cool, I like the example you gave with the radio button as a checkbox, as it looked very good on winxp... I'll add this to my library... I know that I'll definitely use it on the radio buttons, but with a bit more "adjusted" colors, these are kind of basic.
Good Job!

Klaus
  • Members
  • 333 posts
  • Last active: Feb 17 2015 09:31 AM
  • Joined: 12 May 2005
Hi, Community,
I was very pleased to find this function to colour a control's background and text color, but I think it does not work in gui's higher than 1. Here is what I tried, it's just a slightly changed middle section of DerRaphael's example:
Gui 2:  Margin, 10, 10
Gui 2: Font, s12

Gui 2: Add, Text, vTXT1 HwndTX1ID w300 y10 x10 CENTER , TEXT
Control_Colors("TXT1", "Set", Green, White)

Gui, +LastFound
GuiID := WinExist()
Control_Colors(GuiID, "RCB", 0, 0)

Gui 2: Show, , Colors
Return

GuiEscape:
GuiClose:
   ExitApp
Tell me, where's my mistake!
Thanks in advance,
Klaus

SKAN
  • Administrators
  • 9115 posts
  • Last active:
  • Joined: 26 Dec 2005
Gui, [color=red]2:[/color]+LastFound

Also you have to initialise the vars Green and White.
kWo4Lk1.png

Klaus
  • Members
  • 333 posts
  • Last active: Feb 17 2015 09:31 AM
  • Joined: 12 May 2005
Hi, SKAN,
thanks for your rapid reply, now my code looks like this
Gui 2:  Margin, 10, 10
Gui 2: Font, s12

Gui 2: Add, Text, vTXT1 HwndTX1ID w300 y10 x10 CENTER , gefundene Zeile!
Control_Colors("TXT1", "Set", Green, Red)

Gui, 2:+LastFound
GuiID := WinExist()
Control_Colors(GuiID, "RCB", 0, 0)

Gui 2: Show, , Colors
Return

GuiEscape:
GuiClose:
   ExitApp
but it won't work, either.
Could you have another look?
Thank you,
Klaus

mE
  • Guests
  • Last active:
  • Joined: --
You need to this part (or at least green and white) from his original post.
; HTML Colors in BGR-format
Black = 0x000000
Green = 0x008000
Silver = 0xC0C0C0
Lime = 0x00FF00
Gray = 0x808080
Olive = 0x800080
White = 0xFFFFFF
Yellow = 0x00FFFF
Maroon = 0x000080
Navy = 0x800000
Red = 0x0000FF
Blue = 0xFF0000
Purple = 0x800080
Teal = 0x808000
Fuchsia = 0xFF00FF
Aqua = 0xFFFF00
Otherwise you are using null or undefined variables (I'm not sure if AutoHotkey just assumes them as null when it hits an undefined variable or not).

klaus. nli
  • Guests
  • Last active:
  • Joined: --
Hi, mE,
of course I have these colour definitions in my sample script. As I wrote my posted code is just the "middle section" of the original example, at the beginning the blacks, greens and so on and at the end the colouring function.
Thanks for your hint,
Klaus

klaus. nli
  • Guests
  • Last active:
  • Joined: --
Hi, Community,
I would not like to appear presumptuous, but are there any ideas?
Thanks in advance,
Klaus

denick (n-l-i)
  • Guests
  • Last active:
  • Joined: --
Hi, Klaus,

would you please visit the german forum?

klaus. nli
  • Guests
  • Last active:
  • Joined: --
Hi, denick,

thank you, I already stumbled across the post in the german forum which seems to be the root of this thread.

I have already expanded that script from the german forum. Now it also changes the text colour, not only the background.

But now there's the next problem:
The function works pretty good when only get launched once at run time of the script. When you change the colour values again and call the function again, the whole script freezes and has to be removed by the task manager.

To explain the background: I'd like to create a colour chooser using six edit fields with updown-controls representing R, G and B of textcolour and background colour. After each change of one of the six edit controls I want ot call the colour changing function to see the colour change in real time.
But a second call does not work until now.

Any ideas?
Regards,
Klaus

denick (n-l-i)
  • Guests
  • Last active:
  • Joined: --
Hi, Klaus,

are you trying to call Control_Colors(GuiID, "RCB", 0, 0) a second time? You must not do that, use Winset, Redraw for the control's Hwnd or the whole window instead!

BTW: There is a new Version of Control_Colors at the bottom of the german thread. :wink:

klaus. nli
  • Guests
  • Last active:
  • Joined: --
Hi, denick,
you're right, I did the Control_Colors(...) a second time. I'll try to realize a redraw with new colour values with Winset, ...
Thanks a lot, I'll have a look at the german forum!
Klaus

  • Guests
  • Last active:
  • Joined: --
You can't call a method that isn't includded in your code.

darklight_tr
  • Members
  • 31 posts
  • Last active: Feb 11 2014 04:32 AM
  • Joined: 29 May 2008
I used this function for quite a while but recently had to abandon it. There appears to be a bug that causes a script to hang when you try to resize a GUI window.

When you eliminate this function from your script you are able to resize your GUI again.