Powershell Windows窗体边框颜色/控件? [英] Powershell Windows Form Border Color / Controls?

查看:98
本文介绍了Powershell Windows窗体边框颜色/控件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否有一种方法可以将焦点上的边框颜色设置为红色,然后在失去焦点时又重新设置为无?如果可能的话,我也想同时设置较粗的边框样式.

Is there a way to set the border color Red on focus and then back to none when focus is lost? I'd also like to set the border style thicker at the same time if possible.

$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()

推荐答案

这可能很笨拙,但是您可以尝试在代码之上添加此辅助函数:

This may be cludgy, but you can try adding this helper function on top of the code:

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)
}

并设置 GotFocus LostFocus 事件处理程序,如下所示:

and set up the GotFocus and LostFocus event handlers like this:

$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 })

以及代码的结尾:

# 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()

P.S.在事件处理程序脚本块中,您可以使用 $ this

P.S. Inside an event handler scriptblock, you can refer to the control itself using the $this Automatic variable.

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

这篇关于Powershell Windows窗体边框颜色/控件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