Another KryptonTabControl

Share your code snippets, controls and screenshots.

Moderators: Phil Wright, Chris Porter

Another KryptonTabControl

Postby ghaberek » Wed Jul 27, 2011 7:30 am

I was looking for a simple Kryptonized TabControl. I looked at Angel's controls but honestly it's a bit much for me. I just need a simple and easy-to-use, yet nice-looking TabControl. I found this thread and built upon the code there. I basically aligned the tabs better with the left edge and carried the border over the selected tab. I think it looks pretty nice!

Update 07-29-2011
  • Added more Krypton properties (PaletteMode, etc.)
  • Added design-time tasks (SmartTags, Verbs, etc.)
  • Added KryptonTabPage for better control over tab painting
  • Updated RefreshPalette() to update the control when it is triggered for better design-time suppport

Todo:
  • Fix drawing for all Alignments (Bottom, Left, Right) and Appearances (Buttons, FlatButtons)

Here are some examples:

A simple KryptonTabControl / with images

Image Image

Other themes

Image Image Image Image

I also ran across an issue when visual styles were disabled, which I fixed:

Before / After

Image Image

Download:

KryptonTabControl101_src.zip (16 KB)
KryptonTabControl101_bin.zip (8 KB)

Mercurial:

Code: Select all
hg clone https://bitbucket.org/ghaberek/kryptontabcontrol
Last edited by ghaberek on Sat Jul 30, 2011 5:51 am, edited 2 times in total.
ghaberek
 
Posts: 10
Joined: Wed Jul 27, 2011 6:36 am

Re: Another KryptonTabControl

Postby coolvijaymca » Wed Jul 27, 2011 1:58 pm

Hi, At least post some working example and also if possible then add a property to change Palette at design time.

Vijay
coolvijaymca
 
Posts: 27
Joined: Fri Oct 22, 2010 3:23 pm

Re: Another KryptonTabControl

Postby Angel » Fri Jul 29, 2011 8:09 pm

