Skip to content

Instantly share code, notes, and snippets.

@simply-coded
Last active May 31, 2021 12:33
Show Gist options
  • Save simply-coded/3319aca9046bdb85efe2c809a7bd2744 to your computer and use it in GitHub Desktop.
Save simply-coded/3319aca9046bdb85efe2c809a7bd2744 to your computer and use it in GitHub Desktop.
Makes a window always on top if setOnTop is true, else makes it normal again. Will wait up to 10 seconds for window to load.
Sub AlwaysOnTop(appName, regExpTitle, setOnTop)
' @description: Makes a window always on top if setOnTop is true, else makes it normal again. Will wait up to 10 seconds for window to load.
' @author: Jeremy England (SimplyCoded)
If (setOnTop) Then setOnTop = "-1" Else setOnTop = "-2"
CreateObject("wscript.shell").Run "powershell -Command """ & _
"$Code = Add-Type -MemberDefinition '" & vbcrlf & _
" [DllImport(\""user32.dll\"")] public static extern bool SetWindowPos(IntPtr hWnd, IntPtr hWndInsertAfter, int X,int Y, int cx, int cy, uint uFlags);" & vbcrlf & _
" [DllImport(\""user32.dll\"")] public static extern bool ShowWindow(IntPtr hWnd, int nCmdShow);" & vbcrlf & _
" public static void AlwaysOnTop (IntPtr fHandle, int insertAfter) {" & vbcrlf & _
" if (insertAfter == -1) { ShowWindow(fHandle, 7); ShowWindow(fHandle, 9); }" & vbcrlf & _
" SetWindowPos(fHandle, new IntPtr(insertAfter), 0, 0, 0, 0, 3);" & vbcrlf & _
" }' -Name PS -PassThru" & vbcrlf & _
"for ($s=0;$s -le 9; $s++){$hWnd = (GPS " & appName & " -EA 0 | ? {$_.MainWindowTitle -Match '" & regExpTitle & "'}).MainWindowHandle;Start-Sleep 1;if ($hWnd){break}}" & vbcrlf & _
"$Code::AlwaysOnTop($hWnd, " & setOnTop & ")""", 0, True
End Sub
'Make an always on top input box.
AlwaysOnTop "wscript", "My Title", True
result = InputBox("this will be on top of all windows", "My Title")
'Make chrome always on top for 10 seconds, and then return it to normal.
AlwaysOnTop "chrome", ".", True
WScript.Sleep 10000
AlwaysOnTop "chrome", ".", False
'----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
Sub AlwaysOnTop(appName, regExpTitle, setOnTop)
' @description: Makes a window always on top if setOnTop is true, else makes it normal again. Will wait up to 10 seconds for window to load.
' @author: Jeremy England (SimplyCoded)
If (setOnTop) Then setOnTop = "-1" Else setOnTop = "-2"
CreateObject("wscript.shell").Run "powershell -Command """ & _
"$Code = Add-Type -MemberDefinition '" & vbcrlf & _
" [DllImport(\""user32.dll\"")] public static extern bool SetWindowPos(IntPtr hWnd, IntPtr hWndInsertAfter, int X,int Y, int cx, int cy, uint uFlags);" & vbcrlf & _
" [DllImport(\""user32.dll\"")] public static extern bool ShowWindow(IntPtr hWnd, int nCmdShow);" & vbcrlf & _
" public static void AlwaysOnTop (IntPtr fHandle, int insertAfter) {" & vbcrlf & _
" if (insertAfter == -1) { ShowWindow(fHandle, 7); ShowWindow(fHandle, 9); }" & vbcrlf & _
" SetWindowPos(fHandle, new IntPtr(insertAfter), 0, 0, 0, 0, 3);" & vbcrlf & _
" }' -Name PS -PassThru" & vbcrlf & _
"for ($s=0;$s -le 9; $s++){$hWnd = (GPS " & appName & " -EA 0 | ? {$_.MainWindowTitle -Match '" & regExpTitle & "'}).MainWindowHandle;Start-Sleep 1;if ($hWnd){break}}" & vbcrlf & _
"$Code::AlwaysOnTop($hWnd, " & setOnTop & ")""", 0, True
End Sub '------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
@haseakash
Copy link

Update --

Now i have found another problem of ur code

This code works if single exe is launching

i test to set 1 winrar minimized
and edit code with name Winrar

it works .........

but ...

if i open two different winrar windows and make both minimized
then this code is not works...

Thats a problem,

because My test exe is having command to launch anathor cmd window
at this time two cmd are opend.
one is background by windows and one is launched

So my question is

How do i set last open window to " Always on top" ??????????????????????

help

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment