簡體   English   中英

如何在PowerShell中更改列表框的字體大小

[英]How do I change the font size of a List box in PowerShell

我正在編寫一些PowerShell代碼以用於以后的項目。 這是一個列表,用戶可以從中選擇一個項目,並將選擇項分配給一個變量。 我不確定如何控制字體大小,特別是對於列表框文本。

這是代碼:

# Creates a window that prompts a user to select an item from a list

#Enables .NET Framework Classes
Add-Type -AssemblyName System.Windows.Forms
Add-Type -AssemblyName System.Drawing


# Creates the window prompt
$objForm = New-Object System.Windows.Forms.Form
$objForm.Text = "Select an Item"
$objForm.Size = New-Object System.Drawing.Size(600,500)
$objForm.StartPosition = "CenterScreen"

# Defines keystrokes as inputs
#
# Sets Enter to set highlighted item to a variable
# Sets Esc to close windowed prompt
#
$objForm.KeyPreview = $True
$objForm.Add_KeyDown({if ($_.KeyCode -eq "Enter")
    {$x=$objListBox.SelectedItem;$objForm.Close()}})
$objForm.Add_KeyDown({if ($_.KeyCode -eq "Escape")
    {$objForm.Close()}})

# Creates the OK button for the window
$OKButton = New-Object System.Windows.Forms.Button
$OKButton.Location = New-Object System.Drawing.Size(75,300)
$OKButton.Size = New-Object System.Drawing.Size(100,35)
$OKButton.Text = "OK"
$OKButton.Add_Click({$x=$objListBox.SelectedItem;$ObjForm.Close()})
$objForm.Controls.Add($OKButton)

# Creates the cancel button for the window
$CancelButton = New-Object System.Windows.Forms.Button
$CancelButton.Location = New-Object System.Drawing.Size(200,300)
$CancelButton.Size = New-Object System.Drawing.Size(100,35)
$CancelButton.Text = "Cancel"
$CancelButton.Add_Click({$objForm.Close()})
$objForm.Controls.Add($CancelButton)

# Adds the label text
$objLabel = New-Object System.Windows.Forms.Label
$objLabel.Location = New-Object System.Drawing.Size(10,40)
$objLabel.Size = New-Object System.Drawing.Size(400,50)
$objLabel.Text = "Please Select an Item"
$objForm.Controls.Add($ObjLabel)

# Creates the empty List box
$objListBox = New-Object System.Windows.Forms.ListBox
$objListBox.Location = New-Object System.Drawing.Size(10,100)
$objListBox.Size = New-Object System.Drawing.Size(500,300)
$objListBox.Height = 200


# Adds items to the list box
# Can call items from file
#
#    Example : Get-Content C:\Scripts\Test.txt | ForEach-Object {[void] $objListBox.Items.Add($_)}
#
[void] $objListBox.Items.Add("one")
[void] $objListBox.Items.Add("two")
[void] $objListBox.Items.Add("three")
[void] $objListBox.Items.Add("four")
[void] $objListBox.Items.Add("five")

$objForm.Controls.Add($objListBox)

$objForm.Topmost = $True

$objForm.Add_Shown({$objForm.Activate()})
[void] $objForm.ShowDialog()

$x

這是ListBox類的MSDN。 有一個名為Font的屬性。 在字體的MSDN頁面上,可以看到所有構造函數或制作字體對象的方法。 在此示例中,這是我使用的那個。

#Creates the empty List box
$objListBox = New-Object System.Windows.Forms.ListBox
$objListBox.Location = New-Object System.Drawing.Size(10,100)
$objListBox.Size = New-Object System.Drawing.Size(500,300)
$objListBox.Height = 200
$objListBox.Font = New-Object System.Drawing.Font("Lucida Console",12,[System.Drawing.FontStyle]::Regular)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM