Note – I have updated this script, please see FontDialog2.ps1 for the changes which display the selected Font and Style(s).

___________________________________________________________________________________________________________________________________

On occasion you may need to interactively select a particular font from the installed fonts list. The System.Windows.Forms.FontDialog class offers a nice selection dialog. You can select the font family, size, bold, italic, script (character set) and effects while showing a font sample.

FontDialog2

I put together a demo script to show the font properties you can gather while selecting a font. Just click Select Font and define your needs, click OK, and the results are displayed. You can also gather the current font info by clicking Select Font and then canceling the FontDialog.

I defined a WinForms form and the various Labels and TextBoxes.

Add-Type -AssemblyName System.Windows.Forms

# Main Form
$mainForm = New-Object System.Windows.Forms.Form
$mainForm.Font = "Comic Sans MS,8.25"
$mainForm.Text = " FontDialog Demo"
$mainForm.ForeColor = "White"
$mainForm.BackColor = "DarkSlateBlue"
$mainForm.Width = 400
$mainForm.Height = 500

# Labels
$fontNameLabel = New-Object System.Windows.Forms.Label
$fontNameLabel.Font = "Comic Sans MS,10"
$fontNameLabel.Location = "20,20"
$fontNameLabel.Size = "120,20"
$fontNameLabel.Text = "Font Family"
$mainForm.Controls.Add($fontNameLabel)

# TextBoxes
$fontNameTextBox = New-Object System.Windows.Forms.TextBox
$fontNameTextBox.Location = "140,20"
$fontNameTextBox.Size = "200,20"
$fontNameTextBox.Text = ""
$mainForm.Controls.Add($fontNameTextBox)

When you click the “Select Font” button I display the FontDialog window. After the FontDialog control completes, I gather all the returned properties from the $fontDialog.font array and display those values in the TextBoxes.

# Select Button
$fontSelectButton = New-Object System.Windows.Forms.Button
$fontSelectButton.ForeColor = "DarkSlateBlue"
$fontSelectButton.BackColor = "White"
$fontSelectButton.Location = "20, 400"
$fontSelectButton.Size = "75, 23"
$fontSelectButton.Text = "Select Font"
$fontSelectButton.add_Click({
    $fontDialog = New-Object System.Windows.Forms.FontDialog
    [void] $fontDialog.ShowDialog()
    $font = $fontDialog.Font
    $fontNameTextBox.Text = $font.Name
    $fontSizeTextBox.Text = $font.Size
    $fontHeightTextBox.Text = $font.Height
    $fontUnitTextBox.Text = $font.Unit
    $fontBoldTextBox.Text = $font.Bold
    $fontItalicTextBox.Text = $font.Italic
    $fontStyleTextBox.Text = $font.Style
    $fontStrikeoutTextBox.Text = $font.Strikeout
    $fontUnderlineTextBox.Text = $font.Underline
    $fontIsSystemFontTextBox.Text = $font.IsSystemFont
    $fontGdiCharSetTextBox.Text = $font.GDICharSet
    $fontGdiVerticalFontTextBox.Text = $font.GDIVerticalFont
    })
$mainForm.Controls.Add($fontSelectButton)

I added the Exit Button and display the form.

# Exit Button
$exitButton = New-Object System.Windows.Forms.Button
$exitButton.Location = "20,430"
$exitButton.Size = "75,23"
$exitButton.ForeColor = "Red"
$exitButton.BackColor = "White"
$exitButton.Text = "Exit"
$exitButton.add_Click({$mainForm.close()})
$mainForm.Controls.Add($exitButton)

[void] $mainForm.ShowDialog()

FontDialog

FontDialog.ps1

Advertisements