Create a indicator showing which window is activated

Get help with using AutoHotkey and its commands and hotkeys
xkooll
Posts: 14
Joined: 02 Sep 2018, 21:47

Create a indicator showing which window is activated

Post by xkooll » 24 Jan 2021, 02:13

Hello everybody

I like to work with multiple windows open side by side, sometimes is hard to tell which windows I'm currently on since the window animation to indicate it is too subtle.

I want to create a script that helps me to visualize which window I'm currently on, it can be a simple Tooltip on the top of the window bar.


I tried to look at the documentation somewhere related to WinActivated but I wasn't able to find a solution for it

In my mind the logic would be some kind of loop that checks which window is activate and assign a Tooltip to it

Once a new window is focused the Tooltip would be removed from the old one and attached to the new one


please help =)
Rohwedder
Posts: 3974
Joined: 04 Jun 2014, 08:33
Location: Germany

Re: Create a indicator showing which window is activated

Post by Rohwedder » 24 Jan 2021, 02:48

Hallo,
try:

Code: Select all

#Persistent
SetTimer, active, 300
Return
active:
ToolTip, active,20,20,9
Return
User avatar
Spawnova
Posts: 456
Joined: 08 Jul 2015, 00:12
Contact:

Re: Create a indicator showing which window is activated

Post by Spawnova » 24 Jan 2021, 03:21

Here's another solution using an overlay, this may be a bit overkill but I enjoy messing around with gdip =P

Code: Select all

gdip_startup()

Gui -Caption +E0x80000 +AlwaysOnTop +ToolWindow +hwndoverlayhwnd
Gui,Show, NA,Window Outlines

outlineSize := 8
outlineSizeHalf := outlineSize/2
outlinePen := gdip_createpen(0xFFFF0000,outlineSize)


hdc := CreateCompatibleDC(GetDc(overlayhwnd))
hbm := CreateDibSection(a_screenWidth,-a_screenheight,hdc)
SelectObject(hdc,hbm)
g := Gdip_graphicsfromhdc(hdc)

lastWindow := 0
borderDisplayTime := 1000   ;change 1000 to 0 if you want the border to stay forever

settimer,windowCheck,10
return

windowCheck:
hwnd := WinExist("a")
if (hwnd != lastWindow) {
	sleep 10
	wingetpos,x,y,w,h,% "ahk_id " hwnd
	rect := GetClientRect(hwnd,w,h)
	gdip_graphicsclear(g)
	gdip_drawrectangle(g,outlinePen,rect[1]+outlineSizeHalf,outlineSizeHalf,w-1-(outlineSizeHalf+rect[1]*2),h-1-outlineSizeHalf-rect[1])
	UpdateLayeredWindow(overlayhwnd, hdc, x, y, w, h)
	lastWindow := hwnd
	if (borderDisplayTime > 0)
		settimer,clearWindow,% -borderDisplayTime
}
return

clearWindow:
gdip_graphicsclear(g)
UpdateLayeredWindow(overlayhwnd, hdc, 0, 0, 1, 1)
return











GetClientRect(hwnd,w,h) {
	VarSetCapacity(rc, 16)
    DllCall("GetClientRect", "uint", hwnd, "uint", &rc)
    clientWidth := NumGet(rc, 8, "int")
    clientHeight := NumGet(rc, 12, "int")
	
	
	borderSize := floor(((w-clientWidth) / 2))-1
	topBorderSize := floor(((h-clientHeight)) - borderSize)
	return [borderSize,topBorderSize]
}









