Window Pin Function (With Code & File)

Post your working scripts, libraries and tools for AHK v1.1 and older
User avatar
David 9225
Posts: 2
Joined: 10 Feb 2021, 17:16
Contact:

Window Pin Function (With Code & File)

10 Feb 2021, 19:58

Does your text editor often hide behind your reference documents? Do you wish there was a lightweight way to prevent this from happening?
Well, this is just the right script for you!

Here's how it works: pressing "P" while the right Windows key is held will label the active window as "- AlwaysOnTop". Even if you tab into a different window, the pinned window stays on top of the stack.
Using the same key combination will unpin the window, and pin the currently active window (if applicable).

I've included a script file, so those interested may download it. Hopefully you all can learn from tweaking this function as I have! :D

Code: Select all

;------Hotkey------
<#p::  ;Win+P
  pinWindow()
  RETURN
;------------------
  
;---------------Function---------------
;Makes the active window AlwaysOnTop
pinWindow(targetWindow := "A")
{
	static pWnd := 0
	static pinned := FALSE
	tWnd := WinActive(targetWindow)	;Defines %tWnd% as the ahk_id for the target window
	IF (pinned NOT TRUE)
	{
		WinSet, AlwaysOnTop, Toggle, % "ahk_id " tWnd	;Sets the target window as AlwaysOnTop
		WinGetTitle, title, % "ahk_id " tWnd	;Retrieves the title for the target window.
		IF title	;Runs code if title isn't blank.
		{
			Gosub ChangeTitle
		}
		pinned := TRUE	;Used for UnpinWindow logic
		pWnd := tWnd	;Used for unpinning window before pinning the next window.
	}
	ELSE
	{
		Gosub UnpinWindow
	}
	
ChangeTitle:
	WinGet, ExStyle, ExStyle, % "ahk_id " tWnd	;Checks the style of the target window.
	IF (ExStyle & 0x8)	;0x8 is for AlwaysOnTop
	{
		WinSetTitle, % "ahk_id " tWnd,, %title% - AlwaysOnTop	;Adds "- AlwaysOnTop" to the window title.
	}
	ELSE
	{
		WinSetTitle, % "ahk_id " tWnd,, % RegexReplace(title, " - AlwaysOnTop")	;Removes "- AlwaysOnTop" to the window title.
	}
	RETURN
UnpinWindow:
	WinSet, AlwaysOnTop, Off, % "ahk_id " pWnd	;Sets the target window as AlwaysOnTop
	WinGetTitle, title, % "ahk_id " pWnd	;Retrieves the title for the target window.
	WinSetTitle, % "ahk_id " pWnd,, % RegexReplace(title, " - AlwaysOnTop")	;Removes indicator from the window title.
	IF (tWnd != pWnd)
	{
		WinSet, AlwaysOnTop, Toggle, % "ahk_id " tWnd	;Sets the target window as AlwaysOnTop
		WinGetTitle, title, % "ahk_id " tWnd	;Retrieves the title for the target window.
		IF title
		{
			Gosub ChangeTitle
		}
		pWnd := tWnd
	}
	ELSE
	{
		pinned := FALSE
	}
	RETURN
}
;--------------------------------------
Attachments
pinWindow.ahk
(2.64 KiB) Downloaded 182 times
User avatar
David 9225
Posts: 2
Joined: 10 Feb 2021, 17:16
Contact:

Thank you!

10 Feb 2021, 20:01

I would like to thank those on the AutoHotKey Discord Server for reviewing my code, and teaching me some underlying concepts relevant to this function. :superhappy:
User avatar
Xeo786
Posts: 760
Joined: 09 Nov 2015, 02:43
Location: Karachi, Pakistan

Re: Window Pin Function (With Code & File)

11 Feb 2021, 00:40

I was simply using following code before title change idea, :bravo:

Code: Select all

^SPACE::  ; Winset, Alwaysontop, , A
I tried your code its works great, but I would like to simplify it a little bit if you do not mind :thumbup: ,

Code: Select all

^SPACE:: pinWindow()

pinWindow(targetWindow := "A")
{
	tWnd := WinActive(targetWindow)
	WinGetTitle, title, % "ahk_id " tWnd
	WinSetTitle, % "ahk_id " tWnd,, % instr(title," - AlwaysOnTop") ? RegexReplace(title, " - AlwaysOnTop") : title " - AlwaysOnTop"
	WinSet, AlwaysOnTop,, % "ahk_id " tWnd
}
"When there is no gravity, there is absolute vacuum and light travel with no time" -Game changer theory
delseyyy
Posts: 8
Joined: 01 Jan 2018, 17:00

Re: Window Pin Function (With Code & File)

06 May 2024, 04:51

I tried adapting xeo's code to v2.

Code: Select all

<#p:: { ;Win+P
	pinWindow()
	return
}

pinWindow(targetWindow := "A") {
	tWnd := WinActive(targetWindow)
	title := WinGetTitle("ahk_id " tWnd)
	newTitle := InStr(title, " - AlwaysOnTop") ? RegExReplace(title, " - AlwaysOnTop$") : title " - AlwaysOnTop"
	WinSetTitle(newTitle, "ahk_id " tWnd)
	WinSetAlwaysOnTop(-1, "ahk_id " tWnd) 
}

Return to “Scripts and Functions (v1)”

Who is online

Users browsing this forum: No registered users and 44 guests