AutoHotkey Homepage AutoHotkey Community
Let's help each other out
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

Colored Controls (no bitmaps needed)

 
Post new topic   Reply to topic    AutoHotkey Community Forum Index -> Scripts & Functions
View previous topic :: View next topic  
Author Message
DerRaphael



Joined: 23 Nov 2007
Posts: 604
Location: 127.0.0.1

PostPosted: Thu Jul 17, 2008 11:26 am    Post subject: Colored Controls (no bitmaps needed) Reply with quote

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.




Code:
#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.com/forum/viewtopic.php?t=2570

greets
DerRaphael
_________________
Back to top
View user's profile Send private message
CannedCheese



Joined: 21 May 2008
Posts: 85

PostPosted: Tue Jul 22, 2008 1:18 am    Post subject: Reply with quote

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
Code:

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).
Back to top
View user's profile Send private message
Ice_Tea



Joined: 12 Jan 2008
Posts: 114

PostPosted: Fri Jul 25, 2008 3:32 am    Post subject: Reply with quote

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!
Back to top
View user's profile Send private message
Klaus



Joined: 12 May 2005
Posts: 205
Location: Münster, Germany

PostPosted: Tue Aug 05, 2008 12:15 pm    Post subject: Reply with quote

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:
Code:
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
Back to top
View user's profile Send private message Send e-mail
SKAN



Joined: 26 Dec 2005
Posts: 6264

PostPosted: Tue Aug 05, 2008 12:50 pm    Post subject: Reply with quote

Code:
Gui, 2:+LastFound


Also you have to initialise th vars Green and White.
_________________
Back to top
View user's profile Send private message
Klaus



Joined: 12 May 2005
Posts: 205
Location: Münster, Germany

PostPosted: Tue Aug 05, 2008 1:00 pm    Post subject: Reply with quote

Hi, SKAN,
thanks for your rapid reply, now my code looks like this
Code:
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
Back to top
View user's profile Send private message Send e-mail
mE
Guest





PostPosted: Tue Aug 05, 2008 4:28 pm    Post subject: Reply with quote

You need to this part (or at least green and white) from his original post.
Code:
; 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).
Back to top
klaus. nli
Guest





PostPosted: Wed Aug 06, 2008 4:46 am    Post subject: Reply with quote

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
Back to top
klaus. nli
Guest





PostPosted: Wed Aug 06, 2008 7:08 am    Post subject: Reply with quote

Hi, Community,
I would not like to appear presumptuous, but are there any ideas?
Thanks in advance,
Klaus
Back to top
denick (n-l-i)
Guest





PostPosted: Wed Aug 06, 2008 11:53 am    Post subject: Reply with quote

Hi, Klaus,

would you please visit the german forum?
Back to top
klaus. nli
Guest





PostPosted: Wed Aug 06, 2008 12:12 pm    Post subject: Reply with quote

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
Back to top
denick (n-l-i)
Guest





PostPosted: Wed Aug 06, 2008 2:12 pm    Post subject: Reply with quote

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
Back to top
klaus. nli
Guest





PostPosted: Wed Aug 06, 2008 4:04 pm    Post subject: Reply with quote

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
Back to top
Guest






PostPosted: Sun Oct 19, 2008 9:45 am    Post subject: Reply with quote

You can't call a method that isn't includded in your code.
Back to top
Display posts from previous:   
Post new topic   Reply to topic    AutoHotkey Community Forum Index -> Scripts & Functions All times are GMT
Page 1 of 1

 
Jump to:  
You can post new topics in this forum
You can reply to topics in this forum


Powered by phpBB © 2001, 2005 phpBB Group