Page 1 of 1
How to change the color of edit box which has input focus in GUI?
Posted: 17 May 2019, 10:08
by Sabestian Caine
Hello Friends,
I have three edit boxes which looks like this-
- 17_05_19 @8_24_22 white.png (24.58 KiB) Viewed 4604 times
the codes are these-
Code: Select all
Gui Add, Edit, x183 y57 w205 h51, Edit 1
Gui Add, Edit, x184 y128 w205 h51, Edit 2
Gui Add, Edit, x185 y199 w205 h51, Edit 3
Gui Show, w547 h338, Window
Return
Now I want that the color of edit box should change from white to yellow which has input focus. Like this-
- 17_05_19 @8_24_22.PNG (24.59 KiB) Viewed 4604 times
In the above image you can see that edit 1 has input focus, so its color changes to yellow. Similarly, if I put input focus into edit 2 then its color should turn to yellow and so on.
Is it possible? I think there must be some class or function for edit box's customization. Please guide and help.. Thanks a lot..
Re: How to change the color of edit box which has input focus in GUI?
Posted: 17 May 2019, 13:54
by teadrinker
Hi
Try this:
Code: Select all
backColor := 0xFFFF00
Gui Add, Edit, x183 y57 w205 h51 +hwndhEdit1, Edit 1
SetEditColor(hEdit1, backColor)
Gui Add, Edit, x184 y128 w205 h51, Edit 2
Gui Add, Edit, x185 y199 w205 h51, Edit 3
OnMessage( 0x111, Func("WM_COMMAND").Bind(backColor) )
Gui Show, w547 h338, Window
Return
WM_COMMAND(clr, wp, lp) {
static EN_SETFOCUS := 0x100, EN_KILLFOCUS := 0x200
if (wp >> 16 = EN_SETFOCUS)
SetEditColor(lp, clr)
if (wp >> 16 = EN_KILLFOCUS)
SetEditColor(lp)
}
SetEditColor(hEdit, b_color_rgb := "", f_color_rgb := 0) {
static arr := [], _ := OnMessage( 0x133, Func("WM_CTLCOLOREDIT").Bind(arr) )
if arr.HasKey(hEdit)
DllCall("DeleteObject", Ptr, arr[hEdit, "hBrush"])
if (b_color_rgb = "")
arr.Delete(hEdit)
else {
for k, v in ["b", "f"]
%v%_color_bgr := DllCall("Ws2_32\ntohl", UInt, %v%_color_rgb << 8, UInt)
hBrush := DllCall("CreateSolidBrush", UInt, b_color_bgr, Ptr)
arr[hEdit] := {b_color: b_color_bgr, f_color: f_color_bgr, hBrush: hBrush}
}
WinSet, Redraw,, ahk_id %hEdit%
}
WM_CTLCOLOREDIT(arr, hDC, hEdit) {
if !arr.HasKey(hEdit)
Return
DllCall("SetBkColor" , Ptr, hDC, UInt, arr[hEdit, "b_color"])
DllCall("SetTextColor", Ptr, hDC, UInt, arr[hEdit, "f_color"])
Return arr[hEdit, "hBrush"]
}
Re: How to change the color of edit box which has input focus in GUI?
Posted: 19 May 2019, 05:15
by Sabestian Caine
teadrinker wrote: ↑17 May 2019, 13:54
Hi
Try this:
Code: Select all
backColor := 0xFFFF00
Gui Add, Edit, x183 y57 w205 h51 +hwndhEdit1, Edit 1
SetEditColor(hEdit1, backColor)
Gui Add, Edit, x184 y128 w205 h51, Edit 2
Gui Add, Edit, x185 y199 w205 h51, Edit 3
OnMessage( 0x111, Func("WM_COMMAND").Bind(backColor) )
Gui Show, w547 h338, Window
Return
WM_COMMAND(clr, wp, lp) {
static EN_SETFOCUS := 0x100, EN_KILLFOCUS := 0x200
if (wp >> 16 = EN_SETFOCUS)
SetEditColor(lp, clr)
if (wp >> 16 = EN_KILLFOCUS)
SetEditColor(lp)
}
SetEditColor(hEdit, b_color_rgb := "", f_color_rgb := 0) {
static arr := [], _ := OnMessage( 0x133, Func("WM_CTLCOLOREDIT").Bind(arr) )
if arr.HasKey(hEdit)
DllCall("DeleteObject", Ptr, arr[hEdit, "hBrush"])
if (b_color_rgb = "")
arr.Delete(hEdit)
else {
for k, v in ["b", "f"]
%v%_color_bgr := DllCall("Ws2_32\ntohl", UInt, %v%_color_rgb << 8, UInt)
hBrush := DllCall("CreateSolidBrush", UInt, b_color_bgr, Ptr)
arr[hEdit] := {b_color: b_color_bgr, f_color: f_color_bgr, hBrush: hBrush}
}
WinSet, Redraw,, ahk_id %hEdit%
}
WM_CTLCOLOREDIT(arr, hDC, hEdit) {
if !arr.HasKey(hEdit)
Return
DllCall("SetBkColor" , Ptr, hDC, UInt, arr[hEdit, "b_color"])
DllCall("SetTextColor", Ptr, hDC, UInt, arr[hEdit, "f_color"])
Return arr[hEdit, "hBrush"]
}
Thanks a lot dear teadrinker... Your codes are working perfectly fine...
So nice of you sir...
Re: How to change the color of edit box which has input focus in GUI?
Posted: 23 May 2019, 14:30
by Sabestian Caine
teadrinker wrote: ↑17 May 2019, 13:54
Hi
Try this:
Code: Select all
backColor := 0xFFFF00
Gui Add, Edit, x183 y57 w205 h51 +hwndhEdit1, Edit 1
SetEditColor(hEdit1, backColor)
Gui Add, Edit, x184 y128 w205 h51, Edit 2
Gui Add, Edit, x185 y199 w205 h51, Edit 3
OnMessage( 0x111, Func("WM_COMMAND").Bind(backColor) )
Gui Show, w547 h338, Window
Return
WM_COMMAND(clr, wp, lp) {
static EN_SETFOCUS := 0x100, EN_KILLFOCUS := 0x200
if (wp >> 16 = EN_SETFOCUS)
SetEditColor(lp, clr)
if (wp >> 16 = EN_KILLFOCUS)
SetEditColor(lp)
}
SetEditColor(hEdit, b_color_rgb := "", f_color_rgb := 0) {
static arr := [], _ := OnMessage( 0x133, Func("WM_CTLCOLOREDIT").Bind(arr) )
if arr.HasKey(hEdit)
DllCall("DeleteObject", Ptr, arr[hEdit, "hBrush"])
if (b_color_rgb = "")
arr.Delete(hEdit)
else {
for k, v in ["b", "f"]
%v%_color_bgr := DllCall("Ws2_32\ntohl", UInt, %v%_color_rgb << 8, UInt)
hBrush := DllCall("CreateSolidBrush", UInt, b_color_bgr, Ptr)
arr[hEdit] := {b_color: b_color_bgr, f_color: f_color_bgr, hBrush: hBrush}
}
WinSet, Redraw,, ahk_id %hEdit%
}
WM_CTLCOLOREDIT(arr, hDC, hEdit) {
if !arr.HasKey(hEdit)
Return
DllCall("SetBkColor" , Ptr, hDC, UInt, arr[hEdit, "b_color"])
DllCall("SetTextColor", Ptr, hDC, UInt, arr[hEdit, "f_color"])
Return arr[hEdit, "hBrush"]
}
Hello dear teadriker....
Sir, sometimes your codes do not work. Namely when i run the script sometimes it shows all three edit box to be white, while it should show first one to be yellow. However, this does not happen every time. But it happens roughly once in five time. I request you to fix this problem. Thanks a lot dear teadrinker...
Re: How to change the color of edit box which has input focus in GUI?
Posted: 23 May 2019, 14:44
by teadrinker
Try this code:
Code: Select all
backColor := 0xFFFF00
Gui Add, Edit, x183 y57 w205 h51, Edit 1
Gui Add, Edit, x184 y128 w205 h51, Edit 2
Gui Add, Edit, x185 y199 w205 h51, Edit 3
OnMessage( 0x111, Func("WM_COMMAND").Bind(backColor) )
Gui Show, w547 h338, Window
Return
WM_COMMAND(clr, wp, lp) {
static EN_SETFOCUS := 0x100, EN_KILLFOCUS := 0x200
if (wp >> 16 = EN_SETFOCUS)
SetEditColor(lp, clr)
if (wp >> 16 = EN_KILLFOCUS)
SetEditColor(lp)
}
SetEditColor(hEdit, b_color_rgb := "", f_color_rgb := 0) {
static arr := [], _ := OnMessage( 0x133, Func("WM_CTLCOLOREDIT").Bind(arr) )
, flags := (RDW_FRAME := 0x400)|(RDW_INVALIDATE := 0x1)|(RDW_ERASE := 0x4)
if arr.HasKey(hEdit)
DllCall("DeleteObject", Ptr, arr[hEdit, "hBrush"])
if (b_color_rgb = "")
arr.Delete(hEdit)
else {
for k, v in ["b", "f"]
%v%_color_bgr := DllCall("Ws2_32\ntohl", UInt, %v%_color_rgb << 8, UInt)
hBrush := DllCall("CreateSolidBrush", UInt, b_color_bgr, Ptr)
arr[hEdit] := {b_color: b_color_bgr, f_color: f_color_bgr, hBrush: hBrush}
}
DllCall("RedrawWindow", Ptr, hEdit, Ptr, 0, Ptr, 0, UInt, flags)
}
WM_CTLCOLOREDIT(arr, hDC, hEdit) {
if !arr.HasKey(hEdit)
Return
DllCall("SetBkColor" , Ptr, hDC, UInt, arr[hEdit, "b_color"])
DllCall("SetTextColor", Ptr, hDC, UInt, arr[hEdit, "f_color"])
Return arr[hEdit, "hBrush"]
}
Re: How to change the color of edit box which has input focus in GUI?
Posted: 24 May 2019, 02:32
by Sabestian Caine
teadrinker wrote: ↑23 May 2019, 14:44
Try this code:
Code: Select all
backColor := 0xFFFF00
Gui Add, Edit, x183 y57 w205 h51, Edit 1
Gui Add, Edit, x184 y128 w205 h51, Edit 2
Gui Add, Edit, x185 y199 w205 h51, Edit 3
OnMessage( 0x111, Func("WM_COMMAND").Bind(backColor) )
Gui Show, w547 h338, Window
Return
WM_COMMAND(clr, wp, lp) {
static EN_SETFOCUS := 0x100, EN_KILLFOCUS := 0x200
if (wp >> 16 = EN_SETFOCUS)
SetEditColor(lp, clr)
if (wp >> 16 = EN_KILLFOCUS)
SetEditColor(lp)
}
SetEditColor(hEdit, b_color_rgb := "", f_color_rgb := 0) {
static arr := [], _ := OnMessage( 0x133, Func("WM_CTLCOLOREDIT").Bind(arr) )
, flags := (RDW_FRAME := 0x400)|(RDW_INVALIDATE := 0x1)|(RDW_ERASE := 0x4)
if arr.HasKey(hEdit)
DllCall("DeleteObject", Ptr, arr[hEdit, "hBrush"])
if (b_color_rgb = "")
arr.Delete(hEdit)
else {
for k, v in ["b", "f"]
%v%_color_bgr := DllCall("Ws2_32\ntohl", UInt, %v%_color_rgb << 8, UInt)
hBrush := DllCall("CreateSolidBrush", UInt, b_color_bgr, Ptr)
arr[hEdit] := {b_color: b_color_bgr, f_color: f_color_bgr, hBrush: hBrush}
}
DllCall("RedrawWindow", Ptr, hEdit, Ptr, 0, Ptr, 0, UInt, flags)
}
WM_CTLCOLOREDIT(arr, hDC, hEdit) {
if !arr.HasKey(hEdit)
Return
DllCall("SetBkColor" , Ptr, hDC, UInt, arr[hEdit, "b_color"])
DllCall("SetTextColor", Ptr, hDC, UInt, arr[hEdit, "f_color"])
Return arr[hEdit, "hBrush"]
}
Thank you so much dear teadrinker for your reply..
But, sir these codes are working worse than earlier codes.... Every time when i run the script it shows all edit boxes to be white, while one edit box which has input focus should be yellow. However, when i click or press tab to shift focus to other edit boxes then it tuns to yellow. Please fix this problem sir.... Thank you so much dear teadrinker...
Re: How to change the color of edit box which has input focus in GUI?
Posted: 24 May 2019, 06:15
by just me
Because I've been passionate about control colors, does this work more stable for you?
Code: Select all
#NoEnv
BackColor := 0xFFFF00
EditControls := {}
Gui Add, Edit, x183 y57 w205 h51 hwndHED, Edit 1
SetFocusedBkColor(HED, BackColor)
Gui Add, Edit, x184 y128 w205 h51 hwndHED, Edit 2
SetFocusedBkColor(HED, BackColor)
Gui Add, Edit, x185 y199 w205 h51 hwndHED, Edit 3
SetFocusedBkColor(HED, BackColor)
OnMessage(0x0133, "SetFocusedBkColor") ; WM_CTLCOLOREDIT
Gui Show, w547 h338, Window
Return
GuiClose:
ExitApp
; ----------------------------------------------------------------------------------------------------------------------
SetFocusedBkColor(W, L := "", M := "", H := "") {
Static Controls := {}
If (M && H) { ; system call: W = HDC, L = HWND
If Controls.HasKey(L) {
If (L = DllCall("GetFocus", "UPtr")) {
Controls[L, "Invalidated"] := False
DllCall("SetBkColor", "Ptr", W, "UInt", Controls[L, "Color"])
Return Controls[L, "Brush"]
}
Else If !Controls[L, "Invalidated"] {
Controls[L, "Invalidated"] := True
DllCall("InvalidateRect", "Ptr", L, "Ptr", 0, "UInt", 1)
}
}
}
Else { ; setup call: W = HWND, L = color
If DllCall("IsWindow", "Ptr", W) {
If Controls.HasKey(W) {
DllCall("DeleteObject", "Ptr", Controls[W, "Brush"])
Controls.Delete(W)
}
If (L <> "") && ((EditColor := SwapRB(L)) <> "") {
Controls[W, "Color"] := EditColor
Controls[W, "Brush"] := DllCall("CreateSolidBrush", "UInt", EditColor, "Uptr")
Controls[W, "Invalidated"] := True
}
DllCall("RedrawWindow", "Ptr", W, "Ptr", 0, "Ptr", 0, "UInt", 0x0405, "UInt")
}
}
}
; ----------------------------------------------------------------------------------------------------------------------
SwapRB(Color) { ; converts RGB to BGR and vice versa
Return ((Color & 0xFF0000) >> 16) | (Color & 0x00FF00) | ((Color & 0x0000FF) << 16)
}
Edit: Slight code changes.
Re: How to change the color of edit box which has input focus in GUI? Topic is solved
Posted: 27 May 2019, 00:40
by Sabestian Caine
just me wrote: ↑24 May 2019, 06:15
Because I've been passionate about control colors, does this work more stable for you?
Code: Select all
#NoEnv
BackColor := 0xFFFF00
EditControls := {}
Gui Add, Edit, x183 y57 w205 h51 hwndHED, Edit 1
SetFocusedBkColor(HED, BackColor)
Gui Add, Edit, x184 y128 w205 h51 hwndHED, Edit 2
SetFocusedBkColor(HED, BackColor)
Gui Add, Edit, x185 y199 w205 h51 hwndHED, Edit 3
SetFocusedBkColor(HED, BackColor)
OnMessage(0x0133, "SetFocusedBkColor") ; WM_CTLCOLOREDIT
Gui Show, w547 h338, Window
Return
GuiClose:
ExitApp
; ----------------------------------------------------------------------------------------------------------------------
SetFocusedBkColor(W, L := "", M := "", H := "") {
Static Controls := {}
If (M && H) { ; system call: W = HDC, L = HWND
If Controls.HasKey(L) {
If (L = DllCall("GetFocus", "UPtr")) {
Controls[L, "Invalidated"] := False
DllCall("SetBkColor", "Ptr", W, "UInt", Controls[L, "Color"])
Return Controls[L, "Brush"]
}
Else If !Controls[L, "Invalidated"] {
Controls[L, "Invalidated"] := True
DllCall("InvalidateRect", "Ptr", L, "Ptr", 0, "UInt", 1)
}
}
}
Else { ; setup call: W = HWND, L = color
If DllCall("IsWindow", "Ptr", W) {
If Controls.HasKey(W) {
DllCall("DeleteObject", "Ptr", Controls[W, "Brush"])
Controls.Delete(W)
}
If (L <> "") && ((EditColor := SwapRB(L)) <> "") {
Controls[W, "Color"] := EditColor
Controls[W, "Brush"] := DllCall("CreateSolidBrush", "UInt", EditColor, "Uptr")
Controls[W, "Invalidated"] := True
}
DllCall("RedrawWindow", "Ptr", W, "Ptr", 0, "Ptr", 0, "UInt", 0x0405, "UInt")
}
}
}
; ----------------------------------------------------------------------------------------------------------------------
SwapRB(Color) { ; converts RGB to BGR and vice versa
Return ((Color & 0xFF0000) >> 16) | (Color & 0x00FF00) | ((Color & 0x0000FF) << 16)
}
Edit: Slight code changes.
Thanks dear just me.... now the codes seem working perfectly... Thanks a lot just me...
Just me, I make one more request to you that please make me understand this line of code-
Code: Select all
DllCall("RedrawWindow", "Ptr", W, "Ptr", 0, "Ptr", 0, "UInt", 0x0405, "UInt")
Thanks a lot just me..
Re: How to change the color of edit box which has input focus in GUI?
Posted: 27 May 2019, 02:54
by just me
Sabestian Caine wrote:
Just me, I make one more request to you that please make me understand this line of code-
Code: Select all
DllCall("RedrawWindow", "Ptr", W, "Ptr", 0, "Ptr", 0, "UInt", 0x0405, "UInt")
Thanks a lot just me..
RedrawWindow()The RedrawWindow function updates the specified rectangle or region in a window's client area.
I use this function to redraw the control after 'setup' changes.
Parameters:
Code: Select all
hWnd HWND of the control.
*lprcUpdate a pointer to the update rectangle, here 0 = the entire client area is added to the update region
hrgnUpdate a handle to the update region, here 0 = the entire client area is added to the update region
flags redraw flags, here:
RDW_INVALIDATE (0x0001)
RDW_ERASE (0x0004)
RDW_FRAME (0x0400)
Re: How to change the color of edit box which has input focus in GUI?
Posted: 27 May 2019, 03:34
by Hellbent
just me wrote: ↑27 May 2019, 02:54
RDW_INVALIDATE (0x0001)
Where are you get the value from?
i.e 0x0001
- SnapShot_493.png (7.81 KiB) Viewed 4379 times
Re: How to change the color of edit box which has input focus in GUI?
Posted: 27 May 2019, 03:55
by just me
@Hellbent I got it directly from
winuser.h:
Code: Select all
/*
* RedrawWindow() flags
*/
#define RDW_INVALIDATE 0x0001
#define RDW_INTERNALPAINT 0x0002
#define RDW_ERASE 0x0004
#define RDW_VALIDATE 0x0008
#define RDW_NOINTERNALPAINT 0x0010
#define RDW_NOERASE 0x0020
#define RDW_NOCHILDREN 0x0040
#define RDW_ALLCHILDREN 0x0080
#define RDW_UPDATENOW 0x0100
#define RDW_ERASENOW 0x0200
#define RDW_FRAME 0x0400
#define RDW_NOFRAME 0x0800
You get the Windows header files when you download a free Windows SDK from
MicroSoft.
Re: How to change the color of edit box which has input focus in GUI?
Posted: 27 May 2019, 04:10
by Hellbent
@just me
Thank you.
If you don't mind, what is going on with this syntax.
It looks like a multidimensional array, but I can't be sure because some of the object/array stuff still feels a bit odd to me.
Re: How to change the color of edit box which has input focus in GUI?
Posted: 27 May 2019, 05:01
by just me
@Hellbent:
Code: Select all
SetFocusedBkColor(W, L := "", M := "", H := "") {
Static Controls := {}
...
The
Controls object holds the information needed to color the controls. Each control is stored by
HWND (which is passed in the
L parameter for system calls) and contains an array of three properties:
Color,
Brush,
Invalidated.
Invalidated is used to prevent
Code: Select all
DllCall("InvalidateRect", "Ptr", L, "Ptr", 0, "UInt", 1)
to be called more than once for a control after the focus changed, because
InvalidateRect creates an
WM_CTLCOLOREDIT message for this control, and so on, and so on ,...
Re: How to change the color of edit box which has input focus in GUI?
Posted: 29 May 2019, 02:49
by Hellbent
@just me
Thanks.
You wouldn't happen to know how to go about changing the background and font color of a Hotkey Gui control would you?
I looked around and tried a few functions used to change control colors but none that I tried had any effect of Hotkey controls.
Re: How to change the color of edit box which has input focus in GUI?
Posted: 29 May 2019, 08:54
by just me
I donn't know how this could be done. Microsoft's implementation of the hotkey control is rather poor.
Re: How to change the color of edit box which has input focus in GUI?
Posted: 29 May 2019, 16:44
by Hellbent
just me wrote: ↑29 May 2019, 08:54
I donn't know how this could be done. Microsoft's implementation of the hotkey control is rather poor.
Thank you for your time.
Re: How to change the color of edit box which has input focus in GUI?
Posted: 08 Aug 2022, 10:24
by balkinishu
This code works great. Does anyone know how to modify it so it works with
GuiControl, 2: Focus,
The focus highlighting gets broken when I use that line to reactivate my input box. Thanks in advance.
Re: How to change the color of edit box which has input focus in GUI?
Posted: 09 Aug 2022, 01:31
by jsong55
Hi Just Me, I like your ctrlcolors class, would you extend this capability to the class?
just me wrote: ↑24 May 2019, 06:15
Because I've been passionate about control colors, does this work more stable for you?
Code: Select all
#NoEnv
BackColor := 0xFFFF00
EditControls := {}
Gui Add, Edit, x183 y57 w205 h51 hwndHED, Edit 1
SetFocusedBkColor(HED, BackColor)
Gui Add, Edit, x184 y128 w205 h51 hwndHED, Edit 2
SetFocusedBkColor(HED, BackColor)
Gui Add, Edit, x185 y199 w205 h51 hwndHED, Edit 3
SetFocusedBkColor(HED, BackColor)
OnMessage(0x0133, "SetFocusedBkColor") ; WM_CTLCOLOREDIT
Gui Show, w547 h338, Window
Return
GuiClose:
ExitApp
; ----------------------------------------------------------------------------------------------------------------------
SetFocusedBkColor(W, L := "", M := "", H := "") {
Static Controls := {}
If (M && H) { ; system call: W = HDC, L = HWND
If Controls.HasKey(L) {
If (L = DllCall("GetFocus", "UPtr")) {
Controls[L, "Invalidated"] := False
DllCall("SetBkColor", "Ptr", W, "UInt", Controls[L, "Color"])
Return Controls[L, "Brush"]
}
Else If !Controls[L, "Invalidated"] {
Controls[L, "Invalidated"] := True
DllCall("InvalidateRect", "Ptr", L, "Ptr", 0, "UInt", 1)
}
}
}
Else { ; setup call: W = HWND, L = color
If DllCall("IsWindow", "Ptr", W) {
If Controls.HasKey(W) {
DllCall("DeleteObject", "Ptr", Controls[W, "Brush"])
Controls.Delete(W)
}
If (L <> "") && ((EditColor := SwapRB(L)) <> "") {
Controls[W, "Color"] := EditColor
Controls[W, "Brush"] := DllCall("CreateSolidBrush", "UInt", EditColor, "Uptr")
Controls[W, "Invalidated"] := True
}
DllCall("RedrawWindow", "Ptr", W, "Ptr", 0, "Ptr", 0, "UInt", 0x0405, "UInt")
}
}
}
; ----------------------------------------------------------------------------------------------------------------------
SwapRB(Color) { ; converts RGB to BGR and vice versa
Return ((Color & 0xFF0000) >> 16) | (Color & 0x00FF00) | ((Color & 0x0000FF) << 16)
}
Edit: Slight code changes.
Re: How to change the color of edit box which has input focus in GUI?
Posted: 09 Aug 2022, 11:44
by balkinishu
Just me's code below works great. Does anyone know how to modify it so it works with
The focus highlighting gets broken when I use that line to reactivate my input box with a hotkey. Thanks in advance.
just me wrote: ↑24 May 2019, 06:15
Because I've been passionate about control colors, does this work more stable for you?
Code: Select all
#NoEnv
BackColor := 0xFFFF00
EditControls := {}
Gui Add, Edit, x183 y57 w205 h51 hwndHED, Edit 1
SetFocusedBkColor(HED, BackColor)
Gui Add, Edit, x184 y128 w205 h51 hwndHED, Edit 2
SetFocusedBkColor(HED, BackColor)
Gui Add, Edit, x185 y199 w205 h51 hwndHED, Edit 3
SetFocusedBkColor(HED, BackColor)
OnMessage(0x0133, "SetFocusedBkColor") ; WM_CTLCOLOREDIT
Gui Show, w547 h338, Window
Return
GuiClose:
ExitApp
; ----------------------------------------------------------------------------------------------------------------------
SetFocusedBkColor(W, L := "", M := "", H := "") {
Static Controls := {}
If (M && H) { ; system call: W = HDC, L = HWND
If Controls.HasKey(L) {
If (L = DllCall("GetFocus", "UPtr")) {
Controls[L, "Invalidated"] := False
DllCall("SetBkColor", "Ptr", W, "UInt", Controls[L, "Color"])
Return Controls[L, "Brush"]
}
Else If !Controls[L, "Invalidated"] {
Controls[L, "Invalidated"] := True
DllCall("InvalidateRect", "Ptr", L, "Ptr", 0, "UInt", 1)
}
}
}
Else { ; setup call: W = HWND, L = color
If DllCall("IsWindow", "Ptr", W) {
If Controls.HasKey(W) {
DllCall("DeleteObject", "Ptr", Controls[W, "Brush"])
Controls.Delete(W)
}
If (L <> "") && ((EditColor := SwapRB(L)) <> "") {
Controls[W, "Color"] := EditColor
Controls[W, "Brush"] := DllCall("CreateSolidBrush", "UInt", EditColor, "Uptr")
Controls[W, "Invalidated"] := True
}
DllCall("RedrawWindow", "Ptr", W, "Ptr", 0, "Ptr", 0, "UInt", 0x0405, "UInt")
}
}
}
; ----------------------------------------------------------------------------------------------------------------------
SwapRB(Color) { ; converts RGB to BGR and vice versa
Return ((Color & 0xFF0000) >> 16) | (Color & 0x00FF00) | ((Color & 0x0000FF) << 16)
}
Edit: Slight code changes.
Re: How to change the color of edit box which has input focus in GUI?
Posted: 21 Nov 2022, 08:06
by junjunjon
the code is amazing, i've been looking for this kind of script.
is it possible to expand this scripts to other active window, maybe an notepad Edit control, to change the background color of the edit field?