AutoHotkey Community

It is currently May 27th, 2012, 5:23 am

All times are UTC [ DST ]




Post new topic Reply to topic  [ 20 posts ]  Go to page 1, 2  Next
Author Message
PostPosted: September 2nd, 2008, 8:15 pm 
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?


Report this post
Top
  
Reply with quote  
PostPosted: September 2nd, 2008, 8:30 pm 
Offline
User avatar

Joined: December 26th, 2005, 4:40 pm
Posts: 8776
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
StartEx - Portable Shortcut Link


Last edited by SKAN on September 2nd, 2008, 9:46 pm, edited 2 times in total.

Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: September 2nd, 2008, 9:41 pm 
Offline

Joined: May 28th, 2008, 2:11 am
Posts: 739
Location: Minnesota, USA
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.


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: September 2nd, 2008, 9:45 pm 
Offline
User avatar

Joined: December 26th, 2005, 4:40 pm
Posts: 8776
Thanks Slanter. :)


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: September 2nd, 2008, 10:36 pm 
Offline

Joined: November 8th, 2004, 12:46 am
Posts: 1271
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
Image


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: September 2nd, 2008, 10:38 pm 
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? :-)


Report this post
Top
  
Reply with quote  
 Post subject:
PostPosted: September 2nd, 2008, 10:40 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!


Report this post
Top
  
Reply with quote  
 Post subject:
PostPosted: September 2nd, 2008, 10:44 pm 
Offline

Joined: November 8th, 2004, 12:46 am
Posts: 1271
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
Image


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: September 11th, 2008, 5:19 am 
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...


Report this post
Top
  
Reply with quote  
 Post subject:
PostPosted: September 11th, 2008, 1:19 pm 
Offline

Joined: July 21st, 2008, 4:16 pm
Posts: 726
Location: Calgary, AB, Canada
That's what the WishList forum is for.


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: September 11th, 2008, 11:10 pm 
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?


Report this post
Top
  
Reply with quote  
 Post subject:
PostPosted: November 24th, 2008, 12:34 pm 
Offline

Joined: November 17th, 2008, 10:47 am
Posts: 2
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


Report this post
Top
 Profile  
Reply with quote  
 Post subject: nowotny
PostPosted: January 20th, 2009, 10:16 am 
Offline

Joined: January 13th, 2009, 8:15 am
Posts: 25
NO... It changes the colors for ALL the controls... not just a single one. Read the topic :)


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: March 10th, 2010, 7:43 pm 
Offline

Joined: November 19th, 2009, 6:23 pm
Posts: 163
Location: Florida
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?


Report this post
Top
 Profile  
Reply with quote  
PostPosted: January 30th, 2011, 11:33 am 
Offline

Joined: December 17th, 2010, 5:44 am
Posts: 8
Location: Missouri, United States
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)
}


Report this post
Top
 Profile  
Reply with quote  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 20 posts ]  Go to page 1, 2  Next

All times are UTC [ DST ]


Who is online

Users browsing this forum: Exabot [Bot], HotkeyStick, rbrtryn, XstatyK and 85 guests


You can post new topics in this forum
You can reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Powered by phpBB® Forum Software © phpBB Group