/*
***********************
Gdip functions copied from tic's gdip library found here -https://github.com/tariqporter/Gdip/blob/master/Gdip.ahk
***********************
*/
ReleaseDC(hdc, hwnd=0) {
	Ptr := A_PtrSize ? "UPtr" : "UInt"
	return DllCall("ReleaseDC", Ptr, hwnd, Ptr, hdc)
}
SelectObject(hdc, hgdiobj) {
	Ptr := A_PtrSize ? "UPtr" : "UInt"
	return DllCall("SelectObject", Ptr, hdc, Ptr, hgdiobj)
}
CreateDIBSection(w, h, hdc="", bpp=32, ByRef ppvBits=0) {
	Ptr := A_PtrSize ? "UPtr" : "UInt"
	hdc2 := hdc ? hdc : GetDC()
	VarSetCapacity(bi, 40, 0)
	NumPut(w, bi, 4, "uint")
	NumPut(h, bi, 8, "uint")
	NumPut(40, bi, 0, "uint")
	NumPut(1, bi, 12, "ushort")
	NumPut(0, bi, 16, "uInt")
	NumPut(bpp, bi, 14, "ushort")
	hbm := DllCall("CreateDIBSection", Ptr, hdc2, Ptr, &bi, "uint", 0, A_PtrSize ? "UPtr*" : "uint*", ppvBits, Ptr, 0, "uint", 0, Ptr)
	if !hdc
		ReleaseDC(hdc2)
	return hbm
}
CreateCompatibleDC(hdc=0) {
   return DllCall("CreateCompatibleDC", A_PtrSize ? "UPtr" : "UInt", hdc)
}
GetDC(hwnd=0) {
	return DllCall("GetDC", A_PtrSize ? "UPtr" : "UInt", hwnd)
}
Gdip_DrawRectangle(pGraphics, pPen, x, y, w, h) {
	Ptr := A_PtrSize ? "UPtr" : "UInt"
	
	return DllCall("gdiplus\GdipDrawRectangle", Ptr, pGraphics, Ptr, pPen, "float", x, "float", y, "float", w, "float", h)
}
Gdip_CreatePen(ARGB, w) {
   DllCall("gdiplus\GdipCreatePen1", "UInt", ARGB, "float", w, "int", 2, A_PtrSize ? "UPtr*" : "UInt*", pPen)
   return pPen
}
Gdip_GraphicsClear(pGraphics, ARGB=0x00ffffff) {
    return DllCall("gdiplus\GdipGraphicsClear", A_PtrSize ? "UPtr" : "UInt", pGraphics, "int", ARGB)
}
Gdip_Startup() {
	Ptr := A_PtrSize ? "UPtr" : "UInt"
	
	if !DllCall("GetModuleHandle", "str", "gdiplus", Ptr)
		DllCall("LoadLibrary", "str", "gdiplus")
	VarSetCapacity(si, A_PtrSize = 8 ? 24 : 16, 0), si := Chr(1)
	DllCall("gdiplus\GdiplusStartup", A_PtrSize ? "UPtr*" : "uint*", pToken, Ptr, &si, Ptr, 0)
	return pToken
}
Gdip_GraphicsFromHDC(hdc) {
    DllCall("gdiplus\GdipCreateFromHDC", A_PtrSize ? "UPtr" : "UInt", hdc, A_PtrSize ? "UPtr*" : "UInt*", pGraphics)
    return pGraphics
}
UpdateLayeredWindow(hwnd, hdc, x="", y="", w="", h="", Alpha=255) {
	Ptr := A_PtrSize ? "UPtr" : "UInt"
	if ((x != "") && (y != ""))
		VarSetCapacity(pt, 8), NumPut(x, pt, 0, "UInt"), NumPut(y, pt, 4, "UInt")
	if (w = "") ||(h = "")
		WinGetPos,,, w, h, ahk_id %hwnd%
	return DllCall("UpdateLayeredWindow", Ptr, hwnd, Ptr, 0, Ptr, ((x = "") && (y = "")) ? 0 : &pt, "int64*", w|h<<32, Ptr, hdc, "int64*", 0, "uint", 0, "UInt*", Alpha<<16|1<<24, "uint", 2)
}
Some of my AHK programs: 3D Voxel Game - Platformer Game - Langton's Ant - Raycast light/Pixel Water - Creating HD map of any game
Or check out my Youtube for these plus many more projects! :D
xkooll
Posts: 14
Joined: 02 Sep 2018, 21:47

Re: Create a indicator showing which window is activated

Post by xkooll » 24 Jan 2021, 11:26

