简体   繁体   English

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

[英]Width of the text in pixels (Powershell GUI)

Does Powershell have a special method for determining the length of a string in pixels (in this case, the width of the text is determined automatically using ".AutoSize = $True")? Powershell 是否有一种特殊的方法来确定以像素为单位的字符串长度(在这种情况下,文本的宽度是使用“.AutoSize = $True”自动确定的)? Or should I know the width of one character and then multiply by the length of the string?或者我应该知道一个字符的宽度,然后乘以字符串的长度? For example, the text has the font Arial 14px.例如,文本的字体为 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!'

Ideally, if there is a method similar to this:理想情况下,如果有类似这样的方法:

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

PS> 371

And what if it is for example Chinese?如果是例如中文呢?

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

Many thanks!非常感谢!

Thanks to Mclayton's hints, I found the answer ( link ) and write it for those who need it.感谢 Mclayton 的提示,我找到了答案( 链接)并将其写给需要它的人。

$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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM