add gui example

This commit is contained in:
onyx-and-iris 2023-11-29 19:21:43 +00:00
parent 9c5cfbdc7b
commit cb7c7b2e04

76
examples/gui.ps1 Normal file
View File

@ -0,0 +1,76 @@
[void] [System.Reflection.Assembly]::LoadWithPartialName("System.Drawing")
[void] [System.Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms")
$OKB = New-Object System.Windows.Forms.Button
$OTB = New-Object System.Windows.Forms.TextBox
$CAB = New-Object System.Windows.Forms.Button
$Lbl = New-Object System.Windows.Forms.Label
Function InitForm {
$form = New-Object System.Windows.Forms.Form
$form.Text = "Q3Rcon Client"
$form.Size = New-Object System.Drawing.Size(300, 200)
$form.StartPosition = "CenterScreen"
return $form
}
Function AddOkButton {
param($form, $rcon)
$OKB.Location = New-Object System.Drawing.Size(65, 100)
$OKB.Size = New-Object System.Drawing.Size(65, 23)
$OKB.Text = "Send"
$OKB.Add_Click({ $rcon.Send($OTB.Text) })
$form.Controls.Add($OKB)
}
Function AddCloseButton($form) {
$CAB.Location = New-Object System.Drawing.Size(140, 100)
$CAB.Size = New-Object System.Drawing.Size(65, 23)
$CAB.Text = "Close"
$CAB.Add_Click({ Write-Host "Disconnecting from Rcon" -ForegroundColor Green; $form.Close() })
$form.Controls.Add($CAB)
}
Function AddLabel($form) {
$Lbl.Location = New-Object System.Drawing.Size(10, 20)
$Lbl.Size = New-Object System.Drawing.Size(260, 20)
$Lbl.Text = "Input Rcon Command:"
$form.Controls.Add($Lbl)
}
Function AddTextBox($form) {
$OTB.Location = New-Object System.Drawing.Size(10, 50)
$OTB.Size = New-Object System.Drawing.Size(240, 20)
$form.Controls.Add($OTB)
}
Function FinalizeForm($form) {
$form.Topmost = $true
$form.Add_Shown({ $form.Activate() })
}
Function Get-ConnFromPSD1 {
$configpath = Join-Path $PSScriptRoot "config.psd1"
return Import-PowerShellDataFile -Path $configpath
}
try {
$conn = Get-ConnFromPSD1
$rcon = Connect-Rcon -hostname $conn.host -port $conn.port -passwd $conn.passwd
Write-Host $rcon.base.ToString() -ForegroundColor Green
$form = InitForm
AddOkButton -form $form -rcon $rcon
AddCloseButton($form)
AddLabel($form)
AddTextBox($form)
FinalizeForm($form)
[void] $form.ShowDialog()
}
finally {
Disconnect-Rcon -rcon $rcon
}