Hey , thanks for solutions, both works well

I was trying to twerk the first one, instead of showing a tooltip I'm want to blink the window I'm focused on and then do nothing, unless I switch the window, then it must blink again.

Here my code:

Code: Select all


blinkWindow() {
  WinGetPos, X, Y, W, H, A
  Gui Color, 0xbaafaf
  Gui, +AlwaysOnTop +E0x20 +LastFound +Toolwindow -Caption
  
  Gui, Show, NoActivate h%H%  w%W%  x%X% y%Y%, GUIID
  WinSet, Transparent, 50

  Sleep, 200
  Gui, Destroy
}

WinGetActiveTitle, TitleWindow

#Persistent
  SetTimer, active, 300
Return

active:
  WinGetActiveTitle, TitleNew
  If (TitleWindow != TitleNew) {
    blinkWindow()
  }
Return 
The problem is that the active window keeps blinking nonstop, it seems to have something wrong with the If statement

Code: Select all

If (Titlewindow != TitleNew)
What I'm trying to say with the statement is: Only if the value of these variables are different, then blink the window, otherwise do nothing

Please help, my active window is a disco party =)
Rohwedder
Posts: 3974
Joined: 04 Jun 2014, 08:33
Location: Germany

Re: Create a indicator showing which window is activated

Post by Rohwedder » 24 Jan 2021, 12:57

Hallo,
try:

Code: Select all

blinkWindow()
{
	WinGetPos, X, Y, W, H, A
	Gui, +AlwaysOnTop +E0x20 +LastFound +Toolwindow -Caption -DPIScale
	Gui, Color, 0xbaafaf
	WinSet, Transparent, 50
	Gui, Show, NoActivate h%H%  w%W%  x%X% y%Y%, GUIID
	Sleep, 200
	Gui, Destroy
}
WinGetActiveTitle, TitleWindow
#Persistent
SetTimer, active, 300
Return
active:
WinGetActiveTitle, TitleNew
If (TitleWindow != TitleNew)
	blinkWindow()
TitleWindow := TitleNew
Return
xkooll
Posts: 14
Joined: 02 Sep 2018, 21:47

Re: Create a indicator showing which window is activated

Post by xkooll » 24 Jan 2021, 13:58

Rohwedder wrote:
24 Jan 2021, 12:57
Hallo,
try:

Code: Select all

blinkWindow()
{
	WinGetPos, X, Y, W, H, A
	Gui, +AlwaysOnTop +E0x20 +LastFound +Toolwindow -Caption -DPIScale
	Gui, Color, 0xbaafaf
	WinSet, Transparent, 50
	Gui, Show, NoActivate h%H%  w%W%  x%X% y%Y%, GUIID
	Sleep, 200
	Gui, Destroy
}
WinGetActiveTitle, TitleWindow
#Persistent
SetTimer, active, 300
Return
active:
WinGetActiveTitle, TitleNew
If (TitleWindow != TitleNew)
	blinkWindow()
TitleWindow := TitleNew
Return
xkooll
Posts: 14
Joined: 02 Sep 2018, 21:47

Re: Create a indicator showing which window is activated

Post by xkooll » 24 Jan 2021, 13:59

Rohwedder wrote:
24 Jan 2021, 12:57
Hallo,
try:

Code: Select all

blinkWindow()
{
	WinGetPos, X, Y, W, H, A
	Gui, +AlwaysOnTop +E0x20 +LastFound +Toolwindow -Caption -DPIScale
	Gui, Color, 0xbaafaf
	WinSet, Transparent, 50
	Gui, Show, NoActivate h%H%  w%W%  x%X% y%Y%, GUIID
	Sleep, 200
	Gui, Destroy
}
WinGetActiveTitle, TitleWindow
#Persistent
SetTimer, active, 300
Return
active:
WinGetActiveTitle, TitleNew
If (TitleWindow != TitleNew)
	blinkWindow()
TitleWindow := TitleNew
Return
Hey thanks again!!
I had found a similar solution before I checked your message

Code: Select all

