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

问题描述

我正在尝试实现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)
$windowsGreyOff = [System.Drawing.Color]::FromArgb(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)
$windowsGreyOff = [System.Drawing.Color]::FromArgb(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()

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...