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 

Edit Control Background Color
Goto page 1, 2  Next
 
Reply to topic    AutoHotkey Community Forum Index -> Ask for Help
View previous topic :: View next topic  
Author Message
lordkorgen
Guest





PostPosted: Tue Sep 02, 2008 7:15 pm    Post subject: Edit Control Background Color Reply with quote

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?
Back to top
SKAN



Joined: 26 Dec 2005
Posts: 8688

PostPosted: Tue Sep 02, 2008 7:30 pm    Post subject: Re: GuiControl Background Color Reply with quote

lordkorgen wrote:
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.

Code:
TextBackgroundColor := 0xD8D0A7  ; A custom color in BGR format.
TextBackgroundBrush := DllCall("CreateSolidBrush", UInt, TextBackgroundColor)

Gui, Add, Edit, w320 h240 HwndMyTextHwnd, Here is some text that is given`na custom background 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
    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


Edit: Line feed inserted before Gui+LastFound .. Thanks to Slanter
_________________
URLGet - Internet Explorer based Downloader


Last edited by SKAN on Tue Sep 02, 2008 8:46 pm; edited 2 times in total
Back to top
View user's profile Send private message Send e-mail
Slanter



Joined: 28 May 2008
Posts: 739
Location: Minnesota, USA

PostPosted: Tue Sep 02, 2008 8:41 pm    Post subject: Reply with quote

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.
Back to top
View user's profile Send private message Visit poster's website
SKAN



Joined: 26 Dec 2005
Posts: 8688

PostPosted: Tue Sep 02, 2008 8:45 pm    Post subject: Reply with quote

Thanks Slanter. Smile
Back to top
View user's profile Send private message Send e-mail
Serenity



Joined: 07 Nov 2004
Posts: 1271

PostPosted: Tue Sep 02, 2008 9:36 pm    Post subject: Reply with quote

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:

Code:
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
Back to top
View user's profile Send private message Visit poster's website
lordkorgen
Guest





PostPosted: Tue Sep 02, 2008 9:38 pm    Post subject: Reply with quote

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





PostPosted: Tue Sep 02, 2008 9:40 pm    Post subject: Reply with quote

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!
Back to top
Serenity



Joined: 07 Nov 2004
Posts: 1271

PostPosted: Tue Sep 02, 2008 9:44 pm    Post subject: Reply with quote

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. Wink
_________________
"Anything worth doing is worth doing slowly." - Mae West
Back to top
View user's profile Send private message Visit poster's website
lordkorgen
Guest





PostPosted: Thu Sep 11, 2008 4:19 am    Post subject: Reply with quote

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:

Code:

Gui, Add, Edit, h18 w100 vMyEdit bgRed

Wouldn't this be nice? Mark whoever needs to do something about this...
Back to top
Sivvy



Joined: 21 Jul 2008
Posts: 726
Location: Calgary, AB, Canada

PostPosted: Thu Sep 11, 2008 12:19 pm    Post subject: Reply with quote

That's what the WishList forum is for.
Back to top
View user's profile Send private message
lordkorgen
Guest





PostPosted: Thu Sep 11, 2008 10:10 pm    Post subject: Reply with quote

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?
Back to top
nowotny



Joined: 17 Nov 2008
Posts: 2

PostPosted: Mon Nov 24, 2008 11:34 am    Post subject: Reply with quote

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...
Code:
Gui, Color, 000000, FFFFFF
Back to top
View user's profile Send private message
alarian



Joined: 13 Jan 2009
Posts: 25

PostPosted: Tue Jan 20, 2009 9:16 am    Post subject: nowotny Reply with quote

NO... It changes the colors for ALL the controls... not just a single one. Read the topic Smile
Back to top
View user's profile Send private message
The Unknown Jobber



Joined: 19 Nov 2009
Posts: 161
Location: Florida

PostPosted: Wed Mar 10, 2010 6:43 pm    Post subject: Reply with quote

Serenity:

I got a solution for you...check it out.... ok better late than never right???
Code:

;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() 

   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

blue:
SetBgColor( 0xE7E3DE )
return

grey:
SetBgColor( 0x00FFFF )
return

SetBgColor( bg )
{
   Global
   TextBackgroundColor := bg
   TextBackgroundBrush := DllCall("CreateSolidBrush", UInt, TextBackgroundColor)
   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


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?
Back to top
View user's profile Send private message Visit poster's website
xpace



Joined: 17 Dec 2010
Posts: 8
Location: Missouri, United States

PostPosted: Sun Jan 30, 2011 10:33 am    Post subject: How about repeating that? Reply with quote

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.

Code:
#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)
}
Back to top
View user's profile Send private message
Display posts from previous:   
Reply to topic    AutoHotkey Community Forum Index -> Ask for Help All times are GMT
Goto page 1, 2  Next
Page 1 of 2

 
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