Продолжение той самой задачи - нужно налепить форму ввода текста. Уроков нашлось немало, вот один пример:
#https://lazyadmin.nl/powershell/powershell-gui-howto-get-started/
# Init PowerShell Gui
Add-Type -AssemblyName System.Windows.Forms
# Create a new form
$form = New-Object system.Windows.Forms.Form
# Define the size, title and background color
$form.ClientSize = '500,300'
$form.text = "LazyAdmin - PowerShell GUI Example"
$form.BackColor = "#ffffff"
### Inserting the text box that will accept input
$textBox = New-Object System.Windows.Forms.TextBox
$textBox.Location = New-Object System.Drawing.Point(0,0) ### Location of the text box
$textBox.Size = New-Object System.Drawing.Size(500,20) ### Size of the text box
#$textBox.Multiline = $true ### Allows multiple lines of data
$textbox.AcceptsReturn = $true ### By hitting enter it creates a new line
#$textBox.ScrollBars = "Vertical" ### Allows for a vertical scroll bar if the list of text is too big for the window
$form.Controls.Add($textBox)
# Display the form
[void]$form.ShowDialog()
# Init PowerShell Gui
Add-Type -AssemblyName System.Windows.Forms
# Create a new form
$form = New-Object system.Windows.Forms.Form
# Define the size, title and background color
$form.ClientSize = '500,300'
$form.text = "LazyAdmin - PowerShell GUI Example"
$form.BackColor = "#ffffff"
### Inserting the text box that will accept input
$textBox = New-Object System.Windows.Forms.TextBox
$textBox.Location = New-Object System.Drawing.Point(0,0) ### Location of the text box
$textBox.Size = New-Object System.Drawing.Size(500,20) ### Size of the text box
#$textBox.Multiline = $true ### Allows multiple lines of data
$textbox.AcceptsReturn = $true ### By hitting enter it creates a new line
#$textBox.ScrollBars = "Vertical" ### Allows for a vertical scroll bar if the list of text is too big for the window
$form.Controls.Add($textBox)
# Display the form
[void]$form.ShowDialog()
Форма запустилась, продолжились мучения - обработчик событий не заработал. Почему - не знаю.
До $form.Controls.Add($textBox) добавлено
function ms
{
Write-Host $textbox.text
}
$textBox.Add_TextChanged({ms})
Add_TextChanged - это и есть обработка события смены текста. Теперь осталось добавить вторую форму ввода текста и исправить функцию ms :
#https://lazyadmin.nl/powershell/powershell-gui-howto-get-started/
# Init PowerShell Gui
Add-Type -AssemblyName System.Windows.Forms
# Create a new form
$form = New-Object system.Windows.Forms.Form
# Define the size, title and background color
$form.ClientSize = '500,50'
$form.text = "LazyAdmin - PowerShell GUI Example"
$form.BackColor = "#ffffff"
#
#tb2
$textBox2 = New-Object System.Windows.Forms.TextBox
$textBox2.Location = New-Object System.Drawing.Point(0,24) ### Location of the text box
$textBox2.Size = New-Object System.Drawing.Size(500,20) ### Size of the text box
$textbox2.AcceptsReturn = $true ### By hitting enter it creates a new line
#
### Inserting the text box that will accept input
$textBox = New-Object System.Windows.Forms.TextBox
$textBox.Location = New-Object System.Drawing.Point(0,0) ### Location of the text box
$textBox.Size = New-Object System.Drawing.Size(500,20) ### Size of the text box
#$textBox.Multiline = $true ### Allows multiple lines of data
$textbox.AcceptsReturn = $true ### By hitting enter it creates a new line
#$textBox.ScrollBars = "Vertical" ### Allows for a vertical scroll bar if the list of text is too big for the window
function ms
{
$w =':<>?*|"\/'
$f=$textbox.text
$i=0
while($i -lt $f.length)
{
$p=$w.indexof($f.chars($i))
if ($p -ge 0)
{
$f=$f.remove($i,1)
}
else
{
$i=$i+1
}
}
$textbox2.text=$f
}
$textBox.Add_TextChanged({ms})
$form.Controls.Add($textBox)
$form.Controls.Add($textBox2)
# Display the form
[void]$form.ShowDialog()
# Init PowerShell Gui
Add-Type -AssemblyName System.Windows.Forms
# Create a new form
$form = New-Object system.Windows.Forms.Form
# Define the size, title and background color
$form.ClientSize = '500,50'
$form.text = "LazyAdmin - PowerShell GUI Example"
$form.BackColor = "#ffffff"
#
#tb2
$textBox2 = New-Object System.Windows.Forms.TextBox
$textBox2.Location = New-Object System.Drawing.Point(0,24) ### Location of the text box
$textBox2.Size = New-Object System.Drawing.Size(500,20) ### Size of the text box
$textbox2.AcceptsReturn = $true ### By hitting enter it creates a new line
#
### Inserting the text box that will accept input
$textBox = New-Object System.Windows.Forms.TextBox
$textBox.Location = New-Object System.Drawing.Point(0,0) ### Location of the text box
$textBox.Size = New-Object System.Drawing.Size(500,20) ### Size of the text box
#$textBox.Multiline = $true ### Allows multiple lines of data
$textbox.AcceptsReturn = $true ### By hitting enter it creates a new line
#$textBox.ScrollBars = "Vertical" ### Allows for a vertical scroll bar if the list of text is too big for the window
function ms
{
$w =':<>?*|"\/'
$f=$textbox.text
$i=0
while($i -lt $f.length)
{
$p=$w.indexof($f.chars($i))
if ($p -ge 0)
{
$f=$f.remove($i,1)
}
else
{
$i=$i+1
}
}
$textbox2.text=$f
}
$textBox.Add_TextChanged({ms})
$form.Controls.Add($textBox)
$form.Controls.Add($textBox2)
# Display the form
[void]$form.ShowDialog()
А на сладкое осталось рисование
Дополнение. Вечером решил повторить выод по нажатию клавиши Escape - не прокатило. А в win32 для диалогового окна нажатия Enter и Escape обрабатываются легко.
Комментарии
Отправить комментарий