How to change the color of edit box which has input focus in GUI? Topic is solved

Get help with using AutoHotkey and its commands and hotkeys
User avatar
Sabestian Caine
Posts: 415
Joined: 12 Apr 2015, 03:53

How to change the color of edit box which has input focus in GUI?

17 May 2019, 10:08

Hello Friends,

I have three edit boxes which looks like this-
17_05_19 @8_24_22 white.png
17_05_19 @8_24_22 white.png (24.58 KiB) Viewed 681 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
17_05_19 @8_24_22.PNG (24.59 KiB) Viewed 681 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..
I don't normally code as I don't code normally.
YOU do what YOU want, but happens what HE wants.
YOU do what HE wants, will happen what YOU want.
teadrinker
Posts: 758
Joined: 29 Mar 2015, 09:41
Contact:

Re: How to change the color of edit box which has input focus in GUI?

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"]
}
User avatar
Sabestian Caine
Posts: 415
Joined: 12 Apr 2015, 03:53

Re: How to change the color of edit box which has input focus in GUI?

19 May 2019, 05:15

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... :bravo: :bravo:

So nice of you sir... :thumbup:
I don't normally code as I don't code normally.
YOU do what YOU want, but happens what HE wants.
YOU do what HE wants, will happen what YOU want.
User avatar
Sabestian Caine
Posts: 415
Joined: 12 Apr 2015, 03:53

Re: How to change the color of edit box which has input focus in GUI?

23 May 2019, 14:30

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...
I don't normally code as I don't code normally.
YOU do what YOU want, but happens what HE wants.
YOU do what HE wants, will happen what YOU want.
teadrinker
Posts: 758
Joined: 29 Mar 2015, 09:41
Contact:

Re: How to change the color of edit box which has input focus in GUI?

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"]
}
User avatar
Sabestian Caine
Posts: 415
Joined: 12 Apr 2015, 03:53

Re: How to change the color of edit box which has input focus in GUI?

24 May 2019, 02:32

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...
I don't normally code as I don't code normally.
YOU do what YOU want, but happens what HE wants.
YOU do what HE wants, will happen what YOU want.
just me
Posts: 6370
Joined: 02 Oct 2013, 08:51
Location: Germany

Re: How to change the color of edit box which has input focus in GUI?

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.
User avatar
Sabestian Caine
Posts: 415
Joined: 12 Apr 2015, 03:53

Re: How to change the color of edit box which has input focus in GUI?  Topic is solved

27 May 2019, 00:40

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..
I don't normally code as I don't code normally.
YOU do what YOU want, but happens what HE wants.
YOU do what HE wants, will happen what YOU want.
just me
Posts: 6370
Joined: 02 Oct 2013, 08:51
Location: Germany

Re: How to change the color of edit box which has input focus in GUI?

27 May 2019, 02:54

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)
Hellbent
Posts: 572
Joined: 23 Sep 2017, 13:34

Re: How to change the color of edit box which has input focus in GUI?

27 May 2019, 03:34

just me wrote:
27 May 2019, 02:54
RDW_INVALIDATE (0x0001)
Where are you get the value from?
i.e 0x0001
SnapShot_493.png
SnapShot_493.png (7.81 KiB) Viewed 456 times
just me
Posts: 6370
Joined: 02 Oct 2013, 08:51
Location: Germany

Re: How to change the color of edit box which has input focus in GUI?

27 May 2019, 03:55

@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.
Hellbent
Posts: 572
Joined: 23 Sep 2017, 13:34

Re: How to change the color of edit box which has input focus in GUI?

27 May 2019, 04:10

@just me
Thank you.

If you don't mind, what is going on with this syntax.

Code: Select all

Controls[L, "Invalidated"]
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.
just me
Posts: 6370
Joined: 02 Oct 2013, 08:51
Location: Germany

Re: How to change the color of edit box which has input focus in GUI?

27 May 2019, 05:01

@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 ,...
Hellbent
Posts: 572
Joined: 23 Sep 2017, 13:34

Re: How to change the color of edit box which has input focus in GUI?

29 May 2019, 02:49

@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.
just me
Posts: 6370
Joined: 02 Oct 2013, 08:51
Location: Germany

Re: How to change the color of edit box which has input focus in GUI?

29 May 2019, 08:54

I donn't know how this could be done. Microsoft's implementation of the hotkey control is rather poor.
Hellbent
Posts: 572
Joined: 23 Sep 2017, 13:34

Re: How to change the color of edit box which has input focus in GUI?

29 May 2019, 16:44

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.

Return to “Ask For Help”

Who is online

Users browsing this forum: bennybroseph, Flipeador, kronos90, Sid4G, tmplinshi and 211 guests