Borderless Window with Powershell: Difference between revisions

From WickyWiki
mNo edit summary
mNo edit summary
 
(One intermediate revision by the same user not shown)
Line 1: Line 1:
[[Category:Windows Powershell]]
[[Category:Windows Powershell]]
[[Category:202103]]
[[Category:202103]]
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.


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.
 
<source lang=powershell>
$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


Some software will override these settings.
$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);
'@


<syntaxhighlight lang=powershell>
$vc = Get-WmiObject -class "Win32_VideoController"
$vc = Get-WmiObject -class "Win32_VideoController"
$PixelWidth = $vc.CurrentHorizontalResolution
$PixelWidth = $vc.CurrentHorizontalResolution
$PixelHeight = $vc.CurrentVerticalResolution
$PixelHeight = $vc.CurrentVerticalResolution


#Borderless full screen window
$WinLeft =-1
$WinLeft =-1
$WinTop  =0
$WinTop  =0
Line 17: Line 30:
$WinHeight = $PixelHeight
$WinHeight = $PixelHeight


#WorkingArea - screen area without the taskbar
Write-Host Apply borderless window. $WinWidth'x'$WinHeight at '('$WinLeft','$WinTop')' Available windows: -ForegroundColor Yellow
#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
$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


$GWL_STYLE   = -16            #standard style
#set borderless
$GWL_EXSTYLE = -20            #extended style
$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


$WS_BORDER      = 0x00800000  #window with border
Write-Host "Press ENTER to continue: " -NoNewline -ForegroundColor Yellow
$WS_SYSMENU    = 0x00080000  #window with no borders
Read-Host
</source>


$HWND_NOTOPMOST = -2          #above all non-ontop-windows
To get only the 'WorkingArea' - screen area without the taskbar:


$SWP_SHOWWINDOW = 0x0040      #show
<source lang=powershell>
$SWP_NOZORDER  = 0x0004
$vc = Get-WmiObject -class "Win32_VideoController"
$SWP_NOSIZE    = 0x0001
$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')'
</source>


$LWA_ALPHA = 0x00000002      #transparancy alpha method
If you want set transparency:


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


$ProcessList = Get-Process | Where-Object {$_.MainWindowTitle -ne ""}
$WS_BORDER    = 0x00800000 #window with border
 
$GWL_EXSTYLE  = -20        #extended style
Write-Host "Select a window and enter the number:" -ForegroundColor Yellow
$LWA_ALPHA    = 0x00000002 #transparancy alpha method
for($i = 0; $i -lt $ProcessList.count; $i++){
$transparency = 220        #0..255
Write-Host "$($i): $($ProcessList[$i].MainWindowTitle) | $($ProcessList[$i].ProcessName) "
$wl = $user32add::GetWindowLong($WinHandle, $GWL_EXSTYLE)
}
$user32::SetWindowLong($WinHandle, $GWL_EXSTYLE, ($wl -bOR $WS_BORDER)) | Out-Null
$selection = Read-Host -Prompt "Enter the number of the window"
$user32add::SetLayeredWindowAttributes($WinHandle, 0, $transparency, $LWA_ALPHA) | Out-Null
$WinHandle = $ProcessList[$selection].MainWindowHandle
</source>
 
#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


</syntaxhighlight>
Microsoft documentation:
* https://docs.microsoft.com/en-us/windows/win32/api/winuser/nf-winuser-setwindowpos

Latest revision as of 12:54, 23 June 2021

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: