summaryrefslogtreecommitdiff
path: root/misc/hooks/winmessage.ps1
blob: 3672579544e246a4dcc5fd0665ebd909bf1ca4d8 (plain)
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
Param (
	[string]$file = "",
	[string]$text = "",
	[string]$buttons = "OK:0",
	[string]$default = "",
	[switch]$nearmouse = $false,
	[switch]$center = $false,
	[string]$geometry = "",
	[int32]$timeout = 0,
	[string]$title = "Message"
)
Add-Type -assembly System.Windows.Forms

$global:Result = 0

$main_form = New-Object System.Windows.Forms.Form
$main_form.Text = $title

$geometry_data = $geometry.Split("+")
if ($geometry_data.Length -ge 1) {
	$size_data = $geometry_data[0].Split("x")
	if ($size_data.Length -eq 2) {
		$main_form.Width = $size_data[0]
		$main_form.Height = $size_data[1]
	}
}
if ($geometry_data.Length -eq 3) {
	$main_form.StartPosition = [System.Windows.Forms.FormStartPosition]::Manual
	$main_form.Location = New-Object System.Drawing.Point($geometry_data[1], $geometry_data[2])
}
if ($nearmouse) {
	$main_form.StartPosition = [System.Windows.Forms.FormStartPosition]::Manual
	$main_form.Location = System.Windows.Forms.Cursor.Position
}
if ($center) {
	$main_form.StartPosition = [System.Windows.Forms.FormStartPosition]::CenterScreen
}

$main_form.SuspendLayout()

$button_panel = New-Object System.Windows.Forms.FlowLayoutPanel
$button_panel.SuspendLayout()
$button_panel.FlowDirection = [System.Windows.Forms.FlowDirection]::RightToLeft
$button_panel.Dock = [System.Windows.Forms.DockStyle]::Bottom
$button_panel.Autosize = $true

if ($file -ne "") {
	$text = [IO.File]::ReadAllText($file).replace("`n", "`r`n")
}

if ($text -ne "") {
	$text_box = New-Object System.Windows.Forms.TextBox
	$text_box.Multiline = $true
	$text_box.ReadOnly = $true
	$text_box.Autosize = $true
	$text_box.Text = $text
	$text_box.Select(0,0)
	$text_box.Dock = [System.Windows.Forms.DockStyle]::Fill
	$main_form.Controls.Add($text_box)
}

$buttons_array = $buttons.Split(",")
foreach ($button in $buttons_array) {
	$button_data = $button.Split(":")
	$button_ctl = New-Object System.Windows.Forms.Button
	if ($button_data.Length -eq 2) {
		$button_ctl.Tag = $button_data[1]
	} else {
		$button_ctl.Tag = 100 + $buttons_array.IndexOf($button)
	}
	if ($default -eq $button_data[0]) {
		$main_form.AcceptButton = $button_ctl
	}
	$button_ctl.Autosize = $true
	$button_ctl.Text = $button_data[0]
	$button_ctl.Add_Click(
		{
			Param($sender)
			$global:Result = $sender.Tag
			$main_form.Close()
		}
	)
	$button_panel.Controls.Add($button_ctl)
}
$main_form.Controls.Add($button_panel)

$button_panel.ResumeLayout($false)
$main_form.ResumeLayout($false)

if ($timeout -gt 0) {
	$timer = New-Object System.Windows.Forms.Timer
	$timer.Add_Tick(
		{
			$global:Result = 0
			$main_form.Close()
		}
	)
	$timer.Interval = $timeout
	$timer.Start()
}
$dlg_res = $main_form.ShowDialog()

[Environment]::Exit($global:Result)