Hide the dotted focus border

Put simple Tips and Tricks that are not entire Tutorials in this forum
just me
Posts: 9424
Joined: 02 Oct 2013, 08:51
Location: Germany

Hide the dotted focus border

Post by just me » 23 Oct 2015, 01:43

Because of this question I tried tmplinshi's function. But it didn't work as I want, so I changed it. This version seems to work and - according to the MSDN - should work for all Windows versions >= Win 2000. I tested on Win 10 only.

As a result of my test you have to call the function once for each GUI control / GUI after it has been shown.

Code: Select all

; ==================================================================================================================================
; Hides the focus border for the given GUI control or GUI and all of its children.
; Call the function passing only the HWND of the control / GUI in wParam as only parameter.
; WM_UPDATEUISTATE  -> msdn.microsoft.com/en-us/library/ms646361(v=vs.85).aspx
; The Old New Thing -> blogs.msdn.com/b/oldnewthing/archive/2013/05/16/10419105.aspx
; ==================================================================================================================================
HideFocusBorder(wParam, lParam := "", uMsg := "", hWnd := "") {
   ; WM_UPDATEUISTATE = 0x0128
	Static Affected := [] ; affected controls / GUIs
        , HideFocus := 0x00010001 ; UIS_SET << 16 | UISF_HIDEFOCUS
	     , OnMsg := OnMessage(0x0128, Func("HideFocusBorder"))
	If (uMsg = 0x0128) { ; called by OnMessage()
      If (wParam = HideFocus)
         Affected[hWnd] := True
      Else If Affected[hWnd]
         PostMessage, 0x0128, %HideFocus%, 0, , ahk_id %hWnd%
   }
   Else If DllCall("IsWindow", "Ptr", wParam, "UInt")
	  PostMessage, 0x0128, %HideFocus%, 0, , ahk_id %wParam%
}
tmplinshi had already noticed some issues caused by the Alt key. On my system WM_UPDATEUISTATE messages with wParam = 0x00030002 (UIS_INITIALIZE | UISF_HIDEACCEL) are sent/posted to the controls. Although wParam does not contain the UISF_HIDEFOCUS flag, it will be cleared when the message is processed. That's why the message handler for WM_UPDATEUISTATE messages is needed.

User avatar
joedf
Posts: 8940
Joined: 29 Sep 2013, 17:08
Location: Canada
Contact:

Re: Hide the dotted focus border

Post by joedf » 23 Oct 2015, 17:14

hmm... cool :+1:
Image Image Image Image Image
Windows 10 x64 Professional, Intel i5-8500, NVIDIA GTX 1060 6GB, 2x16GB Kingston FURY Beast - DDR4 3200 MHz | [About Me] | [About the AHK Foundation] | [Courses on AutoHotkey]
[ASPDM - StdLib Distribution] | [Qonsole - Quake-like console emulator] | [LibCon - Autohotkey Console Library]

Coco-guest

Re: Hide the dotted focus border

Post by Coco-guest » 23 Oct 2015, 22:10

AutoIt solution I once found way back. It is somehow more complicated(involves subclassing, etc.) compared to your method.

User avatar
Exaskryz
Posts: 2882
Joined: 17 Oct 2015, 20:28

Re: Hide the dotted focus border

Post by Exaskryz » 09 Dec 2015, 14:19

Just wanted to say that it works on 8.1 and I appreciate you sharing this code. Worked great to hide the border on a Slider control.

User avatar
jNizM
Posts: 3183
Joined: 30 Sep 2013, 01:33
Contact:

Re: Hide the dotted focus border

Post by jNizM » 30 Jul 2021, 03:36

Rewritten v2 function after I needed it

Code: Select all

HideFocusBorder(wParam, lParam := "", Msg := "", hWnd := "")
{
	static Affected         := Map()
	static WM_UPDATEUISTATE := 0x0128
	static UIS_SET          := 1
	static UISF_HIDEFOCUS   := 0x1
	static SET_HIDEFOCUS    := UIS_SET << 16 | UISF_HIDEFOCUS
	static init             := OnMessage(WM_UPDATEUISTATE, HideFocusBorder)

	if (Msg = WM_UPDATEUISTATE) {
		if (wParam = SET_HIDEFOCUS)
			Affected[hWnd] := true
		else if (Affected.Has(hWnd))
			PostMessage WM_UPDATEUISTATE, SET_HIDEFOCUS, 0,, "ahk_id " hWnd
	}
	else if (DllCall("IsWindow", "ptr", wParam, "uint"))
		PostMessage WM_UPDATEUISTATE, SET_HIDEFOCUS, 0,, "ahk_id " wParam
}
[AHK] v2.0.5 | [WIN] 11 Pro (Version 22H2) | [GitHub] Profile

Xruss
Posts: 2
Joined: 14 Jul 2020, 06:53

Re: Hide the dotted focus border

Post by Xruss » 04 Dec 2021, 05:13

Help. I have an error: ==> Call to nonexistent function. Specifically: Map()

User avatar
jNizM
Posts: 3183
Joined: 30 Sep 2013, 01:33
Contact:

Re: Hide the dotted focus border

Post by jNizM » 06 Dec 2021, 03:56

@ Xruss: my post is for ahk-v2. if you use ahk-v1 you should use the functions from "just me".
[AHK] v2.0.5 | [WIN] 11 Pro (Version 22H2) | [GitHub] Profile

neogna2
Posts: 586
Joined: 15 Sep 2016, 15:44

Re: Hide the dotted focus border

Post by neogna2 » 07 Dec 2021, 05:27

With both v1 and v2 versions the control that has default focus when creating a GUI still has a dotted focus border, initially. When we tab away from it the dotted border disappears.

v2 example:

Code: Select all

MyGui := Gui()
MyGui.Add("Button", , "1")
MyGui.Add("Button", , "2")
MyGui.Show("y300 w150 h100")
HideFocusBorder(MyGui.Hwnd)
;add HideFocusBorder function here
dotted.png
dotted.png (3.7 KiB) Viewed 4611 times
One way around this issue is to first explicitly focus the default focused GUI element. We then get only the solid focus border.

Code: Select all

MyGui := Gui()
MyGui.Add("Button", , "1")
MyGui.Add("Button", , "2")
MyGui.Show("y300 w150 h100")
MyGui.FocusedCtrl.Focus()
HideFocusBorder(MyGui.Hwnd)
;add HideFocusBorder function here
not_dotted.png
not_dotted.png (3.87 KiB) Viewed 4611 times

jmuhammad
Posts: 6
Joined: 10 Sep 2022, 00:49

Re: Hide the dotted focus border

Post by jmuhammad » 10 Sep 2022, 01:44

Greetings all,
For benefit of anyone searching forum.
I use this to send focus to 'offscreen' button and remove any dotted/border lines from button I clicked.

Code: Select all

Gui, Add, Button, vBtnH05_Diversion x2500 y45 w100 h25 Center gH05_Diversion, DIVERSION ;set x so button is offscreen
Gui, Add, Button, vBtnM13 x100 y100 w100 h25 Left gM13, "M13: - - - - -"

H05_Diversion:
;located outside of visible gui
;just have label of any desired control call Subr_Diversion to divert focus
;ex below is a call from button having global label gM13
return

M13:
  Gosub Subr_Diversion
return

Subr_Diversion:
{
  WinActivate, ahk_exe AutoHotkey.exe
  GuiControl,Enabled, BtnM05
  guicontrol,focus,BtnH05_Diversion
  Send, {Tab}+{Tab}
  return
}

Post Reply

Return to “Tips and Tricks (v1)”