Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/powershell/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Winforms 显示和隐藏面板(Powershell GUI)_Winforms_Powershell_User Interface - Fatal编程技术网

Winforms 显示和隐藏面板(Powershell GUI)

Winforms 显示和隐藏面板(Powershell GUI),winforms,powershell,user-interface,Winforms,Powershell,User Interface,当鼠标悬停在单词“test”上时,会出现一个面板。当光标位于面板(黄色区域)上时,面板可见。一切都很好。但是,当我将鼠标悬停在面板的元素(“Word”和“button”)上时,面板是隐藏的。但是,我不想这样。我希望在将光标移出面板区域时隐藏面板。问题:我怎样才能做到这一点?非常感谢 Add-Type -Assembly System.Windows.Forms $Main = New-Object System.Windows.Forms.Form $Main.Width = 300 $Mai

当鼠标悬停在单词“test”上时,会出现一个面板。当光标位于面板(黄色区域)上时,面板可见。一切都很好。但是,当我将鼠标悬停在面板的元素(“Word”和“button”)上时,面板是隐藏的。但是,我不想这样。我希望在将光标移出面板区域时隐藏面板。问题:我怎样才能做到这一点?非常感谢

Add-Type -Assembly System.Windows.Forms

$Main = New-Object System.Windows.Forms.Form
$Main.Width = 300
$Main.Height = 200
$Main.BackColor = '255, 255, 255'
$Main.StartPosition = 'CenterParent'

$Test = New-Object System.Windows.Forms.Label
$Test.Left = 10
$Test.Top = 10
$Test.Text = 'Test'

$Test.Add_MouseHover({ $Panel.Show() })

$Panel = New-Object System.Windows.Forms.Panel
$Panel.Left = 0
$Panel.Top = 10
$Panel.Width = 200
$Panel.Height = 100
$Panel.BackColor = '255, 255, 0'
$Panel.Hide()

$Panel.Add_MouseLeave({ $Panel.Hide() })

$Word = New-Object System.Windows.Forms.Label
$Word.Left = 80
$Word.Top = 20
$Word.Text = 'Word'

$Button = New-Object System.Windows.Forms.Button
$Button.Left = 50
$Button.Top = 50
$Button.Width = 100
$Button.Height = 30

$Main.Controls.Add($Panel)
$Main.Controls.Add($Test)
$Panel.Controls.Add($Word)
$Panel.Controls.Add($Button)

$Main.ShowDialog()

这是因为面板中的图元位于面板外部。 所以进入这两个系统中的任何一个都会触发

$Panel.Add_MouseLeave({ $Panel.Hide() })
。。。代码。您可以通过注释这一行并观察行为来验证这一点

您还有另一个问题,即“Test”一词也隐藏在面板中的悬停事件中,这听起来也不像您想要的

这不是特定于PowerShell的问题。这是一个GUI设计问题,因为无论您是使用PowerShell还是使用此WinForms/WPF设计的其他编程语言,这都会发生在GUI设计中

一个快速而肮脏的解决方法是在表单上执行此操作。例如:

# $Panel.Add_MouseLeave({ $Panel.Hide() })
$Main.Add_MouseEnter({ $Panel.Hide() })

现在,以上不是唯一的答案,但是,你真的应该去Youtube,观看一些Winforms/WPF GUI设计视频,以便更好地了解这个主题。然后,您可以在稍后编织所需的PowerShell内容。

当面板离开时会触发事件,当您将鼠标悬停在标签/按钮上时也会发生此事件

不管怎样,我就是这样让它工作的:

$Panel.Add_MouseLeave({
    $point = [System.drawing.Point]::new([System.Windows.Forms.Cursor]::Position.X + 1,[System.Windows.Forms.Cursor]::Position.Y + 1)
    if ($panel.width -lt $panel.PointToClient($point).X -or
        $panel.height -lt $panel.PointToClient($point).Y){
        $Panel.Hide()
    }
 })
请注意光标位置(+1)的操作,否则它仍在您的面板中。

仅仅因为您可以在powershell中构建GUI并不意味着您应该这样做。我强烈反对。