如何更改TabPage上文本的颜色?

时间:2018-11-21 14:33:53

标签: winforms powershell

需要使用哪些属性来更改“标签页”上文本的前色和背景色?

查看图片: Composition Root

这是我的代码:

$TabControl_Main = New-Object System.Windows.Forms.TabControl
$TabControl_Main.Location = New-Object System.Drawing.Size(20,550)
$TabControl_Main.Size =  New-Object System.Drawing.Size(850,270)
$form_MainForm.Controls.Add($TabControl_Main)

$TabPage1 = New-Object System.Windows.Forms.TabPage
$TabPage1.Location = New-Object System.Drawing.Size(20,550)
$TabPage1.Size =  New-Object System.Drawing.Size(850,270)
$TabPage1.Text = "Processes"       
$TabControl_Main.Controls.Add($TabPage1)

1 个答案:

答案 0 :(得分:0)

您必须创建一个事件并绘制区域。这是一些代码based on this example in c#, credits @Fun Mun Pieng

# assign a color for each tab
$PageColor  = @{0 = "lightgreen";
                1 = "yellow";
                2 = "lightblue"}

# define the event
$tabControl_Drawing = {
    param([object]$Sender, [System.EventArgs]$e)

    $Background = new-object Drawing.SolidBrush $PageColor[$e.Index]
    $Foreground = new-object Drawing.SolidBrush black

    $tabGraphics = $e.Graphics
    $tabBounds = $e.Bounds
    $tabGraphics.FillRectangle($Background,$tabBounds)
    $tabTextSize = $tabGraphics.MeasureString($sender.TabPages[$e.Index].text, $e.Font)

    $tabGraphics.DrawString($Sender.TabPages[$e.Index].Text,$e.Font,$Foreground,$tabBounds.Left + ($tabBounds.Width - $tabTextSize.Width) / 2,$tabBounds.Top + ($tabBounds.Height -$tabTextSize.Height) / 2 +1)
    $e.DrawFocusRectangle()
}
# add the event
$TabControl_Main.add_DrawItem($tabControl_Drawing)

HotTrack更易于使用:

$TabControl_Main.HotTrack = $true

使用powershell而不是powershell ISE执行脚本时,您会看到效果。

BackColor不执行任何操作。使用MSDN的话:

  

BackColor > This member is not meaningful for this control.

编辑:添加了代码。