【问题标题】:How Can I change the color of the Text on TabPage?如何更改 TabPage 上文本的颜色?
【发布时间】:2019-04-24 03:49:21
【问题描述】:

需要应用哪些属性来改变Tabpage上文字的前景色和背景色?

看图: https://imgur.com/a/Su8aSg7

这是我的代码:

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

【问题讨论】:

  • $TabControl_Main.BackColor = New-Object System.Drawing.Color("Red") 应该作为示例使用
  • 我想更改每个选项卡上每个文本的颜色:)。您在此处编写的此代码会导致以下错误:“New-Object : A constructor was not found. Cannot find a proper constructor for type System.Drawing.Color.”
  • $TabControl_Main.BackColor = [System.Drawing.Color]::Red 应该这样做。
  • 不工作。 :(

标签: winforms powershell


【解决方案1】:

您必须创建一个事件并绘制该区域。这是一些代码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.

编辑:添加代码。

【讨论】:

  • 我测试了这个,我看不到变化:\我想在 Powershell 中这样做:stackoverflow.com/questions/30039627/…
  • @Techno 添加了一些应该做你想做的代码。对于 HotTrack,使用 Powershell 执行脚本或将其复制到 powershell 会话中很重要。在 ISE 中,您不会看到更改。
猜你喜欢
  • 2011-03-17
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-09-21
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多