Borderless Window with Powershell

From WickyWiki
Revision as of 16:30, 27 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.

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

#Borderless full screen window
$WinLeft =-1
$WinTop  =0
$WinWidth = $PixelWidth
$WinHeight = $PixelHeight

#WorkingArea - screen area without the taskbar
#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 Borderless Window. TopLeft: '('$WinLeft','$WinTop')' Size: $WinWidth'x'$WinHeight

$GWL_STYLE   = -16            #standard style
$GWL_EXSTYLE = -20            #extended style

$WS_BORDER      = 0x00800000  #window with border
$WS_SYSMENU     = 0x00080000  #window with no borders

$HWND_NOTOPMOST = -2          #above all non-ontop-windows

$SWP_SHOWWINDOW = 0x0040      #show
$SWP_NOZORDER   = 0x0004
$SWP_NOSIZE     = 0x0001

$LWA_ALPHA = 0x00000002       #transparancy alpha method

$user32 = Add-Type -Name 'user32' -Namespace 'Win32' -PassThru -MemberDefinition @'  
  [DllImport("user32.dll")]
  public static extern int GetWindowLong(IntPtr hWnd, int nIndex);
  
  [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);

  [DllImport("user32.dll")]
  public static extern bool DrawMenuBar(IntPtr hWn);

  [DllImport("user32.dll", SetLastError = true)]
  public static extern bool SetLayeredWindowAttributes(IntPtr hWnd, uint crKey, int bAlpha, uint dwFlags);
'@

$ProcessList = Get-Process | Where-Object {$_.MainWindowTitle -ne ""}

Write-Host "Select a window and enter the number:" -ForegroundColor Yellow
for($i = 0; $i -lt $ProcessList.count; $i++){
	Write-Host "$($i): $($ProcessList[$i].MainWindowTitle) | $($ProcessList[$i].ProcessName) "
}
$selection = Read-Host -Prompt "Enter the number of the window"
$WinHandle = $ProcessList[$selection].MainWindowHandle

#transparency
#$transparency = 240 #0..255
#$wl = $user32::GetWindowLong($WinHandle, $GWL_EXSTYLE)
#$user32::SetWindowLong($WinHandle, $GWL_EXSTYLE, ($wl -bOR $WS_BORDER)) | Out-Null
#$user32::SetLayeredWindowAttributes($WinHandle, 0, $transparency, $LWA_ALPHA) | Out-Null

#borderless
$user32::SetWindowLong($WinHandle, $GWL_STYLE, $WS_SYSMENU) | Out-Null	
#move
$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
#redraw
#$user32::DrawMenuBar($WinHandle) | Out-Null