views:

137

answers:

2

I want to draw a custom TabControl with custom functionality.

To do this, i inherited the Panel class and overrided OnPaint method to draw with TabRenderer class.

The problem is that TabRenderer working only when visual styles enabled (can be checked with TabRenderer.IsSupported), but what should i do if visual styles disabled?

In this case, I thought using the ControlPaint class to draw tabs without visual styles, but it has no draw methods related to Tabs. I want it basically to behave visually like the regular TabControl.

+4  A: 

You have to draw it by yourself, because there is not published API for this. Hopefully this is relatively easy to do it in non-visualstyles way.

You can draw pane border with ControlPaint.DrawBorder3D and use something like the following code for buttons:

int Top = bounds.Top;
int Bottom = bounds.Bottom - 1;
int Sign = 1;

if (tabStrip.EffectiveOrientation == TabOrientation.Bottom)
{
    Top = bounds.Bottom - 1;
    Bottom = bounds.Top;
    Sign = -1;
}

using (Pen OuterLightBorderPen = new Pen(SystemColors.ControlLightLight))
{
    e.Graphics.DrawLine(OuterLightBorderPen, bounds.Left, Bottom, bounds.Left, Top + 2 * Sign);
    e.Graphics.DrawLine(OuterLightBorderPen, bounds.Left, Top + 2 * Sign, bounds.Left + 2, Top);
    e.Graphics.DrawLine(OuterLightBorderPen, bounds.Left + 2, Top, bounds.Right - 3, Top);
}

using (Pen InnerLightBorderPen = new Pen(SystemColors.ControlLight))
{
    e.Graphics.DrawLine(InnerLightBorderPen, bounds.Left + 1, Bottom, bounds.Left + 1, Top + 2 * Sign);
    e.Graphics.DrawLine(InnerLightBorderPen, bounds.Left + 2, Top + 1 * Sign, bounds.Right - 3, Top + 1 * Sign);
}

using (Pen OuterDarkBorderPen = new Pen(SystemColors.ControlDarkDark))
{
    e.Graphics.DrawLine(OuterDarkBorderPen, bounds.Right - 2, Top + 1 * Sign, bounds.Right - 1, Top + 2 * Sign);
    e.Graphics.DrawLine(OuterDarkBorderPen, bounds.Right - 1, Top + 2 * Sign, bounds.Right - 1, Bottom);
}

using (Pen InnerDarkBorderPen = new Pen(SystemColors.ControlDark))
    e.Graphics.DrawLine(InnerDarkBorderPen, bounds.Right - 2, Top + 2 * Sign, bounds.Right - 2, Bottom);
arbiter
you could simplify this a lot using SystemPens - http://msdn.microsoft.com/en-us/library/system.drawing.systempens.aspx
Simon
Thanks for this code! Looks like the real one! Thanks! :-D
DxCK
A: 

This is an "out there" answer but is it possible that you could use wpf? As you can see from the answer above it is a pain in the ear to customise controls in winforms where as in WPF each control is lookless. This means that you control what is rendered and how it looks completely.

DeanMc
Thanks for the suggestion, but i need to support windows 2000 so WPF is not an option for me right now.
DxCK