[英]How to refer to BG-color, used by some Windows elements (scrollbar, pushed-in option button, etc.)?
Does anybody know how can I reference in C# a system background color, used on elements like scrollbar or pushed-in option button or tab (as the background for 'tabPage1' in the image below)? 有谁知道我如何在C#中引用系统背景颜色,用于滚动条或推入选项按钮或标签等元素(作为下图中“tabPage1”的背景)?
Or, if no predefined const exists, any algorithm how to create a Brush with this color? 或者,如果不存在预定义的const,那么任何算法如何用这种颜色创建一个Brush? Thank you!
谢谢!
My intuitive first choice SystemColors.ScrollBar
results in same as .Control
or .ButtonFace
(BG for 'tabPage2'), which is really a "face" color for the scrollbar, not what I'd call "background". 我的直觉首选
SystemColors.ScrollBar
结果相同.Control
或.ButtonFace
(BG为“tabPage2”),这是真正为一个滚动条“面子”的色彩,不是我称之为“背景”。
SystemColors.ControlLight
or SystemColors.ControlLightLight
do not get any closer.. SystemColors.ControlLight
或SystemColors.ControlLightLight
不再接近..
When zoomed in, that area of the control looks like a checker-board with .Control
and .Window
pixels, so to me that hints at a possibility of this color being dithered; 放大时,控制的该区域看起来像一个棋盘用
.Control
和.Window
像素,因此,我认为在这种颜色被抖动的可能性提示; maybe that indicates the reason why standard SystemColors
enum does not define it? 也许这表明标准
SystemColors
枚举没有定义它的原因? How would I apply it to another control? 我如何将它应用于另一个控件?
PS: I'm trying to enhance a custom TabControl (which supports disableable tabs - another good thing missing from std controls, but that's another and already solved story), so that when its .Appearance
is set to Buttons
or FlatButtons
, it looks similar to the original (illustrated above). PS:我正在尝试增强自定义TabControl(它支持可禁用的标签 - std控件中缺少的另一个好东西,但这是另一个已经解决的故事),所以当它的
.Appearance
设置为Buttons
或FlatButtons
,它看起来很相似到原件(如上图所示)。 Selected tab is indicated by a pushed-in button, but its background color is set to Control
or ButtonFace
: 选定的选项卡由推入按钮指示,但其背景颜色设置为
Control
或ButtonFace
:
Ok, this turns out to be a lot simpler than I thought - it's just about the Brush
, used to fill the BG-rect: 好吧,事实证明这比我想象的要简单得多 - 它只是用于填充BG-rect的
Brush
:
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
}
The only trick is that a using( Brush br= new .. ) {..}
block cannot be applied, as initialization differs with resulting object type. 唯一的技巧是无法应用
using( Brush br= new .. ) {..}
块,因为初始化与结果对象类型不同。 Hence, a somewhat ugly try-finally
construct. 因此,一个有点丑陋的
try-finally
构造。 Works as a charm! 充当魅力!
I'm sorry I don't know Forms very well, but MFC has a function for providing such dithered bitmaps ( AfxGetDitheredBitmap ), so I assume you'll have to do something similar. 对不起,我不太了解Forms,但是MFC有一个提供这种抖动位图( AfxGetDitheredBitmap )的功能,所以我假设你必须做类似的事情。
Unfortunately I couldn't find a .Net equivalent function, sorry. 不幸的是,我找不到.Net等效函数,抱歉。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.