Functions/New-FontPicker.ps1

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
function New-FontPicker {
    <#
.SYNOPSIS
    Present a dialog to the user and allow them to select a font and its characteristics
.DESCRIPTION
    Present a dialog to the user and allow them to select a font and its characteristics
.EXAMPLE
    New-FontPicker
 
    Will present the user with a font dialog and return the following if Arial 18 point italic was selected:
 
    FontFamily : [FontFamily: Name=Arial]
    Bold : False
    GdiCharSet : 0
    GdiVerticalFont : False
    Italic : True
    Name : Arial
    OriginalFontName :
    Strikeout : False
    Underline : False
    Style : Italic
    Size : 18
    SizeInPoints : 18
    Unit : Point
    Height : 28
    IsSystemFont : False
    SystemFontName :
.NOTES
    If the user selects 'Cancel' from the dialog box no value is returned.
#>


    [CmdletBinding()]
    [Diagnostics.CodeAnalysis.SuppressMessageAttribute('PSUseShouldProcessForStateChangingFunctions', '')]
    param (

    )

    begin {
        Write-Verbose -Message "Starting [$($MyInvocation.Mycommand)]"
        #~~< FontPicker >~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
        $FontPicker = New-Object -TypeName System.Windows.Forms.Form
        $FontPicker.ClientSize = New-Object -TypeName System.Drawing.Size -ArgumentList (496, 204)
        $FontPicker.Font = New-Object -TypeName System.Drawing.Font -ArgumentList ('Segoe UI', 10.0, [System.Drawing.FontStyle]::Regular, [System.Drawing.GraphicsUnit]::Point, ([byte](0)))
        $FontPicker.Text = 'Font Picker'
        $FontPicker.AutoSize = $true
        $FontPicker.AutoSizeMode = 'GrowOnly'
        $FontPicker.Icon = (Join-Path -Path $Script:ModulePath -ChildPath 'Resources\PoshFunctions.ico')
        #~~< OKButton >~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
        $OKButton = New-Object -TypeName System.Windows.Forms.Button
        $OKButton.DialogResult = [System.Windows.Forms.DialogResult]::OK
        $OKButton.Location = New-Object -TypeName System.Drawing.Point -ArgumentList (389, 69)
        $OKButton.Size = New-Object -TypeName System.Drawing.Size -ArgumentList (95, 23)
        $OKButton.TabIndex = 1
        $OKButton.Text = 'OK'
        $OKButton.UseVisualStyleBackColor = $true
        $FontPicker.AcceptButton = $OKButton
        #~~< CancelButton >~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
        $CancelButton = New-Object -TypeName System.Windows.Forms.Button
        $CancelButton.DialogResult = [System.Windows.Forms.DialogResult]::Cancel
        $CancelButton.Location = New-Object -TypeName System.Drawing.Point -ArgumentList (389, 125)
        $CancelButton.Size = New-Object -TypeName System.Drawing.Size -ArgumentList (95, 23)
        $CancelButton.TabIndex = 2
        $CancelButton.Text = 'Cancel'
        $CancelButton.UseVisualStyleBackColor = $true
        $FontPicker.CancelButton = $CancelButton
        #~~< ItalicsCheck >~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
        $ItalicsCheck = New-Object -TypeName System.Windows.Forms.CheckBox
        $ItalicsCheck.Location = New-Object -TypeName System.Drawing.Point -ArgumentList (261, 153)
        $ItalicsCheck.Size = New-Object -TypeName System.Drawing.Size -ArgumentList (104, 24)
        $ItalicsCheck.TabIndex = 10
        $ItalicsCheck.Text = 'Italic'
        $ItalicsCheck.Enabled = $false
        $ItalicsCheck.UseVisualStyleBackColor = $true
        #~~< BoldCheck >~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
        $BoldCheck = New-Object -TypeName System.Windows.Forms.CheckBox
        $BoldCheck.Location = New-Object -TypeName System.Drawing.Point -ArgumentList (261, 100)
        $BoldCheck.Size = New-Object -TypeName System.Drawing.Size -ArgumentList (104, 24)
        $BoldCheck.TabIndex = 9
        $BoldCheck.Text = 'Bold'
        $BoldCheck.Enabled = $false
        $BoldCheck.UseVisualStyleBackColor = $true
        #~~< StyleText >~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
        $StyleText = New-Object -TypeName System.Windows.Forms.TextBox
        $StyleText.Enabled = $false
        $StyleText.Location = New-Object -TypeName System.Drawing.Point -ArgumentList (13, 156)
        $StyleText.Size = New-Object -TypeName System.Drawing.Size -ArgumentList (161, 21)
        $StyleText.TabIndex = 8
        $StyleText.TabStop = $false
        $StyleText.Text = ''
        #~~< StyleLabel >~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
        $StyleLabel = New-Object -TypeName System.Windows.Forms.Label
        $StyleLabel.Location = New-Object -TypeName System.Drawing.Point -ArgumentList (12, 130)
        $StyleLabel.Size = New-Object -TypeName System.Drawing.Size -ArgumentList (100, 23)
        $StyleLabel.TabIndex = 7
        $StyleLabel.Text = 'Style'
        #~~< SizeText >~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
        $SizeText = New-Object -TypeName System.Windows.Forms.TextBox
        $SizeText.Enabled = $false
        $SizeText.Location = New-Object -TypeName System.Drawing.Point -ArgumentList (12, 100)
        $SizeText.Size = New-Object -TypeName System.Drawing.Size -ArgumentList (60, 21)
        $SizeText.TabIndex = 6
        $SizeText.TabStop = $false
        $SizeText.Text = ''
        #~~< SizeLabel >~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
        $SizeLabel = New-Object -TypeName System.Windows.Forms.Label
        $SizeLabel.Location = New-Object -TypeName System.Drawing.Point -ArgumentList (12, 74)
        $SizeLabel.Size = New-Object -TypeName System.Drawing.Size -ArgumentList (100, 23)
        $SizeLabel.TabIndex = 5
        $SizeLabel.Text = 'Font size'
        #~~< NameText >~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
        $NameText = New-Object -TypeName System.Windows.Forms.TextBox
        $NameText.Enabled = $false
        $NameText.Location = New-Object -TypeName System.Drawing.Point -ArgumentList (13, 42)
        $NameText.Size = New-Object -TypeName System.Drawing.Size -ArgumentList (312, 21)
        $NameText.TabIndex = 4
        $NameText.TabStop = $false
        $NameText.Text = ''
        #~~< NameLabel >~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
        $NameLabel = New-Object -TypeName System.Windows.Forms.Label
        $NameLabel.Location = New-Object -TypeName System.Drawing.Point -ArgumentList (13, 25)
        $NameLabel.Size = New-Object -TypeName System.Drawing.Size -ArgumentList (100, 23)
        $NameLabel.TabIndex = 3
        $NameLabel.Text = 'Font name'
        #~~< FontButon >~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
        $FontButon = New-Object -TypeName System.Windows.Forms.Button
        $FontButon.Location = New-Object -TypeName System.Drawing.Point -ArgumentList (389, 21)
        $FontButon.Size = New-Object -TypeName System.Drawing.Size -ArgumentList (95, 23)
        $FontButon.TabIndex = 0
        $FontButon.Text = 'Font dialog'
        $FontButon.UseVisualStyleBackColor = $true
        $FontButon.add_Click( { FontButonClick -object ($FontButon) })
        $FontPicker.Controls.Add($ItalicsCheck)
        $FontPicker.Controls.Add($BoldCheck)
        $FontPicker.Controls.Add($StyleText)
        $FontPicker.Controls.Add($StyleLabel)
        $FontPicker.Controls.Add($SizeText)
        $FontPicker.Controls.Add($SizeLabel)
        $FontPicker.Controls.Add($NameText)
        $FontPicker.Controls.Add($NameLabel)
        $FontPicker.Controls.Add($CancelButton)
        $FontPicker.Controls.Add($OKButton)
        $FontPicker.Controls.Add($FontButon)
        $FontPicker.add_Load( { FormLoad -object ($FontPicker) })
        #~~< FontDialog >~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
        $FontDialog = New-Object -TypeName System.Windows.Forms.FontDialog
        $FontDialog.ShowHelp = $true

        function FontButonClick {
            [CmdletBinding()]
            [Diagnostics.CodeAnalysis.SuppressMessageAttribute('PSReviewUnusedParameter', '')]
            param
            (
                $object
            )
            $FontResult = $FontDialog.ShowDialog()
            if ($FontResult -eq 'OK') {
                $NameText.Text = $FontDialog.Font.Name
                $SizeText.Text = $FontDialog.Font.Size
                $StyleText.Text = $FontDialog.Font.Style
                $BoldCheck.Checked = $FontDialog.Font.Bold
                $ItalicsCheck.Checked = $FontDialog.Font.Italic
            }
        }
        function FormLoad {
            [CmdletBinding()]
            [Diagnostics.CodeAnalysis.SuppressMessageAttribute('PSReviewUnusedParameter', '')]
            param
            (
                $object
            )
            $NameText.Text = $FontDialog.Font.Name
            $SizeText.Text = $FontDialog.Font.Size
            $StyleText.Text = $FontDialog.Font.Style
            $BoldCheck.Checked = $FontDialog.Font.Bold
            $ItalicsCheck.Checked = $FontDialog.Font.Italic
        }
    }

    process {
        [System.Windows.Forms.Application]::EnableVisualStyles()
        $Result = $FontPicker.ShowDialog()
        if ($Result -eq 'OK') {
            Write-Output -InputObject $FontDialog.Font
        }
    }

    end {
        Write-Verbose -Message "Ending [$($MyInvocation.Mycommand)]"
    }
}