Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/powershell/12.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
Forms Powershell Windows窗体边框颜色/控件?_Forms_Powershell - Fatal编程技术网

Forms Powershell Windows窗体边框颜色/控件?

Forms Powershell Windows窗体边框颜色/控件?,forms,powershell,Forms,Powershell,有没有办法将焦点上的边框颜色设置为红色,然后在焦点丢失时将边框颜色设置为无? 如果可能的话,我还想同时将边框样式设置得更厚一些 $form= New-Object system.Windows.Forms.Form $form.Text= "Testing" $form.StartPosition= 'CenterScreen' $form.ClientSize= '400,200' $form.text= "Form" $form.BackColor= "#0077f7" $form.TopM

有没有办法将焦点上的边框颜色设置为红色,然后在焦点丢失时将边框颜色设置为无? 如果可能的话,我还想同时将边框样式设置得更厚一些

$form= New-Object system.Windows.Forms.Form
$form.Text= "Testing"
$form.StartPosition= 'CenterScreen'
$form.ClientSize= '400,200'
$form.text= "Form"
$form.BackColor= "#0077f7"
$form.TopMost= $true
$form.AutoScroll= $true
$form.Add_KeyDown({if ($_.KeyCode -eq "Escape") { $form.Close() } }) # if escape, exit
$form.KeyPreview= $True

$lbl_one= New-Object system.Windows.Forms.Label
$lbl_one.text= "Input"
$lbl_one.AutoSize= $true
$lbl_one.width= 25
$lbl_one.height= 10
$lbl_one.location= New-Object System.Drawing.Point(10,10)
$lbl_one.Font= 'Verdana,12'
$lbl_one.ForeColor= "#fafa00"

$txt_one= New-Object system.Windows.Forms.TextBox
$txt_one.TabIndex= 1
$txt_one.multiline= $false
$txt_one.text= "test1"
$txt_one.width= 100
$txt_one.height= 20
$txt_one.location= New-Object System.Drawing.Point(100,10)
$txt_one.Font= 'Verdana ,12'
$txt_one.ForeColor= "#0077f7"
$txt_one.Add_GotFocus({ $txt_one.BackColor="LightBlue" } )
$txt_one.Add_LostFocus( { $txt_one.BackColor="White" } )

$lbl_two= New-Object system.Windows.Forms.Label
$lbl_two.text= "Input"
$lbl_two.AutoSize= $true
$lbl_two.width= 25
$lbl_two.height= 10
$lbl_two.location= New-Object System.Drawing.Point(10,40)
$lbl_two.Font= 'Verdana,12'
$lbl_two.ForeColor= "#fafa00"

$txt_two= New-Object system.Windows.Forms.TextBox
$txt_two.TabIndex= 1
$txt_two.multiline= $false
$txt_two.text= "test2"
$txt_two.width= 100
$txt_two.height= 20
$txt_two.location= New-Object System.Drawing.Point(100,40)
$txt_two.Font= 'Verdana ,12'
$txt_two.ForeColor= "#0077f7"
$txt_two.Add_GotFocus({ $txt_two.BackColor="LightBlue" } )
$txt_two.Add_LostFocus( { $txt_two.BackColor="White" } )

$form.controls.AddRange(@($lbl_one,$txt_one,$lbl_two,$txt_two))
[void]$form.ShowDialog()

这可能很难理解,但您可以尝试在代码顶部添加此帮助器函数:

function Paint-FocusBorder([System.Windows.Forms.Control]$control) {
    # get the parent control (usually the form itself)
    $parent = $control.Parent
    $parent.Refresh()
    if ($control.Focused) {
        $control.BackColor = "LightBlue"
        $pen = [System.Drawing.Pen]::new('Red', 2)
    }
    else {
        $control.BackColor = "White"
        $pen = [System.Drawing.Pen]::new($parent.BackColor, 2)
    }
    $rect = [System.Drawing.Rectangle]::new($control.Location, $control.Size)
    $rect.Inflate(1,1)
    $parent.CreateGraphics().DrawRectangle($pen, $rect)
}
# call the Paint-FocusBorder when the form is first drawn
$form.Add_Shown({Paint-FocusBorder $txt_one})

# show the form
[void]$form.ShowDialog()

# clean-up
$form.Dispose()
并设置
GotFocus
LostFocus
事件处理程序,如下所示:

$txt_one.Add_GotFocus({ Paint-FocusBorder $this })
$txt_one.Add_LostFocus({ Paint-FocusBorder $this })
...
$txt_two.Add_GotFocus({ Paint-FocusBorder $this })
$txt_two.Add_LostFocus({ Paint-FocusBorder $this })
在代码的最后:

function Paint-FocusBorder([System.Windows.Forms.Control]$control) {
    # get the parent control (usually the form itself)
    $parent = $control.Parent
    $parent.Refresh()
    if ($control.Focused) {
        $control.BackColor = "LightBlue"
        $pen = [System.Drawing.Pen]::new('Red', 2)
    }
    else {
        $control.BackColor = "White"
        $pen = [System.Drawing.Pen]::new($parent.BackColor, 2)
    }
    $rect = [System.Drawing.Rectangle]::new($control.Location, $control.Size)
    $rect.Inflate(1,1)
    $parent.CreateGraphics().DrawRectangle($pen, $rect)
}
# call the Paint-FocusBorder when the form is first drawn
$form.Add_Shown({Paint-FocusBorder $txt_one})

# show the form
[void]$form.ShowDialog()

# clean-up
$form.Dispose()
另外,在事件处理程序脚本块中,可以使用
$this
引用控件本身


另外,完成后请执行
$form.Dispose()

谢谢。但当我进行测试时,我必须在红色边框出现之前至少在两个字段中滚动tab一次。关于解决这个问题的建议?另外,对于如何使用PowerShell执行Windows窗体,您是否推荐了一个好的网站?谷歌对PowerShell没有太多表现。再次感谢你@PaulWasserman我编辑了我的答案,使代码更通用,并在表单首次显示时绘制边框。使用PowerShell构建表单的一个好站点是工作完美的。我确实使用POSHGUI创建了我的初始页面,并且想知道您将使用哪个网站来实现windows窗体控件,尤其是与Powershell相关的控件。再次感谢西奥!后续问题-这在Powershell 5.1中运行良好,但在Powershell 7中失败。PS C:\work>\formsTest.ps1 SetValueInvocationException:C:\work\formsTest.ps1:6 Line | 6 | |$form.ClientSize='400200'| ~~~~~~~~~~~~~~~~~~~~~~~~~~异常设置“ClientSize:”无法将“System.String”类型的“400200”值转换为“System.Drawing.Size”。…这太棒了!非常感谢。