Hi, very nice ...
I've fixed some method declaration and forced a repaint on runtime palette change, this is the code:
Code: Select all
using Microsoft.VisualBasic;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.Diagnostics;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Windows.Forms;
using ComponentFactory.Krypton.Toolkit;



    [ToolboxBitmap(typeof(System.Windows.Forms.TabControl))]
    public class KryptonTabControlLite : TabControl
    {

        static IRenderer Renderer;
        static IPalette Palette;
        static ViewLayoutContext ViewLayoutContext;

        static Control ViewLayoutContextControl = new Control();
        static PaletteBackInheritRedirect PaletteTabPageBackground;
        static PaletteBorderInheritRedirect PaletteTabPageBorder;
        static PaletteBackInheritRedirect PaletteTabButtonBackground;
        static PaletteBorderInheritRedirect PaletteTabButtonBorder;
        static IDisposable MementoTabPageBackground;
        static IDisposable MementoTabButtonBackground;
        static IDisposable MementoTabButtonBorder;
        static Font TabFontBold;
        static Font TabFontRegular;
        static SolidBrush TabBrush;

        static StringFormat SF;
        bool _DrawTabPage;

        Font TabFont;
        public KryptonTabControlLite()
        {
            SetStyle(ControlStyles.AllPaintingInWmPaint | ControlStyles.UserPaint | ControlStyles.OptimizedDoubleBuffer | ControlStyles.ResizeRedraw, true);
            SF = new StringFormat(StringFormatFlags.NoWrap);
            SF.Alignment = StringAlignment.Center;
            SF.LineAlignment = StringAlignment.Center;

            RefreshPalette();

            KryptonManager.GlobalPaletteChanged += KryptonManager_GlobalPaletteChanged;
        }

        private  void KryptonManager_GlobalPaletteChanged(object sender, EventArgs e)
        {
            RefreshPalette();
        }

        private void RefreshPalette()
        {
            Palette = KryptonManager.CurrentGlobalPalette;
            Renderer = Palette.GetRenderer();
            ViewLayoutContext = new ViewLayoutContext(ViewLayoutContextControl, Renderer);

            PaletteRedirect PaletteRedirect = new PaletteRedirect(Palette);
            PaletteTabPageBackground = new PaletteBackInheritRedirect(PaletteRedirect);
            PaletteTabPageBorder = new PaletteBorderInheritRedirect(PaletteRedirect);
            PaletteTabButtonBackground = new PaletteBackInheritRedirect(PaletteRedirect);
            PaletteTabButtonBorder = new PaletteBorderInheritRedirect(PaletteRedirect);

            PaletteTabPageBackground.Style = PaletteBackStyle.ControlToolTip;
            PaletteTabPageBorder.Style = PaletteBorderStyle.ButtonNavigatorStack;
            PaletteTabButtonBackground.Style = PaletteBackStyle.ButtonNavigatorStack;
            PaletteTabButtonBorder.Style = PaletteBorderStyle.ButtonNavigatorMini;

            TabFontBold = new Font(Palette.GetContentShortTextFont(PaletteContentStyle.ButtonNavigatorStack, PaletteState.Normal), FontStyle.Bold);
            TabFontRegular = new Font(TabFontBold, FontStyle.Regular);
            TabBrush = new SolidBrush(Palette.GetContentShortTextColor1(PaletteContentStyle.ButtonNavigatorStack, PaletteState.Normal));
            Invalidate();

        }

        [System.ComponentModel.DefaultValue(false)]
        public bool DrawTabPage
        {
            get { return _DrawTabPage; }
            set
            {
                _DrawTabPage = value;
                if (_DrawTabPage)
                {
                    foreach (TabPage TP in TabPages)
                    {
                        TP.Paint += TabPage_Paint;
                    }
                    if (SelectedTab != null)
                        SelectedTab.Invalidate();
                }
                else
                {
                    foreach (TabPage TP in TabPages)
                    {
                        TP.Paint -= TabPage_Paint;
                    }
                    if (SelectedTab != null)
                        SelectedTab.Invalidate();
                }
            }
        }

        protected override void OnPaint(System.Windows.Forms.PaintEventArgs e)
        {
            Rectangle RBorder = DisplayRectangle;
            RBorder.Inflate(1, 1);

            using (Pen PBorder = new Pen(PaletteTabPageBorder.GetBorderColor1(PaletteState.Normal)))
            {
                e.Graphics.DrawRectangle(PBorder, RBorder);
            }

            if (this.TabCount > 0)
            {
                using (RenderContext renderContext = new RenderContext(this, e.Graphics, e.ClipRectangle, Renderer))
                {
                    renderContext.Graphics.CompositingQuality = CompositingQuality.HighQuality;
                    renderContext.Graphics.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;

                    int selectedIndex = this.SelectedIndex;
                    for (int index = 0; index <= this.TabCount - 1; index++)
                    {
                        if (index != selectedIndex)
                            this.PaintTab(index, renderContext);
                    }
                    if (selectedIndex >= 0)
                        this.PaintTab(SelectedIndex, renderContext);
                }
            }

        }

        protected override void OnPaintBackground(System.Windows.Forms.PaintEventArgs pevent)
        {
            if (Application.RenderWithVisualStyles)
            {
                System.Windows.Forms.GroupBoxRenderer.DrawParentBackground(pevent.Graphics, pevent.ClipRectangle, this);
            }
            else
            {
                // Fill default panel background color (TODO: this should be configurable)
                Color backColor = Palette.GetBackColor1(PaletteBackStyle.PanelClient, PaletteState.Normal);
                using (SolidBrush backBrush = new SolidBrush(backColor))
                {
                    pevent.Graphics.FillRectangle(backBrush, pevent.ClipRectangle);
                }
            }
        }

        private void PaintTab(int index, RenderContext RenderContext)
        {
            bool Selected = (SelectedIndex == index);

            Rectangle TabRect = GetTabRect(index);
            //If Selected Then TabRect.Inflate(0, 2)

            if ((Appearance & TabAppearance.Normal) == TabAppearance.Normal)
            {
                TabRect.Inflate(0, Selected ? 2 : 1);
                TabRect.X += 1;
            }

            PaletteState State = default(PaletteState);
            if (Selected)
            {
                State = PaletteState.Pressed;
            }
            else
            {
                State = TabRect.Contains(PointToClient(MousePosition)) ? PaletteState.Tracking : PaletteState.Normal;
            }

            VisualOrientation visualOrientation = (VisualOrientation)Alignment;

            if (PaletteTabButtonBackground.GetBackDraw(State) == InheritBool.True)
            {
                using (GraphicsPath BackPath = Renderer.RenderStandardBorder.GetBackPath(RenderContext, TabRect, PaletteTabButtonBorder, visualOrientation, State))
                {
                    MementoTabButtonBackground = Renderer.RenderStandardBack.DrawBack(RenderContext, TabRect, BackPath, PaletteTabButtonBackground, visualOrientation, State, MementoTabButtonBackground);
                }
            }

            if (PaletteTabButtonBorder.GetBorderDraw(State) == InheritBool.True)
            {
                Renderer.RenderStandardBorder.DrawBorder(RenderContext, TabRect, PaletteTabButtonBorder, visualOrientation, State);
            }
            else if (Selected)
            {
                using (Pen PBorder = new Pen(PaletteTabPageBorder.GetBorderColor1(PaletteState.Normal)))
                {
                    Rectangle RBorder = TabRect;
                    RBorder.Width -= 1;

                    RenderContext.Graphics.DrawRectangle(PBorder, RBorder);
                }
            }

            // Draw tab image (TODO: adjust rendering for other Appearance settings)
            if (ImageList != null)
            {
                Image tabImage = null;

                if (TabPages[index].ImageIndex != -1)
                {
                    int imageIndex = TabPages[index].ImageIndex;
                    tabImage = ImageList.Images[imageIndex];
                }
                else if (TabPages[index].ImageKey != null)
                {
                    string imageKey = TabPages[index].ImageKey;
                    tabImage = ImageList.Images[imageKey];
                }

                if (tabImage != null)
                {
                    int x = TabRect.X + (tabImage.Width / 2);
                    int y = TabRect.Y + (TabRect.Height - tabImage.Height) / 2;

                    RenderContext.Graphics.DrawImage(tabImage, x, y);

                    TabRect.X += tabImage.Width;
                    TabRect.Width -= tabImage.Width;

                }

            }

            if (TabFont == null || (!object.ReferenceEquals(TabFont, TabFontBold) & !object.ReferenceEquals(TabFont, TabFontRegular)))
            {
                if (RenderContext.Graphics.MeasureString(TabPages[index].Text, TabFontBold, TabRect.X, SF).Width <= TabRect.Width)
                {
                    TabFont = TabFontBold;
                }
                else
                {
                    TabFont = TabFontRegular;
                }
            }

            RenderContext.Graphics.DrawString(TabPages[index].Text, TabFont, TabBrush, TabRect, SF);

        }

        protected override void OnControlAdded(System.Windows.Forms.ControlEventArgs e)
        {
            if (_DrawTabPage)
                e.Control.Paint += TabPage_Paint;
            base.OnControlAdded(e);
        }

        protected override void OnControlRemoved(System.Windows.Forms.ControlEventArgs e)
        {
            if (_DrawTabPage)
                e.Control.Paint -= TabPage_Paint;
            base.OnControlRemoved(e);
        }

        private void TabPage_Paint(object sender, System.Windows.Forms.PaintEventArgs e)
        {
            TabPage TP = (TabPage)sender;

            using (RenderContext renderContext = new RenderContext(TP, e.Graphics, e.ClipRectangle, Renderer))
            {
                using (GraphicsPath path = new GraphicsPath())
                {
                    Rectangle R = TP.DisplayRectangle;
                    path.AddRectangle(R);
                    MementoTabPageBackground = Renderer.RenderStandardBack.DrawBack(renderContext, R, path, PaletteTabPageBackground, VisualOrientation.Top, PaletteState.Normal, MementoTabPageBackground);
                }
            }
        }

    }



