我希望更改当前活动选项卡页面上选项卡的颜色,以使其更加突出。
我已将 DrawMode 设置为 OwnerDrawFixed,并且我能够转换一些 C# 代码(在此处的另一个问题中找到)来绘制选项卡。我已启用 TabControl 选择和取消选择操作,但这些操作不会接收 DrawItem 事件。
这是我的 DrawItem 代码,如果您认为它可以优化,请告诉我,因为我不是最擅长将 c# 转换为 powershell。
$TabControl_DrawItem = {
$CurrentTabPage = $TabControl1.TabPages[$_.Index]
$paddedBounds = New-Object Rectanglef ($_.Bounds.Location,$_.Bounds.Size)
$paddedBounds.Inflate(-2,-2)
$_.Graphics.DrawString($CurrentTabPage.Text, $_.Font, [SolidBrush]::New([Color]::Black), $paddedBounds)}
我知道如何在加载时更改选项卡的背景颜色,但是每次选择新选项卡时如何执行此操作?
$_.Graphics.FillRectangle([SolidBrush]::New([Color]::Yellow), $_.Bounds)
这是我完整的测试表,提前致谢。
using namespace System.Windows.Forms
using namespace System.Drawing
Add-Type -AssemblyName System.Windows.Forms
Add-Type -AssemblyName System.Drawing
$TabControl_DrawItem = {
$CurrentTabPage = $TabControl1.TabPages[$_.Index]
$paddedBounds = New-Object Rectanglef ($_.Bounds.Location,$_.Bounds.Size)
$paddedBounds.Inflate(-2,-2)
$_.Graphics.DrawString($CurrentTabPage.Text, $_.Font, [SolidBrush]::New([Color]::Black), $paddedBounds)
}
$TabControl_Selecting = {
#---Tab is selecting
}
$TabControl_Deselecting = {
#---Tab is deselecting
}
$Form1 = New-Object -TypeName Form
$TabControl1 = New-Object -TypeName TabControl
$TabPage1 = New-Object -TypeName TabPage
$TabPage2 = New-Object -TypeName TabPage
$TabPage3 = New-Object -TypeName TabPage
$Form1.Text = "Test-TabControl-Form"
$Form1.ClientSize = (New-Object -TypeName Size -ArgumentList @(632,356))
$Form1.BackColor = [Color]::DimGray
$TabControl1.Name = "TabControl1"
$TabControl1.Size = (New-Object -TypeName Size -ArgumentList @(610,308))
$TabControl1.Location = (New-Object -TypeName Point -ArgumentList @(12,36))
$TabControl1.Multiline = $true
$TabControl1.HotTrack = $true
$TabControl1.SelectedIndex = 0
$TabControl1.TabIndex = 0
$TabControl1.DrawMode = [TabDrawMode]::OwnerDrawFixed
$TabControl1.add_DrawItem($TabControl_DrawItem)
$TabControl1.add_Selecting($TabControl_Selecting)
$TabControl1.add_Deselecting($TabControl_Deselecting)
$TabPage1.Name = "TabPage1"
$TabPage1.Text = "TabPage1"
$TabPage1.Location = (New-Object -TypeName Point -ArgumentList @(4,22))
$TabPage1.Padding = (New-Object -TypeName Padding -ArgumentList @(3))
$TabPage1.Size = (New-Object -TypeName Size -ArgumentList @(391,282))
$TabPage1.TabIndex = 0
$TabPage1.BackColor = [Color]::DimGray
$TabPage2.Name = "TabPage2"
$TabPage2.Text = "TabPage2"
$TabPage2.Location = (New-Object -TypeName Point -ArgumentList @(4,22))
$TabPage2.Padding = (New-Object -TypeName Padding -ArgumentList @(3))
$TabPage2.Size = (New-Object -TypeName Size -ArgumentList @(602,282))
$TabPage2.TabIndex = 1
$TabPage2.BackColor = [Color]::DimGray
$TabPage3.Name = "TabPage3"
$TabPage3.Text = "TabPage3"
$TabPage3.Location = (New-Object -TypeName Point -ArgumentList @(4,22))
$TabPage3.Size = (New-Object -TypeName Size -ArgumentList @(391,282))
$TabPage3.TabIndex = 2
$TabPage3.BackColor = [Color]::DimGray
$Form1.Controls.Add($TabControl1)
$TabControl1.Controls.Add($TabPage1)
$TabControl1.Controls.Add($TabPage2)
$TabControl1.Controls.Add($TabPage3)
$Form1.ShowDialog()
在编写选项卡名称之前,您必须实现
FillRectangle()
方法,然后检查选项卡 (Index
) 是否正在绘制:
$TabControl_DrawItem = {
$CurrentTabPage = $TabControl1.TabPages[$_.Index]
$paddedBounds = New-Object Rectanglef ($_.Bounds.Location,$_.Bounds.Size)
$paddedBounds.Inflate(-2,-2)
# If the tab being drawn is the currently selected tab, color it; otherwise, use a default color
$tabColor = if ($_.Index -eq $TabControl1.SelectedIndex)
{
switch ($CurrentTabPage.Name)
{
"TabPage1" { [Color]::Red }
"TabPage2" { [Color]::Green }
"TabPage3" { [Color]::Blue }
}
}
else
{
[Color]::White # Default color for non-selected tabs
}
$_.Graphics.FillRectangle([SolidBrush]::New($tabColor), $_.Bounds) # Fill tab background
$_.Graphics.DrawString($CurrentTabPage.Text, $_.Font, [SolidBrush]::New([Color]::Black), $paddedBounds)
}
我为每个选项卡指定了一些基本颜色,以在选择时更改为示例,但可以轻松修改它以满足您的需求。