【问题标题】:Powershell GUI tabbed layout script placementPowershell GUI 选项卡式布局脚本放置
【发布时间】:2021-07-08 15:02:08
【问题描述】:

我写了六个小 gui 脚本,我想将它们放在一个文件中,以便将所述文件转换为可使用 ps2exe 执行的 rxecutable。

我在堆栈上找到了一个脚本,here,非常适合我想要的。不幸的是,我在选项卡中找不到有关脚本放置的任何信息,MS 文档将我引导至 ISE 选项卡,这没有帮助。

说我想放这个

Add-Type -AssemblyName PresentationFramework

    $button_click = {
        $folder = $textBox1.Text;
        
          $pytanie = [System.Windows.MessageBox]::Show('Czy chcesz usunac folder?', '', '4');
           If($pytanie -eq 'Yes')
           {Remove-Item –path $folder –recurse -Force};
           $test = Test-Path $folder;
           if ($test -eq $false){[System.Windows.MessageBox]::Show('Folder Usuniety', '', '0')}}
    
    $label2 = New-Object System.Windows.Forms.Label
    $label2.AutoSize = $True
    $label2.Text = ("Scieżka")
    $label2.Location = New-Object System.Drawing.Point (10,30)
    $label2.Size = New-Object System.Drawing.Size (25,70)
    $label2.Font = [System.Drawing.Font]::new("Arial", 10, [System.Drawing.FontStyle]::Bold)
    
    $textBox1 = New-Object System.Windows.Forms.TextBox
    $textBox1.Location = New-Object System.Drawing.Point(10,70) ### Location of the text box
    $textBox1.Size = New-Object System.Drawing.Size(200,50) ### Size of the text box
    $textBox1.Multiline = $false ### Allows multiple lines of data
    $textBox1.Font = New-Object System.Drawing.Font("Consolas",10,[System.Drawing.FontStyle]::Regular)
    $textBox1.ReadOnly=$false
    
    $button = New-Object System.Windows.Forms.Button
    $button.Location = New-Object System.Drawing.Point(10,120)
    $button.Size = New-Object System.Drawing.Size (200,30)
    $button.Text = "Usun Folder"
    $button.Add_Click($button_click)
    
    $form = New-Object System.Windows.Forms.Form
    $form.Text = 'Mapowanie' ### Text to be displayed in the title
    $form.Size = New-Object System.Drawing.Size(240,200) ### Size of the window
    $form.StartPosition = 'Manual'
    $form.Location      = '10,10'
    $form.Topmost = $true  ### Optional - Opens on top of other windows
    
    
    $form.Controls.AddRange(@($textBox1,$button, $label2))
    
    
    
    $form.ShowDialog()

在标签中。怎么做?

【问题讨论】:

  • 您需要添加一个 TabControl 对象,然后添加带有格式、控件等的页面(选项卡)。根据经验,可能值得从头开始,而不是尝试将当前脚本调整为选项卡图形用户界面。它会让您更好地了解它们的工作原理。
  • 我同意从头开始可能更有效,我不介意。但问题仍然存在。如何将对象(即文本框、按钮)放入选项卡中?你能帮我看一些文件吗?
  • 这是一个很好的使用 WPF 和 XAML 的教程,它肯定会给你一个提升,因为你不必声明太多的控件,第四部分是你的目标:foxdeploy.com/blog/…
  • 谢谢@alexzelaya,我有空会读这篇文章

标签: powershell user-interface tabs placement


【解决方案1】:

我认为是

$Tab1.Controls.Add($button)

你正在寻找。

例如

[void] [System.Reflection.Assembly]::LoadWithPartialName("System.Drawing") 
[void] [System.Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms")

$ApplicationForm = New-Object System.Windows.Forms.Form
$ApplicationForm.StartPosition = "CenterScreen"
$ApplicationForm.Topmost = $false 
$ApplicationForm.Size = "800,600"

$FormTabControl = New-object System.Windows.Forms.TabControl 
$FormTabControl.Size = "755,475" 
$FormTabControl.Location = "25,75" 
$ApplicationForm.Controls.Add($FormTabControl)

$Tab1 = New-object System.Windows.Forms.Tabpage
$Tab1.DataBindings.DefaultDataSourceUpdateMode = 0 
$Tab1.UseVisualStyleBackColor = $True 
$Tab1.Name = "Tab1" 
$Tab1.Text = "Tab1” 
$FormTabControl.Controls.Add($Tab1)

$textBox1 = New-Object System.Windows.Forms.TextBox
$textBox1.Location = New-Object System.Drawing.Point(10,70) ### Location of the text box
$textBox1.Size = New-Object System.Drawing.Size(200,50) ### Size of the text box
$textBox1.Multiline = $false ### Allows multiple lines of data
$textBox1.Font = New-Object System.Drawing.Font("Consolas",10,[System.Drawing.FontStyle]::Regular)
$textBox1.ReadOnly=$false
$Tab1.Controls.Add($textBox1)


$Tab2 = New-object System.Windows.Forms.Tabpage
$Tab2.DataBindings.DefaultDataSourceUpdateMode = 0 
$Tab2.UseVisualStyleBackColor = $True 
$Tab2.Name = "Tab2" 
$Tab2.Text = "Tab2” 
$FormTabControl.Controls.Add($Tab2)

$button = New-Object System.Windows.Forms.Button
$button.Location = New-Object System.Drawing.Point(10,120)
$button.Size = New-Object System.Drawing.Size (200,30)
$button.Text = "Usun Folder"
$button.Add_Click($button_click)
$Tab2.Controls.Add($button)

# Initlize the form
$ApplicationForm.Add_Shown({$ApplicationForm.Activate()})
[void] $ApplicationForm.ShowDialog()

【讨论】:

  • 好的,谢谢。这是一个更直接的解决方案
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2023-03-28
  • 1970-01-01
  • 1970-01-01
  • 2015-11-28
  • 2018-05-13
  • 1970-01-01
  • 2020-06-02
相关资源
最近更新 更多