Skip to content

Instantly share code, notes, and snippets.

@simply-coded
Last active July 20, 2024 14:38
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

haseakash commented May 31, 2021

I test this code.. it works to set CMD on top.
Also chrome as top.

But i want to run my test.exe as top of all windows.
i created a batch file and converted to exe.
my test.exe is used to check keyboard/mouse is faulty or not

i used findstr method in that code
utility checks a bad keys are pressing or not within 10 sec timer

my utility is works best than ever

But....

@ 1 condition it failed.

( if a key is already pressed and hold (held) continuously before launching my program.
i find out after hard way why it failed.
And found a reason

  • if a key (example z key) is pressed + hold (held) before launching my program,
  • AT this point . running exe is going to (not on top) white blur window (ie current running window is not selected)

So i need ur code to run my exe to set on top of all windows for 10 seconds

please reply how to set .. share full vbs script

see below 1st one is without key press and 2nd one is with key was pressed and holded before launching program.

WATCH TITAL OF WINDOW (to see difference)

without key press
z key pressed and holding then exe launched

@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