Skip to content

Instantly share code, notes, and snippets.

@trashvin
Created December 12, 2022 11:13
Show Gist options
  • Save trashvin/bc118be43c70d35603692140c0d748a0 to your computer and use it in GitHub Desktop.
Save trashvin/bc118be43c70d35603692140c0d748a0 to your computer and use it in GitHub Desktop.
Set an app window as top most in Windows (based on superuser post https://superuser.com/questions/1554533/make-windows-always-on-top-win10)
Echo TopMost.bat
@Echo Off
Echo This file compiles TopMost.vb to TopMost.exe
Echo TopMost.exe set a window on top or not
Echo To use
Echo TopMost Top ^<windowtitle^>
Echo TopMost Not ^<windowtitle^>
Echo E.G.
Echo TopMost Top Untitled - Notepad
Echo -----------------------------------------------------
"C:\Windows\Microsoft.NET\Framework\v4.0.30319\vbc.exe" /target:winexe /out:"%~dp0\TopMost.exe" "%~dp0\TopMost.vb"
pause
Get-Process | Where {
$_.MainWindowTitle
} |
Select-Object ProcessName, MainWindowTitle
'this the simple code to make an app window topmost
'build the code to .exe using the bat file
'
'usage
' > TopMost Top <window title>
' > TopMost Not <window title>
'
'note : the window title should not have quotations
'use the script TopMost.ps1 to display all window titles
'TopMost.vb
Imports System
Imports System.IO
Imports System.Runtime.InteropServices
Imports Microsoft.Win32
Public Module TopMost
Public Declare UNICODE Function FindWindowW Lib "user32" (ByVal lpClassName As String, ByVal lpWindowName As String) As IntPtr
Public Declare Function SetWindowPos Lib "user32" (ByVal hwnd As IntPtr, ByVal hWndInsertAfter As Integer, ByVal x As Integer, ByVal y As Integer, ByVal cx As Integer, ByVal cy As Integer, ByVal wFlags As Integer) As Integer
Public Const HWND_TOPMOST = -1
Public Const SWP_NOMOVE = &H2
Public Const SWP_NOSIZE = &H1
Public Const HWND_NOTOPMOST = -2
Sub Main()
On Error Resume Next
Dim hWindows as IntPtr
Dim CmdLine as String
Dim Ret as Integer
CmdLine = Mid(Command(),5)
hwindows = FindWindowW(vbNullString, CmdLine)
If hwindows = 0 then
Msgbox(Cmdline & " cannot be found.")
Else
If LCase(Left(Command(), 3)) = LCase("Top") then
Ret = SetWindowPos(hwindows, HWND_TOPMOST, 0, 0, 0, 0, SWP_NOMOVE + SWP_NOSIZE)
If Ret = 0 Then MsgBox("Set Pos Error is " & Err.LastDllError)
ElseIf LCase(Left(Command(), 3)) = LCase("Not") then
Ret = SetWindowPos(hwindows, HWND_NOTOPMOST, 0, 0, 0, 0, SWP_NOMOVE + SWP_NOSIZE)
If Ret = 0 Then MsgBox("Set Pos Error is " & Err.LastDllError)
Else
Msgbox("Command line not recognised")
End If
End If
End Sub
End Module
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment