[玩转系统] 周五乐趣:衡量我的大小
作者:精品下载站 日期: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。
但这是实际设定的。
因此,如果您想包含标题栏,则需要进行相应调整。
所有这些可能并不真正适用于您的工作,但如果您发现好的用途,我希望您能让我知道。周末愉快。
猜你还喜欢
- 03-30 [玩转系统] 如何用批处理实现关机,注销,重启和锁定计算机
- 02-14 [系统故障] Win10下报错:该文件没有与之关联的应用来执行该操作
- 01-07 [系统问题] Win10--解决锁屏后会断网的问题
- 01-02 [系统技巧] Windows系统如何关闭防火墙保姆式教程,超详细
- 12-15 [玩转系统] 如何在 Windows 10 和 11 上允许多个 RDP 会话
- 12-15 [玩转系统] 查找 Exchange/Microsoft 365 中不活动(未使用)的通讯组列表
- 12-15 [玩转系统] 如何在 Windows 上安装远程服务器管理工具 (RSAT)
- 12-15 [玩转系统] 如何在 Windows 上重置组策略设置
- 12-15 [玩转系统] 如何获取计算机上的本地管理员列表?
- 12-15 [玩转系统] 在 Visual Studio Code 中连接到 MS SQL Server 数据库
- 12-15 [玩转系统] 如何降级 Windows Server 版本或许可证
- 12-15 [玩转系统] 如何允许非管理员用户在 Windows 中启动/停止服务
取消回复欢迎 你 发表评论:
- 精品推荐!
-
- 最新文章
- 热门文章
- 热评文章
[影视] 黑道中人 Alto Knights(2025)剧情 犯罪 历史 电影
[古装剧] [七侠五义][全75集][WEB-MP4/76G][国语无字][1080P][焦恩俊经典]
[实用软件] 虚拟手机号 电话 验证码 注册
[电视剧] 安眠书店/你 第五季 You Season 5 (2025) 【全10集】
[电视剧] 棋士(2025) 4K 1080P【全22集】悬疑 犯罪 王宝强 陈明昊
[软件合集] 25年6月5日 精选软件22个
[软件合集] 25年6月4日 精选软件36个
[短剧] 2025年06月04日 精选+付费短剧推荐33部
[短剧] 2025年06月03日 精选+付费短剧推荐25部
[软件合集] 25年6月3日 精选软件44个
[剧集] [央视][笑傲江湖][2001][DVD-RMVB][高清][40集全]李亚鹏、许晴、苗乙乙
[电视剧] 欢乐颂.5部全 (2016-2024)
[电视剧] [突围] [45集全] [WEB-MP4/每集1.5GB] [国语/内嵌中文字幕] [4K-2160P] [无水印]
[影视] 【稀有资源】香港老片 艺坛照妖镜之96应召名册 (1996)
[剧集] 神经风云(2023)(完结).4K
[剧集] [BT] [TVB] [黑夜彩虹(2003)] [全21集] [粤语中字] [TV-RMVB]
[实用软件] 虚拟手机号 电话 验证码 注册
[资源] B站充电视频合集,包含多位重量级up主,全是大佬真金白银买来的~【99GB】
[影视] 内地绝版高清录像带 [mpg]
[书籍] 古今奇书禁书三教九流资料大合集 猎奇必备珍藏资源PDF版 1.14G
[电视剧] [突围] [45集全] [WEB-MP4/每集1.5GB] [国语/内嵌中文字幕] [4K-2160P] [无水印]
[剧集] [央视][笑傲江湖][2001][DVD-RMVB][高清][40集全]李亚鹏、许晴、苗乙乙
[电影] 美国队长4 4K原盘REMUX 杜比视界 内封简繁英双语字幕 49G
[电影] 死神来了(1-6)大合集!
[软件合集] 25年05月13日 精选软件16个
[精品软件] 25年05月15日 精选软件18个
[绝版资源] 南与北 第1-2季 合集 North and South (1985) /美国/豆瓣: 8.8[1080P][中文字幕]
[软件] 25年05月14日 精选软件57个
[短剧] 2025年05月14日 精选+付费短剧推荐39部
[短剧] 2025年05月15日 精选+付费短剧推荐36部
- 最新评论
-
- 热门tag