winmessage.ps1 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. Param (
  2. [string]$file = "",
  3. [string]$text = "",
  4. [string]$buttons = "OK:0",
  5. [string]$default = "",
  6. [switch]$nearmouse = $false,
  7. [switch]$center = $false,
  8. [string]$geometry = "",
  9. [int32]$timeout = 0,
  10. [string]$title = "Message"
  11. )
  12. Add-Type -assembly System.Windows.Forms
  13. $global:Result = 0
  14. $main_form = New-Object System.Windows.Forms.Form
  15. $main_form.Text = $title
  16. $geometry_data = $geometry.Split("+")
  17. if ($geometry_data.Length -ge 1) {
  18. $size_data = $geometry_data[0].Split("x")
  19. if ($size_data.Length -eq 2) {
  20. $main_form.Width = $size_data[0]
  21. $main_form.Height = $size_data[1]
  22. }
  23. }
  24. if ($geometry_data.Length -eq 3) {
  25. $main_form.StartPosition = [System.Windows.Forms.FormStartPosition]::Manual
  26. $main_form.Location = New-Object System.Drawing.Point($geometry_data[1], $geometry_data[2])
  27. }
  28. if ($nearmouse) {
  29. $main_form.StartPosition = [System.Windows.Forms.FormStartPosition]::Manual
  30. $main_form.Location = System.Windows.Forms.Cursor.Position
  31. }
  32. if ($center) {
  33. $main_form.StartPosition = [System.Windows.Forms.FormStartPosition]::CenterScreen
  34. }
  35. $main_form.SuspendLayout()
  36. $button_panel = New-Object System.Windows.Forms.FlowLayoutPanel
  37. $button_panel.SuspendLayout()
  38. $button_panel.FlowDirection = [System.Windows.Forms.FlowDirection]::RightToLeft
  39. $button_panel.Dock = [System.Windows.Forms.DockStyle]::Bottom
  40. $button_panel.Autosize = $true
  41. if ($file -ne "") {
  42. $text = [IO.File]::ReadAllText($file).replace("`n", "`r`n")
  43. }
  44. if ($text -ne "") {
  45. $text_box = New-Object System.Windows.Forms.TextBox
  46. $text_box.Multiline = $true
  47. $text_box.ReadOnly = $true
  48. $text_box.Autosize = $true
  49. $text_box.Text = $text
  50. $text_box.Select(0,0)
  51. $text_box.Dock = [System.Windows.Forms.DockStyle]::Fill
  52. $main_form.Controls.Add($text_box)
  53. }
  54. $buttons_array = $buttons.Split(",")
  55. foreach ($button in $buttons_array) {
  56. $button_data = $button.Split(":")
  57. $button_ctl = New-Object System.Windows.Forms.Button
  58. if ($button_data.Length -eq 2) {
  59. $button_ctl.Tag = $button_data[1]
  60. } else {
  61. $button_ctl.Tag = 100 + $buttons_array.IndexOf($button)
  62. }
  63. if ($default -eq $button_data[0]) {
  64. $main_form.AcceptButton = $button_ctl
  65. }
  66. $button_ctl.Autosize = $true
  67. $button_ctl.Text = $button_data[0]
  68. $button_ctl.Add_Click(
  69. {
  70. Param($sender)
  71. $global:Result = $sender.Tag
  72. $main_form.Close()
  73. }
  74. )
  75. $button_panel.Controls.Add($button_ctl)
  76. }
  77. $main_form.Controls.Add($button_panel)
  78. $button_panel.ResumeLayout($false)
  79. $main_form.ResumeLayout($false)
  80. if ($timeout -gt 0) {
  81. $timer = New-Object System.Windows.Forms.Timer
  82. $timer.Add_Tick(
  83. {
  84. $global:Result = 0
  85. $main_form.Close()
  86. }
  87. )
  88. $timer.Interval = $timeout
  89. $timer.Start()
  90. }
  91. $dlg_res = $main_form.ShowDialog()
  92. [Environment]::Exit($global:Result)