Powershell、WinForms和模式弹出窗口,背景褪色

Powershell、WinForms和模式弹出窗口,背景褪色,winforms,powershell,powershell-4.0,Winforms,Powershell,Powershell 4.0,我正在尝试实现一个PowerShell表单,其中后台桌面上有一个褪色的覆盖层 我认为实现这一点的唯一方法是使用两种形式——一种是显示全屏覆盖(背景形式),另一种是实际的(主)形式。但是,当我单击关闭(或停用)主窗体时,我希望所有内容都关闭。然而,它不再关闭,我不明白为什么?我感觉当我点击主窗体时,点击事件正试图点击背景窗体(由于主窗体有焦点而无法点击),而“停用”事件不再工作 感谢您的帮助: [void][System.Reflection.Assembly]::LoadWithPartialN

我正在尝试实现一个PowerShell表单,其中后台桌面上有一个褪色的覆盖层

我认为实现这一点的唯一方法是使用两种形式——一种是显示全屏覆盖(背景形式),另一种是实际的(主)形式。但是,当我单击关闭(或停用)主窗体时,我希望所有内容都关闭。然而,它不再关闭,我不明白为什么?我感觉当我点击主窗体时,点击事件正试图点击背景窗体(由于主窗体有焦点而无法点击),而“停用”事件不再工作

感谢您的帮助:

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

$windowsGreyTrans = [System.Drawing.Color]::FromArgb(20,43,43,43)
$windowsGreyOff = [System.Drawing.Color]::FromArgb(43,43,43)
$windowsGreyOn = [System.Drawing.Color]::FromArgb(60,60,60)

$bgform = New-Object System.Windows.Forms.Form
$bgform.WindowState = 'Maximized'
$bgform.ShowInTaskbar = $false
$bgform.MaximizeBox = $false
$bgform.Opacity = 0.5
$bgform.ControlBox = $false
$bgform.FormBorderStyle = [System.Windows.Forms.FormBorderStyle]::none

[void]$bgform.show()

$form = New-Object System.Windows.Forms.Form
$form.Size = New-Object System.Drawing.Size(200, 40)
$Form.StartPosition = "CenterScreen"
$form.ShowInTaskbar = $false
$form.Name ="Power"
$form.MaximizeBox = $false
$form.ControlBox = $false
$form.FormBorderStyle = [System.Windows.Forms.FormBorderStyle]::none

$shutdownBtn = New-Object System.Windows.Forms.Button
$shutdownBtn.Location = New-Object System.Drawing.Point(0, 0)
$shutdownBtn.Size = New-Object System.Drawing.Size(200, 40)
$shutdownBtn.Text = "Shut down"
$shutdownBtn.FlatStyle = "Flat"
$shutdownBtn.UseVisualStyleBackColor = $True
$shutdownBtn.BackColor = $windowsGreyOff
$shutdownBtn.FlatAppearance.BorderSize = 0
$shutdownBtn.ForeColor = "White"
$shutdownBtn.Font = "Segoe UI,10pt"
#$shutdownBtn.Cursor = [System.Windows.Forms.Cursors]::Hand

$shutdownBtn.Add_Click({         
    try {
    }
    catch{
        
    }
})

$shutdownBtn.Add_MouseEnter({
    $this.BackColor = $windowsGreyOn
})
$shutdownBtn.Add_MouseLeave({
    $this.BackColor = $windowsGreyOff
})

$form.Controls.Add($shutdownBtn)

$form.Add_Deactivate({    
    $this.Close()
    $bgform.close()
})

$form.Add_Shown({
   $this.Activate()
})

[void]$form.showdialog()

我建议您一个更好的解决方案,使用tablelayoutpanel使按钮居中并截取单击事件

试试这个:

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

$windowsGreyTrans = [System.Drawing.Color]::FromArgb(20,43,43,43)
$windowsGreyOff = [System.Drawing.Color]::FromArgb(43,43,43)
$windowsGreyOn = [System.Drawing.Color]::FromArgb(60,60,60)

#create form
$form = New-Object System.Windows.Forms.Form
$form.SuspendLayout()
$form.WindowState = [System.Windows.Forms.FormWindowState]::Maximized
$form.ShowInTaskbar = $false
$form.MaximizeBox = $false
$form.Opacity = 0.5
$form.ControlBox = $false
$form.FormBorderStyle = [System.Windows.Forms.FormBorderStyle]::none

#create Tablelayout panel on this form
$TableLayoutPanel = New-Object System.Windows.Forms.TableLayoutPanel
$TableLayoutPanel.SuspendLayout()
$TableLayoutPanel.ColumnCount=1
$TableLayoutPanel.RowCount=1
$TableLayoutPanel.Dock=[System.Windows.Forms.DockStyle]::Fill

$form.Controls.Add($TableLayoutPanel)

#create button on this tablelayoutpanel
$shutdownBtn = New-Object System.Windows.Forms.Button
$shutdownBtn.Size = New-Object System.Drawing.Size(200, 40)
$shutdownBtn.Text = "Shut down"
$shutdownBtn.FlatStyle = "Flat"
$shutdownBtn.UseVisualStyleBackColor = $True
$shutdownBtn.BackColor = $windowsGreyOff
$shutdownBtn.FlatAppearance.BorderSize = 0
$shutdownBtn.ForeColor =[System.Drawing.Color]::White
$shutdownBtn.Font = "Segoe UI,10pt"
$shutdownBtn.Anchor=[System.Windows.Forms.AnchorStyles]::None

$TableLayoutPanel.Controls.Add($shutdownBtn)

#Layout
$TableLayoutPanel.ResumeLayout($false)
$form.ResumeLayout($false)

$shutdownBtn.Add_Click({         
    try {
    
    }
    catch{
        
    }
})

#change color to tablelayot on first show
$form.Add_Shown({         
    $TableLayoutPanel.BackColor =$windowsGreyTrans
})

$TableLayoutPanel.Add_Click({         
    $form.close()
})

$shutdownBtn.Add_MouseEnter({
    $this.BackColor = $windowsGreyOn
})
$shutdownBtn.Add_MouseLeave({
    $this.BackColor = $windowsGreyOff
})



[void]$form.ShowDialog()
$form.Dispose()

谢谢你。从点击的角度来看,它工作得很好。我想唯一的缺点是按钮也是透明的,但如果我给它们加上较深的灰色,这可能没什么大不了的……我想你不知道如何将两个按钮叠在一起?无法指定位置有点痛苦……我最终还是设法做到了。两排,50%高。顶部按钮与底部对齐,底部按钮与顶部对齐!谢谢。为你高兴:)你好!