【问题标题】:Powershell, WinForms and modal popup with faded backgroundPowershell、WinForms 和带有褪色背景的模态弹出窗口
【发布时间】:2020-10-07 14:49:04
【问题描述】:

我正在尝试实现一个 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()

【问题讨论】:

    标签: winforms powershell powershell-4.0


    【解决方案1】:

    我建议你一个更好的解决方案,使用一个表格布局面板来让你的按钮居中并拦截点击事件。

    试试这个:

    [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% 的高度。顶部按钮对齐底部,底部按钮对齐顶部!谢谢。
    • 祝你快乐 :) 祝你有美好的一天!
    猜你喜欢
    • 1970-01-01
    • 2013-07-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-12-26
    • 2018-01-18
    • 1970-01-01
    • 2015-05-06
    相关资源
    最近更新 更多