有谁知道我如何在 C# 中引用系统背景颜色,用于滚动条或插入选项按钮或选项卡等元素(作为下图中“tabPage1”的背景)?
或者,如果不存在预定义的 const,任何算法如何创建具有这种颜色的 Brush?谢谢!
我的直觉首选 SystemColors.ScrollBar
结果与 .Control
或 .ButtonFace
('tabPage2' 的 BG)相同,它是真的是滚动条的“面部”颜色,而不是我所说的“背景”。
SystemColors.ControlLight
或 SystemColors.ControlLightLight
不要靠近..
当放大时,控件的那个区域看起来像一个带有 .Control
和 .Window
像素的棋盘,所以对我来说这暗示了这种可能性颜色被抖动;也许这表明标准 SystemColors
枚举没有定义它的原因?我如何将它应用到另一个控件?
P.S.:我正在尝试增强自定义 TabControl(它支持可禁用的选项卡 - std 控件中缺少的另一个好东西,但这是另一个已经解决的故事),所以当它的 .Appearance
设置为 Buttons
或 FlatButtons
,它看起来与原始按钮(如上图所示)相似。选定的选项卡由按下的按钮指示,但其背景颜色设置为 Control
或 ButtonFace
:
最佳答案
好吧,这比我想象的要简单得多 - 它只是关于 Brush
,用于填充 BG-rect:
protected override void OnDrawItem( DrawItemEventArgs e )
{
base.OnDrawItem( e );
int i= e.Index;
TabPage p= this.TabPages[ i ];
Rectangle r= GetTabRect( i );
Brush br= null;
if( this.Appearance != TabAppearance.Normal && i == this.SelectedIndex )
br= new HatchBrush( HatchStyle.Percent50, SystemColors.Control, SystemColors.Window );
else
br= new SolidBrush( p.BackColor );
try // fill the BG-rectangle
{ e.Graphics.FillRectangle( br, r ); }
finally
{ br.Dispose( ); } // make sure brush is disposed of
.. // the rest of the event-handler
}
唯一的技巧是不能应用 using( Brush br= new .. ) {..}
block ,因为初始化与生成的对象类型不同。因此,一个有点难看的 try-finally
结构。很有魅力!
关于c# - 如何引用某些 Windows 元素(滚动条、插入式选项按钮等)使用的 BG 颜色?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9182690/