文本的宽度(像素)(Powershell GUI)

文本的宽度(像素)(Powershell GUI),powershell,Powershell,Powershell是否有一种特殊的方法来确定以像素为单位的字符串长度(在本例中,文本的宽度是使用“.AutoSize=$True”自动确定的)?或者我应该知道一个字符的宽度,然后乘以字符串的长度?例如,文本的字体为Arial 14px $StackOverflow = New-Object System.Windows.Forms.Label $StackOverflow.AutoSize = $True $StackOverflow.Font = 'Arial, 14px, Style=Re

Powershell是否有一种特殊的方法来确定以像素为单位的字符串长度(在本例中,文本的宽度是使用“.AutoSize=$True”自动确定的)?或者我应该知道一个字符的宽度,然后乘以字符串的长度?例如,文本的字体为Arial 14px

$StackOverflow = New-Object System.Windows.Forms.Label
$StackOverflow.AutoSize = $True
$StackOverflow.Font = 'Arial, 14px, Style=Regular'
$StackOverflow.Text = 'Thank you for helping me learn the Powershell syntax!'
理想情况下,如果有类似的方法:

$StackOverflow.TextWidth() # Width of the text in pixels

PS> 371
如果是中国人呢

$StackOverflow.Text = '感謝您幫助我學習Powershell語法。'

非常感谢

多亏了麦克莱顿的提示,我找到了答案()并为那些需要的人写了它

$String = 'Thank you for helping me learn the Powershell syntax!'
$Font = New-Object System.Drawing.Font('Arial', 14)
$Size = [System.Windows.Forms.TextRenderer]::MeasureText($String, $Font)

$Size.Width

net框架有一个“MeasureString”方法可用于此,您可以通过一些诱使从PowerShell调用.net方法。。。