Borderless Window with Powershell

From WickyWiki
Revision as of 10:04, 28 March 2021 by Wilbert (talk | contribs)

This script allows you to select the window from a list that you want to have as a borderless window that fills your screen. You could try this for a game that does not natively supports this function.

Note that some applications will override these settings.

$GWL_STYLE      = -16        #normal styling
$WS_SYSMENU     = 0x00080000 #window with no borders
$HWND_NOTOPMOST = -2         #above all non-ontop-windows
$SWP_SHOWWINDOW = 0x0040     #show
$SWP_NOZORDER   = 0x0004     #do not change z-order
$SWP_NOSIZE     = 0x0001     #do not resize

$user32 = Add-Type -Name 'user32' -Namespace 'Win32' -PassThru -MemberDefinition @'  
  [DllImport("user32.dll")]
  public static extern int SetWindowLong(IntPtr hWnd, int nIndex, int dwNewLong);
  
  [DllImport("user32.dll")]
  public static extern IntPtr SetWindowPos(IntPtr hWnd, int hWndInsertAfter, int x, int y, int cx, int cy, int wFlags);
'@

$vc = Get-WmiObject -class "Win32_VideoController"
$PixelWidth = $vc.CurrentHorizontalResolution
$PixelHeight = $vc.CurrentVerticalResolution

$WinLeft =-1
$WinTop  =0
$WinWidth = $PixelWidth
$WinHeight = $PixelHeight

Write-Host Apply borderless window. $WinWidth'x'$WinHeight at '('$WinLeft','$WinTop')' Available windows: -ForegroundColor Yellow

$ProcessList = Get-Process | Where-Object {$_.MainWindowTitle -ne ""}
for($i = 0; $i -lt $ProcessList.count; $i++){
	Write-Host " $($i+1): $($ProcessList[$i].MainWindowTitle) | $($ProcessList[$i].ProcessName) "
}
Write-Host "Type the number to select the window and press ENTER: " -NoNewline -ForegroundColor Yellow
$i = Read-Host
$WinHandle = $ProcessList[$i-1].MainWindowHandle

#set borderless
$user32::SetWindowLong($WinHandle, $GWL_STYLE, $WS_SYSMENU) | Out-Null	
#topleft position
$user32::SetWindowPos($WinHandle, $HWND_NOTOPMOST, $WinLeft, $WinTop, $WinWidth, $WinHeight, ($SWP_NOSIZE -bOR $SWP_NOZORDER)) | Out-Null
#size (must be done as separate command)
$user32::SetWindowPos($WinHandle, $HWND_NOTOPMOST, $WinLeft, $WinTop, $WinWidth, $WinHeight, $SWP_SHOWWINDOW) | Out-Null

Write-Host "Press ENTER to continue: " -NoNewline -ForegroundColor Yellow
Read-Host

To get only the 'WorkingArea' - screen area without the taskbar:

$vc = Get-WmiObject -class "Win32_VideoController"
$PixelWidth = $vc.CurrentHorizontalResolution
Add-Type -AssemblyName System.Windows.Forms
$ScaledWidth = [System.Windows.Forms.Screen]::PrimaryScreen.Bounds.Width
$Scale = $PixelWidth / $ScaledWidth 
$WinLeft = [System.Windows.Forms.Screen]::PrimaryScreen.WorkingArea.X * $Scale -1
$WinTop = [System.Windows.Forms.Screen]::PrimaryScreen.WorkingArea.Y * $Scale
$WinWidth = [System.Windows.Forms.Screen]::PrimaryScreen.WorkingArea.Width * $Scale
$WinHeight = [System.Windows.Forms.Screen]::PrimaryScreen.WorkingArea.Height * $Scale
Write-Host $WinWidth'x'$WinHeight at '('$WinLeft','$WinTop')'

If you want set transparency:

$user32add = Add-Type -Name 'user32add' -Namespace 'Win32' -PassThru -MemberDefinition @'  
  [DllImport("user32.dll")]
  public static extern int GetWindowLong(IntPtr hWnd, int nIndex);
 
  [DllImport("user32.dll", SetLastError = true)]
  public static extern bool SetLayeredWindowAttributes(IntPtr hWnd, uint crKey, int bAlpha, uint dwFlags);
'@

$WS_BORDER    = 0x00800000 #window with border
$GWL_EXSTYLE  = -20        #extended style
$LWA_ALPHA    = 0x00000002 #transparancy alpha method
$transparency = 220        #0..255
$wl = $user32add::GetWindowLong($WinHandle, $GWL_EXSTYLE)
$user32::SetWindowLong($WinHandle, $GWL_EXSTYLE, ($wl -bOR $WS_BORDER)) | Out-Null
$user32add::SetLayeredWindowAttributes($WinHandle, 0, $transparency, $LWA_ALPHA) | Out-Null

Microsoft documentation: