当前位置:网站首页 > 更多 > 玩电脑 > 正文

[玩转系统] 周五乐趣:衡量我的大小

作者:精品下载站 日期:2024-12-14 07:45:03 浏览:14 分类:玩电脑

周五乐趣:衡量我的大小


日常工作的一部分涉及创建培训材料,通常采用 Pluralsight 视频培训或 Petri.com 文章的形式。由于我通常涉及 PowerShell,因此我经常需要捕获 PowerShell 会话。有时我希望屏幕具有特定的尺寸。因此,随着时间的推移,我创建了一些 PowerShell 工具来调整控制台和应用程序窗口的大小。 PowerShell 控制台窗口将其尺寸存储在 $host.ui.rawui.windowsize 下。

[玩转系统] 周五乐趣:衡量我的大小

这些设置与您在此处看到的设置相同:

[玩转系统] 周五乐趣:衡量我的大小

只要您使用小于缓冲区尺寸的值,您就可以通过提示修改控制台窗口。但这需要您可能没有意识到的一步。你不能这样做:

$host.ui.rawui.WindowSize.Height = 40

相反,您可以创建具有所需尺寸的新型对象。

$size = New-Object System.Management.Automation.Host.Size(100,25)

然后您可以使用该对象作为 WindowSize 属性的值。

$host.ui.rawui.WindowSize = $size

[玩转系统] 周五乐趣:衡量我的大小

当然,我创建了一个函数来为我执行此操作。

#requires -version 3.0

#not for the ISE

Function Set-ConsoleWindowSize {
[cmdletbinding(SupportsShouldProcess)]

Param(
[Alias("x")]
[ValidateScript({$_ -ge 10 -AND $_ -le $host.ui.rawui.buffersize.width})]
[int]$Width = $host.ui.rawui.windowsize.width,

[Alias("y")]
[ValidateScript({$_ -ge 10 -AND $_ -le $host.ui.rawui.buffersize.height})]
[int]$Height = $host.ui.rawui.windowsize.height
)

    write-verbose "Setting dimensions to $width x $height"  
    $size = New-Object System.Management.Automation.Host.Size($Width,$Height)
    
    #WhatIf code
    if ($PSCmdlet.ShouldProcess("$width by $height")) {
        $host.ui.rawui.WindowSize = $size
    }
}

我的函数还包括支持 -WhatIf 的代码。

[玩转系统] 周五乐趣:衡量我的大小

当然,既然我已经向你展示了我有一个替代方案。您可以使用 .NET 类 [System.Console],它具有宽度和高度属性。您可以独立设置这些值。

[system.console]::WindowHeight = 20

[玩转系统] 周五乐趣:衡量我的大小

除非您了解 .NET Framework,否则您无法发现这一点,但您可能已经发现了 $host,这就是我首先向您展示的原因。由于我经常需要以 1280×720 尺寸录制视频,因此我编写了一个快速但肮脏的脚本来将我的 PowerShell 控制台窗口设置为这些尺寸。

#requires -version 3.0

#set PowerShell console to 1280x720
#This is NOT for the ISE

[cmdletbinding(SupportsShouldProcess)]
Param()

$h = 33
$w = 103

Write-Verbose "Setting Height to $h"
Write-Verbose "Setting Width to $w"

if ($PSCmdlet.shouldProcess("current session")) {
    [system.console]::WindowHeight = $h
    [system.console]::WindowWidth = $w
}

[玩转系统] 周五乐趣:衡量我的大小

到目前为止我向您展示的所有内容都是针对 PowerShell 控制台的。但是 ISE 呢?您不能使用我介绍的技术。应用程序窗口有点复杂,我不打算详细介绍。但我在 GitHub 上发现了一些代码(https://gist.github.com/coldnebo/1148334)。我不做《我的世界》,但没花太多时间就把它变成可重用的功能。

#requires -version 3.0

#from https://gist.github.com/coldnebo/1148334

Function Set-WindowSize {

[cmdletbinding(SupportsShouldProcess)]
Param(
[Parameter(Position=0,HelpMessage="What is the MainWindowHandle?",
ValueFromPipeline,ValueFromPipelineByPropertyName)]
[ValidateNotNullorEmpty()]
[Alias("handle")]
[System.IntPtr]$MainWindowHandle = (Get-Process -id $pid).MainWindowHandle,
[Alias("x")]
[int]$Width = 1280,
[Alias("y")]
[int]$Height = 720

)

Begin {
    Write-Verbose "Starting $($MyInvocation.Mycommand)" 
    
    Write-Verbose "Adding type"

Add-Type @"
  using System;
  using System.Runtime.InteropServices;
 
  public class Win32 {
    [DllImport("user32.dll")]
    [return: MarshalAs(UnmanagedType.Bool)]
    public static extern bool GetWindowRect(IntPtr hWnd, out RECT lpRect);
 
    [DllImport("user32.dll")]
    [return: MarshalAs(UnmanagedType.Bool)]
    public static extern bool GetClientRect(IntPtr hWnd, out RECT lpRect);
 
    [DllImport("user32.dll")]
    [return: MarshalAs(UnmanagedType.Bool)]
    public static extern bool MoveWindow(IntPtr hWnd, int X, int Y, int nWidth, int nHeight, bool bRepaint);
  }
 
  public struct RECT
  {
    public int Left;        // x position of upper-left corner
    public int Top;         // y position of upper-left corner
    public int Right;       // x position of lower-right corner
    public int Bottom;      // y position of lower-right corner
  }
 
"@
  
} #begin

Process {

#verify handle and get process

Write-Verbose "Verifying process with MainWindowHandle of $MainWindowhandle"

$proc = (Get-Process).where({$_.mainwindowhandle -eq $MainWindowhandle})

if (-NOT $proc.mainwindowhandle) {
    Write-Warning "Failed to find a process with a MainWindowHandle of $MainWndowhandle"
    #bail out
    Return
}

Write-Verbose "Creating rectangle objects" 
$rcWindow = New-Object RECT
$rcClient = New-Object RECT

Write-Verbose "Getting current settings"
[Win32]::GetWindowRect($MainWindowHandle,[ref]$rcWindow) | Out-Null
[Win32]::GetClientRect($MainWindowHandle,[ref]$rcClient) | Out-Null
 
Write-Verbose "Setting new coordinates"

#WhatIf
if ($PSCmdlet.ShouldProcess("$($proc.MainWindowTitle) to $width by $height")) {
    $dx = ($rcWindow.Right - $rcWindow.Left) - $rcClient.Right
    $dy = ($rcWindow.Bottom - $rcWindow.Top) - $rcClient.Bottom

    Write-Verbose "Moving window" 
    [Win32]::MoveWindow($MainWindowHandle, $rct.Left, $rct.Top, $width + $dx, $height + $dy, $true ) | out-null
} #close Whatif


} #process

End {
    Write-Verbose "Ending $($MyInvocation.Mycommand)"
} #end

} #end Set-WindowSize function

该代码支持 -WhatIf 并默认为当前应用程序,大概是 PowerShell ISE。

[玩转系统] 周五乐趣:衡量我的大小

但这是实际设定的。

[玩转系统] 周五乐趣:衡量我的大小

因此,如果您想包含标题栏,则需要进行相应调整。

所有这些可能并不真正适用于您的工作,但如果您发现好的用途,我希望您能让我知道。周末愉快。

您需要 登录账户 后才能发表评论

取消回复欢迎 发表评论:

关灯