blinkWindow() {
  WinGetPos, X, Y, W, H, A
  
  If %H% {  ; If variable %H% there's no value, then the code below will not run
  Gui Color, 0xbaafaf
  Gui, +AlwaysOnTop +E0x20 +LastFound +Toolwindow -Caption
  
  Gui, Show, NoActivate h%H%  w%W%  x%X% y%Y%, GUIID
  WinSet, Transparent, 50

  Sleep, 200
  Gui, Destroy
  }
}

#Persistent
  SetTimer, active, 300
Return

active:
  WinGet, TitleNew, ID, A
  If (Titlewindow != TitleNew) {
    blinkWindow()
  }

  WinGet, TitleWindow, ID, A
Return 
I was getting a error message from windowBlink() every time I pressed Alt+tab saying that the window has none of the attributes X, Y, W, H.

I solved the problem by creating the follow if statement within windowBlink():

Code: Select all

If %H% {  ; If variable %H% there's no value, then the code below will not run
....
}
Last edited by xkooll on 24 Jan 2021, 14:30, edited 2 times in total.
JamesO2
Posts: 1
Joined: 24 Jan 2021, 13:59

Re: Create a indicator showing which window is activated

Post by JamesO2 » 24 Jan 2021, 14:12

Rohwedder wrote:
24 Jan 2021, 12:57
Hallo,
try:

Code: Select all

blinkWindow()
{
	WinGetPos, X, Y, W, H, A
	Gui, +AlwaysOnTop +E0x20 +LastFound +Toolwindow -Caption -DPIScale
	Gui, Color, 0xbaafaf
	WinSet, Transparent, 50
	Gui, Show, NoActivate h%H%  w%W%  x%X% y%Y%, GUIID
	Sleep, 200
	Gui, Destroy
}
WinGetActiveTitle, TitleWindow
#Persistent
SetTimer, active, 300
Return
active:
WinGetActiveTitle, TitleNew
If (TitleWindow != TitleNew)
	blinkWindow()
TitleWindow := TitleNew
Return
Hi, I'm trying to do something very similar. Your code here works, but it also spits up an error message every time I select a different window. Specifically this error message:

https://i.imgur.com/K4kecfB.png [Mod edit: Link fixed.]
xkooll
Posts: 14
Joined: 02 Sep 2018, 21:47

Re: Create a indicator showing which window is activated

Post by xkooll » 24 Jan 2021, 14:28

JamesO2 wrote:
24 Jan 2021, 14:12
Rohwedder wrote:
24 Jan 2021, 12:57
Hallo,
try:

Code: Select all

blinkWindow()
{
	WinGetPos, X, Y, W, H, A
	Gui, +AlwaysOnTop +E0x20 +LastFound +Toolwindow -Caption -DPIScale
	Gui, Color, 0xbaafaf
	WinSet, Transparent, 50
	Gui, Show, NoActivate h%H%  w%W%  x%X% y%Y%, GUIID
	Sleep, 200
	Gui, Destroy
}
WinGetActiveTitle, TitleWindow
#Persistent
SetTimer, active, 300
Return
active:
WinGetActiveTitle, TitleNew
If (TitleWindow != TitleNew)
	blinkWindow()
TitleWindow := TitleNew
Return
Hi, I'm trying to do something very similar. Your code here works, but it also spits up an error message every time I select a different window. Specifically this error message:

https i.imgur.com /K4kecfB.png Broken Link for safety [Mod edit: Link fixed.]
I was having the same error, I explained how I solved on my previous message, long story short, use that instead:

Code: Select all

blinkWindow() {
  WinGetPos, X, Y, W, H, A
  
  If %H% {  ; If variable %H% there's no value, then the code below will not run
  Gui Color, 0xbaafaf
  Gui, +AlwaysOnTop +E0x20 +LastFound +Toolwindow -Caption
  
  Gui, Show, NoActivate h%H%  w%W%  x%X% y%Y%, GUIID
  WinSet, Transparent, 50

  Sleep, 200
  Gui, Destroy
  }
}
=)
Post Reply

Return to “Ask For Help”