Jump to content

Sky Slate Blueberry Blackcurrant Watermelon Strawberry Orange Banana Apple Emerald Chocolate
Photo

Gui Color question: making the background change behind text


  • Please log in to reply
6 replies to this topic
POINTS
  • Members
  • 290 posts
  • Last active: Oct 13 2010 02:12 AM
  • Joined: 17 Jan 2006
I'm basically trying to do the opposite of what this code is doing. I want the background behind the word Red to be black and the rest of the gui to be default. Is there a way to apply the "-Background" command to the main window?

#SingleInstance Force
Gui, Add, Text, cRed -Background, Red
Gui, Color, Black
Gui, Add, Text, cLime, Lime
Gui, Add, Text, , Default
Gui, Show, x200 y200 w400 h400


PhiLho
  • Moderators
  • 6850 posts
  • Last active: Jan 02 2012 10:09 PM
  • Joined: 27 Dec 2005
I tried this:
WM_CTLCOLORSTATIC=0x138
b = 128
g = 255
r = 255
brush := DllCall("CreateSolidBrush"
		, "UInt", RGB(r, g, b))
OnMessage(WM_CTLCOLORSTATIC, "Colorize")

Gui Add, Button, gTest, OK
Gui Add, Text, vLabel x70 y30 cBlue, (Dummy Text)
Gui Add, Text, vDebug x10 y70 cGrey w180, /
Gui Show, w200 h100
GuiControl Text, Label, Colored Text with Background!

Return

Test:
	i++
	GuiControl Text, Debug, Test %i%
Return

GuiClose:
GuiEscape:
	; Probably uneeded as it is released on program exit, but cleaner this way
	DllCall("DeleteObject"
			, "UInt", brush)
ExitApp

Colorize(wParam, lParam, msg, hwnd)
{
	global	; for brush
	DllCall("SetTextColor"
			, "UInt", wParam
			, "UInt", RGB(0, 0, 255))
	DllCall("SetBkMode"
			, "UInt", wParam
			, "UInt", 1)	; TRANSPARENT
	Return brush
}

; COLORREF = DWORD = UInt, format: 0x00bbggrr (blue green red)
RGB(r, g, b)
{
	Return (b << 16) + (g << 8) + r
}
but it doesn't work well... I was about to write "doesn't work", but I found strange results...
[EDIT] Updated code for better results, but still an issue when repainting the window. See following messages.


The Colorize function isn't called, unless I perform an action: button click or hotkey press.
And even so, only one static control is changed.

I guess that actually, there is conflict between AutoHotkey's internal handling of this message (which return system default color) and this attempt to take control of this message.

Or maybe I miss something. I let others experiment with this idea...
Posted Image vPhiLho := RegExReplace("Philippe Lhoste", "^(\w{3})\w*\s+\b(\w{3})\w*$", "$1$2")

POINTS
  • Members
  • 290 posts
  • Last active: Oct 13 2010 02:12 AM
  • Joined: 17 Jan 2006
This will get both things to change color.

d::
Test:
   i++
   GuiControl Text, Debug1, Test %i%
   GuiControl Text, Debug2, Call %i%
Return

Thanks for your help, this should do what I need.

PhiLho
  • Moderators
  • 6850 posts
  • Last active: Jan 02 2012 10:09 PM
  • Joined: 27 Dec 2005
Oh, no need for action then, the label must be updated after the Show command:

Gui Show, w200 h100
GuiControl Text, Label, One
GuiControl Text, Debug1, Two
GuiControl Text, Debug2, Three

That was the missing link...
[EDIT] Not perfect, each time the window is repainted (eg. going behind another window and coming back to front), it gets again the original color... That's when internal coloring kicks in...

The Label text color goes back to default, you have to set it to the wanted color in the Colorize function with something like:
DllCall("SetTextColor"
			, "UInt", wParam
			, "UInt", RGB(0, 0, 255))
You can change the color of only one Text component (or giving different colors per component) by checking its handle (HWND) in the lParam.

Of course, you can remove the debug c++ / GuiControl lines from the Colorize function. Just keep the global command.

Oh, I will just update the first post to give correct code to cut & paste...
Posted Image vPhiLho := RegExReplace("Philippe Lhoste", "^(\w{3})\w*\s+\b(\w{3})\w*$", "$1$2")

POINTS
  • Members
  • 290 posts
  • Last active: Oct 13 2010 02:12 AM
  • Joined: 17 Jan 2006
Actually I need the background behind some images to be black. The problem is that when I reload an image, you can see the default gui color flicker before the image is loaded.

How would we modify your code to work behind images?

Chris
  • Administrators
  • 10727 posts
  • Last active:
  • Joined: 02 Mar 2004
You could try putting a Picture control behind the text control as a background. Such an image could probably be a 1-pixel GIF or BMP file consisting of the desired background color. That single pixel should get stretched automatically if you specify a width and height for the Picture control.

There are some tips in the help file about using Picture controls as backgrounds for other controls. It is also planned to have support for custom background colors for individual Text controls someday.

PhiLho
  • Moderators
  • 6850 posts
  • Last active: Jan 02 2012 10:09 PM
  • Joined: 27 Dec 2005
Yes, this hack isn't really usable... :-)
I even tried to update the Text string on WM_NCPAINT (or even WM_PAINT), without result.
I can update it with a timer, but this is an overkill, and the text flickers, which is ugly...

So we have to wait for a future release of AutoHotkeys, allowing lot of flashy colored controls, to make your application looks like a baby toy :-)

I joke, but some carefully chosen colored labels can be effective to draw attention of users on some points.
Posted Image vPhiLho := RegExReplace("Philippe Lhoste", "^(\w{3})\w*\s+\b(\w{3})\w*$", "$1$2")