For some reason, I just can't seem to find a way to change the background color of a single edit control. I have tried guicontrol +background among other things, and I just can't get it to work. Any help?

Edit Control Background Color
Started by
lordkorgen
, Sep 02 2008 07:15 PM
23 replies to this topic
#1
-
Posted 02 September 2008 - 07:15 PM

I just can't seem to find a way to change the background color of a single edit control.
You need to subclass the control. There is an example shown in AHK Doc under RegisterCallback() that shows how to subclass a Text control.
I have minimally adapted it for Edit control.
TextBackgroundColor := [color=red]0xD8D0A7[/color] ; A custom color in BGR format. TextBackgroundBrush := DllCall("CreateSolidBrush", UInt, TextBackgroundColor) [color=red]Gui, Add, Edit, w320 h240 HwndMyTextHwnd, Here is some text that is given`na custom background color.[/color] Gui +LastFound GuiHwnd := WinExist() WindowProcNew := RegisterCallback("WindowProc", "" ; "" to avoid fast-mode for subclassing. , 4, MyTextHwnd) ; Must specify exact ParamCount when EventInfo parameter is present. WindowProcOld := DllCall("SetWindowLong", UInt, GuiHwnd, Int, -4 ; -4 is GWL_WNDPROC , Int, WindowProcNew, UInt) ; Return value must be set to UInt vs. Int. Gui Show return WindowProc(hwnd, uMsg, wParam, lParam) { Critical global TextBackgroundColor, TextBackgroundBrush, WindowProcOld [color=red]if (uMsg = 0x133 && lParam = A_EventInfo) [/color] ; WM_CTLCOLOREDIT = 0x133 { DllCall("SetBkColor", UInt, wParam, UInt, TextBackgroundColor) return TextBackgroundBrush ; Return the HBRUSH to notify the OS that we altered the HDC. } ; Otherwise (since above didn't return), pass all unhandled events to the original WindowProc. return DllCall("CallWindowProcA", UInt, WindowProcOld, UInt, hwnd, UInt, uMsg, UInt, wParam, UInt, lParam) } GuiClose: ExitApp
Edit: Line feed inserted before Gui+LastFound .. Thanks to Slanter
#2
-
Posted 02 September 2008 - 07:30 PM

You missed a line feed in there skan :wink:
#3
-
Posted 02 September 2008 - 08:41 PM

Unless otherwise stated, all code is untested
(\__/) This is Bunny.
(='.'=) Cut, copy, and paste bunny onto your sig.
(")_(") Help Bunny gain World Domination.
(\__/) This is Bunny.
(='.'=) Cut, copy, and paste bunny onto your sig.
(")_(") Help Bunny gain World Domination.
I'm not sure if I should start a new thread for this. I tried to turn it into a function, but when I try to call the function twice it crashes the script:
Gui, Add, Edit, w320 h240 HwndMyTextHwnd, Here is some text that is given`na custom background color. Gui, Add, Button, gblue, blue Gui, Add, Button, x+5 ggrey, grey Gui +LastFound GuiHwnd := WinExist() Gui Show return blue: SetBgColor( 0xE7E3DE ) return grey: SetBgColor( 0xEFEFEF ) return SetBgColor( bg ) { Global TextBackgroundColor := bg TextBackgroundBrush := DllCall("CreateSolidBrush", UInt, TextBackgroundColor) WindowProcNew := RegisterCallback("WindowProc", "" ; "" to avoid fast-mode for subclassing. , 4, MyTextHwnd) ; Must specify exact ParamCount when EventInfo parameter is present. WindowProcOld := DllCall("SetWindowLong", UInt, GuiHwnd, Int, -4 ; -4 is GWL_WNDPROC , Int, WindowProcNew, UInt) ; Return value must be set to UInt vs. Int. Winset, Redraw } WindowProc(hwnd, uMsg, wParam, lParam) { Critical global TextBackgroundColor, TextBackgroundBrush, WindowProcOld if (uMsg = 0x133 && lParam = A_EventInfo) ; WM_CTLCOLOREDIT = 0x133 { DllCall("SetBkColor", UInt, wParam, UInt, TextBackgroundColor) return TextBackgroundBrush ; Return the HBRUSH to notify the OS that we altered the HDC. } ; Otherwise (since above didn't return), pass all unhandled events to the original WindowProc. return DllCall("CallWindowProcA", UInt, WindowProcOld, UInt, hwnd, UInt, uMsg, UInt, wParam, UInt, lParam) } GuiClose: ExitApp
#5
-
Posted 02 September 2008 - 09:36 PM

"Anything worth doing is worth doing slowly." - Mae West

Noooooooooooooooooooooooo! I hate dllcall! I don't understand a word of that code you gave me! It's just way too much work for one aspect of an interface! I'm sorry, but could you simplify it please? :-)
#6
-
Posted 02 September 2008 - 09:38 PM

Now I just saw serenity's post and I don't like that either. I'm really sorry guys, but I DON'T GET IT!
#7
-
Posted 02 September 2008 - 09:40 PM

Turning it into a function would make it easier to (re)use. Just ignore the DllCall code, post it at the bottom of your script out of the way.

#8
-
Posted 02 September 2008 - 09:44 PM

"Anything worth doing is worth doing slowly." - Mae West

I don't know what to say. Thank you? No, that would be too much. People have asked for this for a long time and yet it still requires a lot of scripting. What about:
Gui, Add, Edit, h18 w100 vMyEdit bgRedWouldn't this be nice? Mark whoever needs to do something about this...
#9
-
Posted 11 September 2008 - 04:19 AM

lol I know, I'm just saying...
Anyways, there are many more GuiControl features that have yet to be added, like GuiControl, Destroy, you know?
Anyways, there are many more GuiControl features that have yet to be added, like GuiControl, Destroy, you know?
#11
-
Posted 11 September 2008 - 10:10 PM

To anyone who still might be wondering here's a simple solution: use the last parameter of the Gui, Color command to set the background color of the controls...
Gui, Color, 000000, FFFFFF
#12
-
Posted 24 November 2008 - 11:34 AM

NO... It changes the colors for ALL the controls... not just a single one. Read the topic

#13
-
Posted 20 January 2009 - 09:16 AM

Serenity:
I got a solution for you...check it out.... ok better late than never right???
Just don't ask me to explain that LOL....mainly because I don't know how....
Basically, you only need to do the RegisterCallback once... opening the communication lines basically so the reason why it crashed was because RegisterCallback was already accessed and when the second button was hit, BLAM... sorry charlie, its open to someone else you gots to close it first...(if I'm understanding it correctly..)
Then you can do the color how ever many times you want
I moved the code in red from the code in Dark Red
Ok, I'm terrible at explaining this does someone want to save me from humiliation?
I got a solution for you...check it out.... ok better late than never right???
;Set the initial background color TextBackgroundColor := 0xFFFFFF TextBackgroundBrush := DllCall("CreateSolidBrush", UInt, TextBackgroundColor) Gui, Add, Edit, w320 h240 HwndMyTextHwnd, Here is some text that is given`na custom background color. Gui, Add, Button, gblue, blue Gui, Add, Button, x+5 ggrey, grey Gui +LastFound GuiHwnd := WinExist() [color=red] WindowProcNew := RegisterCallback("WindowProc", "" ; "" to avoid fast-mode for subclassing. , 4, MyTextHwnd) ; Must specify exact ParamCount when EventInfo parameter is present. WindowProcOld := DllCall("SetWindowLong", UInt, GuiHwnd, Int, -4 ; -4 is GWL_WNDPROC , Int, WindowProcNew, UInt) ; Return value must be set to UInt vs. Int.[/color] Gui Show return blue: SetBgColor( 0xE7E3DE ) return grey: SetBgColor( 0x00FFFF ) return [color=darkred]SetBgColor( bg ) { Global TextBackgroundColor := bg TextBackgroundBrush := DllCall("CreateSolidBrush", UInt, TextBackgroundColor) Winset, Redraw }[/color] WindowProc(hwnd, uMsg, wParam, lParam) { Critical global TextBackgroundColor, TextBackgroundBrush, WindowProcOld if (uMsg = 0x133 && lParam = A_EventInfo) ; WM_CTLCOLOREDIT = 0x133 { DllCall("SetBkColor", UInt, wParam, UInt, TextBackgroundColor) return TextBackgroundBrush ; Return the HBRUSH to notify the OS that we altered the HDC. } ; Otherwise (since above didn't return), pass all unhandled events to the original WindowProc. return DllCall("CallWindowProcA", UInt, WindowProcOld, UInt, hwnd, UInt, uMsg, UInt, wParam, UInt, lParam) } GuiClose: ExitApp
Just don't ask me to explain that LOL....mainly because I don't know how....
Basically, you only need to do the RegisterCallback once... opening the communication lines basically so the reason why it crashed was because RegisterCallback was already accessed and when the second button was hit, BLAM... sorry charlie, its open to someone else you gots to close it first...(if I'm understanding it correctly..)
Then you can do the color how ever many times you want
I moved the code in red from the code in Dark Red
Ok, I'm terrible at explaining this does someone want to save me from humiliation?
#14
-
Posted 10 March 2010 - 06:43 PM

Thanks! This is almost exactly what I'm looking for.
I do have two slight problems implementing this code into my project, however. First, I have two separate text boxes for which I want to change the background color for. So I'd like to call this function twice.
Secondly, this code changes not only the background color, but it overrides my cRed color specification for the text forground color. And I have no idea how to modify this code to either leave the forground color alone or change it to red.
I do have two slight problems implementing this code into my project, however. First, I have two separate text boxes for which I want to change the background color for. So I'd like to call this function twice.
Secondly, this code changes not only the background color, but it overrides my cRed color specification for the text forground color. And I have no idea how to modify this code to either leave the forground color alone or change it to red.
#NoTrayIcon TextBackgroundColor := 0xFFFFFF ; A custom color in BGR format. TextBackgroundBrush := DllCall("CreateSolidBrush", UInt, TextBackgroundColor) Gui +LastFound ; Make the GUI window the last found window for use by the line below. WinSet, Transparent, 210 Gui, Add, Text, x86 y20 w312 h98 0x6 , Gui, Add, GroupBox, x90 y18 w304 h96 , Gui, Font, S28 cRed Bold, Arial Gui, Add, Text, x136 y34 w220 h40 HwndMyTextHwnd, JAVERSION Gui +LastFound GuiHwnd := WinExist() Gui, Font, S18 cRed Bold, Arial Gui, Add, Text, x104 y71 w280 h30 HwndMyTextHwnd, Java Version Verifier 0.7 Gui +LastFound GuiHwnd := WinExist() WindowProcNew := RegisterCallback("WindowProc", "" ; Specifies "" to avoid fast-mode for subclassing. , 4, MyTextHwnd) ; Must specify exact ParamCount when EventInfo parameter is present. WindowProcOld := DllCall("SetWindowLong", UInt, GuiHwnd, Int, -4 ; -4 is GWL_WNDPROC , Int, WindowProcNew, UInt) ; Return value must be set to UInt vs. Int. Gui, Font, S10 cBlack, Arial Gui, Add, Text, x17 y140 w450 h270 , Some More Text Gui, Show, w486 h434, Javersion - Java Version Verifier Return GuiClose: ExitApp WindowProc(hwnd, uMsg, wParam, lParam) { Critical global TextBackgroundColor, TextBackgroundBrush, WindowProcOld if (uMsg = 0x138 && lParam = A_EventInfo) ; 0x138 is WM_CTLCOLORSTATIC. { DllCall("SetBkColor", UInt, wParam, UInt, TextBackgroundColor) return TextBackgroundBrush ; Return the HBRUSH to notify the OS that we altered the HDC. } ; Otherwise (since above didn't return), pass all unhandled events to the original WindowProc. return DllCall("CallWindowProcA", UInt, WindowProcOld, UInt, hwnd, UInt, uMsg, UInt, wParam, UInt, lParam) }
#15
-
Posted 30 January 2011 - 10:33 AM