If you don't mind, I'll integrate into the ExtededRenderer Toolkit ...
Regards /// Angel
Angel
 
Posts: 251
Joined: Tue Aug 07, 2007 5:12 pm
Location: Lugano, Switzerland

Re: Another KryptonTabControl

Postby ghaberek » Sat Jul 30, 2011 5:49 am

coolvijaymca wrote:Hi, At least post some working example and also if possible then add a property to change Palette at design time.

Vijay

I've added a few design-time properties now to make the control more "Kryptonized". I don't know if an example is really necessary. If you can use a normal TabControl, you can use this. :D


Angel wrote:Hi, very nice ...
I've fixed some method declaration and forced a repaint on runtime palette change, this is the code:

snipped

If you don't mind, I'll integrate into the ExtededRenderer Toolkit ...
Regards /// Angel

Thanks Angel. I fixed that repaint issue myself almost immediately after posting the initial code. :P

I'd like to keep this as my own separate project, if you don't mind. I'm now hosting it on my BitBucket page.
ghaberek
 
Posts: 10
Joined: Wed Jul 27, 2011 6:36 am

Re: Another KryptonTabControl

Postby johnfermor » Sun Aug 21, 2011 12:07 am

Don't know if I'm missing something but ..

In design-time, setting the "Transparent Background" property turns the transparency OFF whereas turning it OFF, switches it on?!

Looks good though..

John :D
johnfermor
 
Posts: 101
Joined: Fri Oct 05, 2007 1:32 am
Location: Great Britain UK


Return to Code Snippets

Who is online

Users browsing this forum: No registered users and 1 guest