Reliable way to mark arbitrary window as always-on-top

Get help with using AutoHotkey (v2 or newer) and its commands and hotkeys
john_c
Posts: 493
Joined: 05 May 2017, 13:19

Reliable way to mark arbitrary window as always-on-top

19 Jul 2022, 18:52

I have a simple function to keep window on top.

The problem is that I want to easily distinguish between always-on-top and normal windows when I look on the screen.

I tried to add a prefix "[Pinned]", but this doesn't work well for Notepad3 (it seems the program silently update its title each time you switch to another application and back)

I tried to move text to the right using ExStyle, but this doesn't work for VS Code.

What other good options?

Code: Select all

KeepWindowOnTop() {
    Title := WinGetTitle("A")
    ExStyle := WinGetExStyle(Title)
    if (ExStyle & 0x8) { ; 0x8 stands for WS_EX_TOPMOST.
        WinSetAlwaysOnTop False, Title
        WinSetExStyle -0x1000, Title
    }
    else {
        WinSetAlwaysOnTop True, Title
        WinSetExStyle +0x1000, Title ; Moves title to the right
    }
}
User avatar
FredOoo
Posts: 186
Joined: 07 May 2019, 21:58
Location: Paris

Re: Reliable way to mark arbitrary window as always-on-top

19 Jul 2022, 21:14

I use almost the same.

Notepad change the title only when I save the file.

Code: Select all

^SPACE::{
	;➽ Toggle window always-on-top
	if not winID := winExist('A')
		return
	winTitle   := winGetTitle(winID)
	winExStyle := winGetExStyle(winID)
	if winExStyle & 0x8 {  ; 0x8 is WS_EX_TOPMOST.
		if askBox( "Free on-top?" ) {
			winSetAlwaysOnTop( false, winID )
			toggleBeep( false )
			winSetExStyle -0x1000, winID ; Moves title to the left
		}
	} else {
		if askBox( "Keep on-top?" ) {
			winSetAlwaysOnTop( true, winID )
			toggleBeep( true )
			;winSetTitle( "[ONTOP]", winID )
			winSetExStyle +0x1000, winID ; Moves title to the right
		}
	}
	askBox( msg ){
		return 'ok'=msgBox( winTitle "`n" msg, "Always-on-top", "OKCancel Icon! 4096 Default1")
	}
}
toggleBeep( cond ) {
	;➽ Global for set/unset, up/down
	if cond
		soundBeep 2500, 90
	else
		soundBeep 450, 110
}
Last edited by FredOoo on 20 Jul 2022, 00:44, edited 1 time in total.
(Alan Turing) « What would be the point of saying that A = B if it was really the same thing? »
(Albert Camus) « Misnaming things is to add to the misfortunes of the world. »
User avatar
FredOoo
Posts: 186
Joined: 07 May 2019, 21:58
Location: Paris

Re: Reliable way to mark arbitrary window as always-on-top

19 Jul 2022, 22:22

And I just see it doesn’t toggle FilesExplorer windows's always-on-top style.
I don't know why.
Maybe FilesExplorer windows's super-power ?
(Alan Turing) « What would be the point of saying that A = B if it was really the same thing? »
(Albert Camus) « Misnaming things is to add to the misfortunes of the world. »
User avatar
FredOoo
Posts: 186
Joined: 07 May 2019, 21:58
Location: Paris

Re: Reliable way to mark arbitrary window as always-on-top

19 Jul 2022, 23:08

With a check if toggle always-on-top worked.

Code: Select all

^SPACE::{
	;➽ Fixer une fenêtre toujours au-dessus
	if not winID := winExist('A') {
		;(if you right-click a tray icon, key-escape the menu and ^Space, you will arrive here)
		errorBeep
		return
	}
	if winGetExStyle(winID) & 0x8 {  ; 0x8 = WS_EX_TOPMOST
		if askBox( "Ne plus mettre toujours au-dessus ?" ) {
			winSetAlwaysOnTop( false, winID )
			if not winGetExStyle(winID) & 0x8
				toggleBeep( false )
			else
				errorBeep
		}
	} else {
		if askBox( "Mettre toujours au-dessus ?" ) {
			winSetAlwaysOnTop( true, winID )
			if winGetExStyle(winID) & 0x8
				toggleBeep( true )
			else
				errorBeep
		}
	}
	askBox( msg ){
		return 'ok'=msgBox( winGetTitle(winID) "`n" msg, "Fenêtre au-dessus", "OKCancel Icon! 4096 Default1")
	}
}
toggleBeep( cond ) {
	cond
		? soundBeep( 2500, 90 )
		: soundBeep( 450, 110 )
}
errorBeep() {
	soundBeep( 170, 120 )
}
(Alan Turing) « What would be the point of saying that A = B if it was really the same thing? »
(Albert Camus) « Misnaming things is to add to the misfortunes of the world. »
User avatar
FredOoo
Posts: 186
Joined: 07 May 2019, 21:58
Location: Paris

