gpt4 book ai didi

powershell - 以像素为单位的文本宽度(Powershell GUI)

转载 作者:行者123 更新时间:2023-12-02 23:34:20 25 4
gpt4 key购买 nike

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語法。'

非常感谢!

最佳答案

感谢 Mclayton 的提示,我找到了答案 (link) 并将其写给需要它的人。

$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

关于powershell - 以像素为单位的文本宽度(Powershell GUI),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59518030/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com