Jump to content

Sky Slate Blueberry Blackcurrant Watermelon Strawberry Orange Banana Apple Emerald Chocolate

Edit Control Background Color


  • Please log in to reply
23 replies to this topic
lordkorgen
  • Guests
  • Last active:
  • Joined: --
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?

SKAN
  • Administrators
  • 9115 posts
  • Last active:
  • Joined: 26 Dec 2005

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
kWo4Lk1.png

Slanter
  • Members
  • 739 posts
  • Last active: Jul 08 2011 05:26 AM
  • Joined: 28 May 2008
You missed a line feed in there skan :wink:
Unless otherwise stated, all code is untested

(\__/) This is Bunny.
(='.'=) Cut, copy, and paste bunny onto your sig.
(")_(") Help Bunny gain World Domination.

SKAN
  • Administrators
  • 9115 posts
  • Last active:
  • Joined: 26 Dec 2005
Thanks Slanter. :)

Serenity
  • Members
  • 1271 posts
  • Last active:
  • Joined: 07 Nov 2004
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

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

lordkorgen
  • Guests
  • Last active:
  • Joined: --
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? :-)

lordkorgen
  • Guests
  • Last active:
  • Joined: --
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!

Serenity
  • Members
  • 1271 posts
  • Last active:
  • Joined: 07 Nov 2004
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. ;)
"Anything worth doing is worth doing slowly." - Mae West
Posted Image

lordkorgen
  • Guests
  • Last active:
  • Joined: --
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 bgRed
Wouldn't this be nice? Mark whoever needs to do something about this...

Sivvy
  • Members
  • 726 posts
  • Last active: Apr 23 2010 02:50 PM
  • Joined: 21 Jul 2008
That's what the WishList forum is for.

lordkorgen
  • Guests
  • Last active:
  • Joined: --
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?

nowotny
  • Members
  • 2 posts
  • Last active: Mar 17 2009 04:54 PM
  • Joined: 17 Nov 2008
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


alarian
  • Members
  • 25 posts
  • Last active: May 22 2009 01:08 AM
  • Joined: 13 Jan 2009
NO... It changes the colors for ALL the controls... not just a single one. Read the topic :)

frescalus
  • Members
  • 163 posts
  • Last active: Dec 14 2012 09:34 PM
  • Joined: 19 Nov 2009
Serenity:

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?

xpace
  • Members
  • 9 posts
  • Last active: Feb 20 2013 06:46 PM
  • Joined: 17 Dec 2010
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.

#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)
}