簡體   English   中英

以像素為單位的文本寬度(Powershell GUI)

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

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 的提示,我找到了答案( 鏈接)並將其寫給需要它的人。

$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