Jump to content

Sky Slate Blueberry Blackcurrant Watermelon Strawberry Orange Banana Apple Emerald Chocolate

Edit text and background color


  • Please log in to reply
11 replies to this topic
  • Guests
  • Last active:
  • Joined: --
Is there a way to change the text and the background color of an edit control?
I searched the forum and only found a way to change the background but not the text color.

CodeKiller
  • Members
  • 2067 posts
  • Last active: Feb 26 2016 09:30 AM
  • Joined: 10 Jul 2008
Gui Font, cRed

Gui Add, Edit, vE1, Text

Gui Font ; Restore previous settings

Gui Add, Edit, vE2, Text

Gui Show


  • Guests
  • Last active:
  • Joined: --
Hello CodeKiller,
I don't think you understood what I want, I know how to change edit text color, what I want is to change both text color and background color.
The function I found on the forum can change the background color but then it will also change the text color to black no matter what it was before.

tomoe_uehara
  • Members
  • 2166 posts
  • Last active: Jun 11 2015 05:33 PM
  • Joined: 05 Sep 2009
Do you want to change the text and background color in editbox? Maybe this could help you --> [Link]

tomoe_uehara
  • Members
  • 2166 posts
  • Last active: Jun 11 2015 05:33 PM
  • Joined: 05 Sep 2009
But actually you just need to do this, if you want a simpler GUI.
(Press F1 or F2)

Gui, Add, Edit, vMyEdit w200 Center, 1234567890
Gui, Show,, My GUI #1
return

F1::
Random, col, 111111, 999999
Gui, Color,, %col%
return

F2::
Random, col2, 111111, 999999
Gui, Font, c%col2%
GuiControl, Font, MyEdit
return

ESC::
GuiClose:
ExitApp


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

The function I found on the forum can change the background color but then it will also change the text color to black no matter what it was before.


Can you post some code?.
kWo4Lk1.png

  • Guests
  • Last active:
  • Joined: --
@tomoe_uehara,
That's amazing, I didn't know it could be so simple, so all I needed to do was
Gui, Color,[color=red],[/color]< Color >
It seems to change all edit controls background then.

@SKAN,
Actually the only other solution I found was by you here
That works fine but only to change the background, and then it will always change the text to black.

Let me try tomoe_uehara solution for a while and see
Thanks a million tomoe_uehara :)

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

@SKAN,
Actually the only other solution I found was by you here
That works fine but only to change the background, and then it will always change the text to black.


Only one additional line is required:

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
    {
        [color=#FF0040]DllCall("SetTextColor", UInt, wParam, UInt,0x0000FF )[/color]
        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


  • Guests
  • Last active:
  • Joined: --
Nice SKAN,
But I need a way to change colors for both text and background on demand later after I show the gui, I'm not sure how to achieve that using your function :/
Could you please demonstrate that?

just me
  • Members
  • 1496 posts
  • Last active: Nov 03 2015 04:32 PM
  • Joined: 28 May 2011
http://www.autohotke...=533895#p533895

Prefer ahkscript.org for the time being.


  • Guests
  • Last active:
  • Joined: --
Oh just me, you couldn't be more off topic :p

CodeKiller
  • Members
  • 2067 posts
  • Last active: Feb 26 2016 09:30 AM
  • Joined: 10 Jul 2008
I think just me wanted to show this topic :

<!-- l --><a class="postlink-local" href="http://www.autohotkey.com/community/viewtopic.php?f=13&t=77715">viewtopic.php?f=13&t=77715</a><!-- l -->