-
Notifications
You must be signed in to change notification settings - Fork 15
/
powershell-gui.ps1
129 lines (99 loc) · 3.18 KB
/
powershell-gui.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
### Sample showing a PowerShell GUI with drag-and-drop ###
[void] [System.Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms")
[void] [System.Reflection.Assembly]::LoadWithPartialName("System.Drawing")
### Create form ###
$form = New-Object System.Windows.Forms.Form
$form.Text = "PowerShell GUI"
$form.Size = '260,320'
$form.StartPosition = "CenterScreen"
$form.MinimumSize = $form.Size
$form.MaximizeBox = $False
$form.Topmost = $True
### Define controls ###
$button = New-Object System.Windows.Forms.Button
$button.Location = '5,5'
$button.Size = '75,23'
$button.Width = 120
$button.Text = "Print list to console"
$checkbox = New-Object Windows.Forms.Checkbox
$checkbox.Location = '140,8'
$checkbox.AutoSize = $True
$checkbox.Text = "Clear afterwards"
$label = New-Object Windows.Forms.Label
$label.Location = '5,40'
$label.AutoSize = $True
$label.Text = "Drop files or folders here:"
$listBox = New-Object Windows.Forms.ListBox
$listBox.Location = '5,60'
$listBox.Height = 200
$listBox.Width = 240
$listBox.Anchor = ([System.Windows.Forms.AnchorStyles]::Bottom -bor [System.Windows.Forms.AnchorStyles]::Left -bor [System.Windows.Forms.AnchorStyles]::Right -bor [System.Windows.Forms.AnchorStyles]::Top)
$listBox.IntegralHeight = $False
$listBox.AllowDrop = $True
$statusBar = New-Object System.Windows.Forms.StatusBar
$statusBar.Text = "Ready"
### Add controls to form ###
$form.SuspendLayout()
$form.Controls.Add($button)
$form.Controls.Add($checkbox)
$form.Controls.Add($label)
$form.Controls.Add($listBox)
$form.Controls.Add($statusBar)
$form.ResumeLayout()
### Write event handlers ###
$button_Click = {
write-host "Listbox contains:" -ForegroundColor Yellow
foreach ($item in $listBox.Items)
{
$i = Get-Item -LiteralPath $item
if($i -is [System.IO.DirectoryInfo])
{
write-host ("`t" + $i.Name + " [Directory]")
}
else
{
write-host ("`t" + $i.Name + " [" + [math]::round($i.Length/1MB, 2) + " MB]")
}
}
if($checkbox.Checked -eq $True)
{
$listBox.Items.Clear()
}
$statusBar.Text = ("List contains $($listBox.Items.Count) items")
}
$listBox_DragOver = [System.Windows.Forms.DragEventHandler]{
if ($_.Data.GetDataPresent([Windows.Forms.DataFormats]::FileDrop)) # $_ = [System.Windows.Forms.DragEventArgs]
{
$_.Effect = 'Copy'
}
else
{
$_.Effect = 'None'
}
}
$listBox_DragDrop = [System.Windows.Forms.DragEventHandler]{
foreach ($filename in $_.Data.GetData([Windows.Forms.DataFormats]::FileDrop)) # $_ = [System.Windows.Forms.DragEventArgs]
{
$listBox.Items.Add($filename)
}
$statusBar.Text = ("List contains $($listBox.Items.Count) items")
}
$form_FormClosed = {
try
{
$listBox.remove_Click($button_Click)
$listBox.remove_DragOver($listBox_DragOver)
$listBox.remove_DragDrop($listBox_DragDrop)
$listBox.remove_DragDrop($listBox_DragDrop)
$form.remove_FormClosed($Form_Cleanup_FormClosed)
}
catch [Exception]
{ }
}
### Wire up events ###
$button.Add_Click($button_Click)
$listBox.Add_DragOver($listBox_DragOver)
$listBox.Add_DragDrop($listBox_DragDrop)
$form.Add_FormClosed($form_FormClosed)
#### Show form ###
[void] $form.ShowDialog()