How to get batch/powershell MessageBox and InputBox windows to display always on top of other windows?

208 Views Asked by At

I am writing a batch script to open a series of MessageBox and InputBox windows. The program works well when it is first launched and the users are able to interact with the windows, but if they click on any other programs it brings them to the foreground and essentially hides the message/input boxes. Here is an example of two of the boxes, how can I keep these always on top of other windows while the program is being used?

powershell -Command "& {Add-Type -AssemblyName Microsoft.VisualBasic; [Microsoft.VisualBasic.Interaction]::InputBox(\"`r`nEnter the unique component number:`r`n\", 'Component Number')}" > %TEMP%\uniqueno1.tmp
set /p UniqueNo1=<%TEMP%\uniqueno1.tmp

powershell [Reflection.Assembly]::LoadWithPartialName("""System.Windows.Forms""");[Windows.Forms.MessageBox]::show(\"Unique Component Number %UniqueNo1%`r`n`r`n Click OK on this window to continue\", 'Component Number',0)>nul

1

There are 1 best solutions below

2
7three On

Try this:

powershell [Reflection.Assembly]::LoadWithPartialName("""System.Windows.Forms""");[Windows.Forms.MessageBox]::show(\"Unique Component Number %UniqueNo1%`r`n`r`n Click OK on this window to continue\", 'Component Number',0,0,0,131072)>nul

Flag enum 131072 stands for DefaultDesktopOnly.

See https://learn.microsoft.com/en-us/dotnet/api/system.windows.forms.messageboxoptions?view=windowsdesktop-7.0

Alternative could be (new-object System.Windows.Window -Property @{TopMost = true})

powershell [Reflection.Assembly]::LoadWithPartialName("""System.Windows.Forms""");[Windows.Forms.MessageBox]::show((New-Object System.Windows.Forms.Form -Property @{TopMost = $true }),\"Unique Component Number %UniqueNo1%`r`n`r`n Click OK on this window to continue\", 'Component Number',0)>nul

But this one didn't work for me. Maybe it's meant to be used in a other way, no idea.