Astrogator
Astrogator

Reputation: 1061

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)?
Or, if no predefined const exists, any algorithm how to create a Brush with this color? Thank you!

background for tabPage1 indicates desired color

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.ControlLight or SystemColors.ControlLightLight do not get any closer..

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; maybe that indicates the reason why standard SystemColors enum does not define it? How would I apply it to another control?

zoomed-in view exposes the dithering

P.S.: 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). Selected tab is indicated by a pushed-in button, but its background color is set to Control or ButtonFace:

enter image description here

Upvotes: 4

Views: 821

Answers (2)

Astrogator
Astrogator

Reputation: 1061

Ok, this turns out to be a lot simpler than I thought - it's just about the Brush, used to fill the 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
}

The only trick is that a using( Brush br= new .. ) {..} block cannot be applied, as initialization differs with resulting object type. Hence, a somewhat ugly try-finally construct. Works as a charm!

Upvotes: 1

GazTheDestroyer
GazTheDestroyer

Reputation: 21251

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.

Unfortunately I couldn't find a .Net equivalent function, sorry.

Upvotes: 0

Related Questions