Re: Reliable way to mark arbitrary window as always-on-top

20 Jul 2022, 21:51

A little better

Code: Select all

^SPACE::{
	;➽ Toggle window always-on-top
	if not winID := winExist('A') {
		; If you right-click a tray icon, key-escape the menu and ^Space, you will arrive here
		errorBeep
		return
	}
	if winGetExStyle(winID) & 0x8 {  ; 0x8 = WS_EX_TOPMOST
		if askBox( "Free on-top?" ) {
			winSetAlwaysOnTop( false, winID )
			checkIt false
		}
	} else {
		if askBox( "Keep on-top?" ) {
			winSetAlwaysOnTop( true, winID )
			checkIt true
		}
	}
	checkIt( TrueFalse ){
		; FilesExplorer windows (maybe others) don't toggle always-on-top style
		if TrueFalse=((winGetExStyle(winID) & 0x8)>0)
			toggleBeep TrueFalse
		else
			errorBeep
	}
	askBox( msg ){
		; One more Space to confirm
		return 'ok'=msgBox( winGetTitle(winID) "`n" msg, "Always-on-top", "OKCancel Icon! 4096 Default1")
	}
}
toggleBeep( cond ) {
	;➽ GlobalApp for set/unset, up/down … onSettings feed back
	cond
	? soundBeep( 2500, 90 )
	: soundBeep( 450, 110 )
}
errorBeep() {
	;➽ GlobalApp errors feed back
	soundBeep( 170, 120 )
}
(Alan Turing) « What would be the point of saying that A = B if it was really the same thing? »
(Albert Camus) « Misnaming things is to add to the misfortunes of the world. »
User avatar
FredOoo
Posts: 186
Joined: 07 May 2019, 21:58
Location: Paris

Re: Reliable way to mark arbitrary window as always-on-top

24 Jul 2022, 17:46


 • Dialog box display in the same screen as the window it talks about.
 • Window's application name is now displayed in the dialog box.
 • When a window is set alwaysOnTop, it's tranparency is a little modified.
john_c wrote:
19 Jul 2022, 18:52
The problem is that I want to easily distinguish between always-on-top and normal windows when I look on the screen.

Code: Select all

 ^SPACE::{
	;➽	Toggle window always-on-top.
	if not winID := winExist('A') {
		; If you right-click a tray icon, key-escape the menu and ^Space, you will arrive here.
		errorBeep
		return
	}
	if winGetExStyle(winID) & 0x8 {  ; 0x8 • WS_EX_TOPMOST
		if askBox( "Free on-top?" ) {
			winSetAlwaysOnTop false, winID
			checkIt false
		}
	} else {
		if askBox( "Keep on-top?" ) {
			winSetAlwaysOnTop true, winID
			checkIt true
		}
	}
	checkIt( TrueFalse ){
		;➽	Check alwaysOnTop setting is done.
		;	FilesExplorer windows (maybe others) don't toggle always-on-top style.
			if TrueFalse=((winGetExStyle(winID) & 0x8)>0) {
				winSetTransparent TrueFalse?225:255, winID ; 255 • opaque
				toggleBeep TrueFalse
			} else
				errorBeep
	}
	askBox( msg ){
		;➽	Ask confirmation.
		;	One more Space to confirm.
		txt := appName() " :"       '`n`n'
		     . winGetTitle(winID)   '`n`n'
		     . msg
		return 'ok'=msgBox(txt, "Always-on-top", "OKCancel Icon! 4096 Default1 Owner" winID)
	}
	appName(){
		;➽	Get application name.
		;	"sublime_text.exe" → "Sublime Text"
		splitPath winGetProcessName(winID) ,,,, &name
		return strTitle( strReplace(name, "_", " ") )
	}
}
toggleBeep( OnOff ) {
	;➽	GlobalApp for set/unset, up/down … onSettings feed back.
	OnOff
	? soundBeep( 2500, 90 )
	: soundBeep( 450, 110 )
}
errorBeep() {
	;➽	GlobalApp errors feed back.
	soundBeep( 170, 120 )
}
(Alan Turing) « What would be the point of saying that A = B if it was really the same thing? »
(Albert Camus) « Misnaming things is to add to the misfortunes of the world. »

Return to “Ask for Help (v2)”

Who is online

Users browsing this forum: macromint and 65 guests