TabStripProfessionalRenderer.cs 17.8 KB
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422
using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;
using System.Drawing;
using System.Windows.Forms.VisualStyles;
using System.Drawing.Drawing2D;

namespace RibbonStyle
{
    public class TabStripProfessionalRenderer : ToolStripProfessionalRenderer
    {

        private const int BOTTOM_LEFT = 0;
        private const int TOP_LEFT = 1;
        private const int TOP_RIGHT = 2;
        private const int BOTTOM_RIGHT = 3;
        private Color oncolor;
        private Color onbackcolor;

        private Color basecolor;
        private Color halocolor;

        int X0;
        int XF;
        int Y0;
        int YF;
        int T = 2;
        int i_Zero = 180;
        int i_Sweep = 90;
        int X; int Y;
        GraphicsPath path;
        int D = -1;

        public Color OnColor
        {
            get
            {
                return oncolor;
            }
            set
            {
                oncolor = value;
            }

        }
        public Color OnBackColor
        {
            get
            {
                return onbackcolor;
            }
            set
            {
                onbackcolor = value;
            }

        }

        public Color BaseColor
        {
            get { return basecolor; }
            set { basecolor = value; }
        }

        public Color HaloColor
        {
            get { return halocolor; }
            set { halocolor = value; }
        }

        /// <summary>
        /// This renderer supports rendering of tabs.
        /// </summary>
        public TabStripProfessionalRenderer()
        {
            this.RoundedEdges = false; // get rid of the curves around the edges
            this.OnColor = Color.FromArgb(226, 209, 156);
            this.OnBackColor = Color.FromArgb(191, 219, 255);

        }


        public Color SetTransparency(Color color, int transparency)
        {
            if (transparency >= 0 & transparency <= 255)
            {
                return Color.FromArgb(transparency, color.R, color.G, color.B);
            }
            else if (transparency > 255)
            {
                return Color.FromArgb(255, color.R, color.G, color.B);
            }
            else
            {
                return Color.FromArgb(0, color.R, color.G, color.B);
            }
        }

        /// <summary>
        /// Control when the background of the Tab is painting.
        /// </summary>
        /// <param name="e"></param>
        protected override void OnRenderButtonBackground(ToolStripItemRenderEventArgs e)
        {
            TabStrip tabStrip = e.ToolStrip as TabStrip;
            Tab tab = e.Item as Tab;
            int i_opacity = tab.i_opacity;

            if (tab == null)
            {
                base.OnRenderButtonBackground(e);
                return;
            }
            Rectangle bounds = new Rectangle(Point.Empty, e.Item.Size);
            Graphics g = e.Graphics;
            g.SmoothingMode = SmoothingMode.AntiAlias;
            g.TextRenderingHint = System.Drawing.Text.TextRenderingHint.AntiAlias;

            int textwidth = Convert.ToInt16(g.MeasureString(tab.Text, tab.Font).Width) - Convert.ToInt16(tab.Text.Length / 3);
            int textheight = Convert.ToInt16(g.MeasureString(tab.Text, tab.Font).Height);

            if (tabStrip != null)
            {

                #region Tab Selected and NOT active
                if (tab.b_selected & !tab.b_active)
                {
                    #region Show Lateral Gradients
                    Rectangle ri = bounds;
                    ri.Width = (tab.Width - textwidth) / 2;
                    ri.X += 2; ri.Width -= 2;
                    Color C0i = BaseColor; int c_origin = 255; C0i = SetTransparency(C0i, 0);
                    Color CFi = HaloColor; c_origin = 255; CFi = SetTransparency(CFi, c_origin - i_opacity);
                    LinearGradientBrush b = new LinearGradientBrush(ri, C0i, CFi, LinearGradientMode.BackwardDiagonal);
                    g.FillRectangle(b, ri.X, ri.Y, ri.Width, ri.Height);
                    int offset = bounds.Width - ri.Width - 1;
                    ri.Location = new Point(offset, 1);
                    b = new LinearGradientBrush(ri, C0i, CFi, LinearGradientMode.ForwardDiagonal);
                    g.FillRectangle(b, offset, ri.Y, ri.Width, ri.Height);
                    #endregion

                    #region Show Central Gradient
                    Rectangle r = bounds;
                    r.X += 2; r.Width -= 2;
                    r.Height = r.Height / 2;
                    Color C0 = BaseColor; c_origin = 255; C0 = SetTransparency(C0, c_origin - i_opacity);
                    Color CF = HaloColor; c_origin = 255; CF = SetTransparency(CF, c_origin - i_opacity);
                    b = new LinearGradientBrush(r, C0, CF, LinearGradientMode.Vertical);
                    g.FillRectangle(b, r.X, r.Y + r.Height + 1, r.Width - r.X, r.Height);
                    #endregion

                    #region Show Vertical Side Lines
                    int Offs = 0;
                    X0 = 0; XF = tab.Width + X0;
                    YF = tab.Height + Y0; Y0 = 0;
                    Point P0 = new Point(X0, Y0);
                    Point PF = new Point(X0, YF / 2);
                    Color C0sl = this.OnBackColor; C0sl = SetTransparency(C0sl, 0);
                    Color CFsl = Color.White;
                    Rectangle rsl = new Rectangle(X0, Y0 + Offs, X0 + 1, YF / 2);
                    b = new LinearGradientBrush(rsl, C0sl, CFsl, LinearGradientMode.Vertical);
                    g.FillRectangle(b, X0, Y0 + Offs, rsl.Width, rsl.Height);
                    rsl = new Rectangle(X0, YF / 2 - 1, X0 + 1, YF - Offs);
                    b = new LinearGradientBrush(rsl, CFsl, C0sl, LinearGradientMode.Vertical);
                    g.FillRectangle(b, X0, YF / 2 - 1, rsl.Width, rsl.Height);

                    Rectangle rsr = new Rectangle(XF - 2, Y0 + Offs, XF - 1, YF / 2);
                    b = new LinearGradientBrush(rsr, C0sl, CFsl, LinearGradientMode.Vertical);
                    g.FillRectangle(b, XF - 1, Y0 + Offs, rsr.Width, rsr.Height);
                    rsr = new Rectangle(XF - 2, YF / 2 - 1, XF - 1, YF - Offs);
                    b = new LinearGradientBrush(rsr, CFsl, C0sl, LinearGradientMode.Vertical);
                    g.FillRectangle(b, XF - 1, YF / 2 - 1, rsr.Width, rsr.Height);
                    #endregion

                    #region Show Border
                    X0 = 0; XF = tab.Width + X0;
                    Y0 = 0; YF = tab.Height + Y0;
                    P0 = new Point(X0, Y0);
                    PF = new Point(X0, Y0 + YF - 15);
                    Color BorderColor = BaseColor; c_origin = 255; BorderColor = SetTransparency(BorderColor, c_origin - i_opacity);
                    Pen PBorder = new Pen(BorderColor);
                    X = X0; Y = Y0; i_Zero = 270; D = 1; T = 5;
                    DrawArc(0);
                    g.DrawPath(PBorder, path);
                    X = X0; Y = Y0; i_Zero = 270; D = 1; T = 5;
                    DrawArc(1);
                    Color IBC = Color.White; c_origin = 164; IBC = SetTransparency(IBC, c_origin - i_opacity);
                    PBorder = new Pen(IBC);
                    g.DrawPath(PBorder, path);
                    #endregion
                }
                #endregion

                else if (tab.b_active & !tab.b_selected)
                {
                    #region Show Upper Rectangle
                    Rectangle upblock = new Rectangle(8, 3, bounds.Width - 16, 4);
                    g.FillRectangle(new SolidBrush(Color.FromArgb(245, 250, 255)), upblock);
                    #endregion

                    #region Show Bottom Rectangle
                    Color CFsl = this.BaseColor;
                    Color C0sl = Color.FromArgb(CFsl.R + 19, CFsl.G + 15, CFsl.B + 10);
                    Rectangle doblock = new Rectangle(6, 3, bounds.Width - 12, bounds.Height);
                    Brush b = new LinearGradientBrush(doblock, C0sl, CFsl, LinearGradientMode.Vertical);
                    g.FillRectangle(b, doblock);
                    #endregion

                    #region Show Line Borders
                    Pen b2 = Pens.White; Pen b3 = Pens.White; Pen b4 = Pens.White;
                    int R0 = this.BaseColor.R;
                    int G0 = this.BaseColor.G;
                    int B0 = this.BaseColor.B;
                    int TX0 = 0; int TY0 = 0; int TXF = bounds.Width; int TYF = bounds.Height;
                    if (R0 != 0 & G0 != 0 & B0 != 0)
                    {

                        if (BaseColor.GetBrightness() < 0.5) //Dark Colors
                        {
                            b2 = new Pen(Color.FromArgb(R0 - 22, G0 - 11, B0));
                            b3 = new Pen(Color.FromArgb(R0 + 18, G0 + 25, B0));
                            b4 = new Pen(Color.FromArgb(R0, G0, B0));
                        }
                        else
                        {
                            b2 = new Pen(Color.FromArgb(R0 - 74, G0 - 49, B0 - 15));
                            b3 = new Pen(Color.FromArgb(R0 - 8, G0 - 24, B0 + 10));
                            b4 = new Pen(Color.FromArgb(R0, G0, B0));
                        }


                        DrawTab(TX0, TY0, TXF, TYF);
                        g.DrawPath(b2, path);
                        DrawTab(TX0 + 1, TY0 + 1, TXF - 1, TYF);
                        g.DrawPath(b3, path);
                        DrawTab(TX0 + 2, TY0 + 2, TXF - 2, TYF);
                        g.DrawPath(b4, path);

                    }
                    #endregion

                    #region Show Shadow
                    Point P0 = new Point(bounds.Right - 5, 3); Point PF = new Point(bounds.Right - 5, bounds.Height - 2);
                    Pen ps = new Pen(SetTransparency(Color.Black, 20));
                    g.DrawLine(ps, P0, PF);
                    P0 = new Point(bounds.Right - 4, 4); PF = new Point(bounds.Right - 4, bounds.Height - 1);
                    ps = new Pen(SetTransparency(Color.Black, 10));
                    g.DrawLine(ps, P0, PF);
                    #endregion

                }
                else if (tab.b_active & tab.b_selected)
                {
                    #region Show Upper Rectangle
                    Rectangle upblock = new Rectangle(8, 3, bounds.Width - 16, 4);
                    g.FillRectangle(new SolidBrush(Color.FromArgb(245, 250, 255)), upblock);
                    #endregion

                    #region Show Bottom Rectangle
                    Color CFsl = this.BaseColor;
                    Color C0sl = Color.FromArgb(CFsl.R + 19, CFsl.G + 15, CFsl.B + 10);
                    Rectangle doblock = new Rectangle(6, 3, bounds.Width - 12, bounds.Height);
                    Brush b = new LinearGradientBrush(doblock, C0sl, CFsl, LinearGradientMode.Vertical);
                    g.FillRectangle(b, doblock);
                    #endregion

                    #region Show Line Borders
                    Pen b2 = Pens.White; Pen b3 = Pens.White; Pen b4 = Pens.White;
                    int R0 = this.BaseColor.R;
                    int G0 = this.BaseColor.G;
                    int B0 = this.BaseColor.B;
                    int TX0 = 0; int TY0 = 0; int TXF = bounds.Width; int TYF = bounds.Height;
                    if (R0 != 0 & G0 != 0 & B0 != 0)
                    {

                        if (BaseColor.GetBrightness() < 0.5) //Dark Colors
                        {
                            b2 = new Pen(Color.FromArgb(R0 - 26, G0 - 14, B0 - 3));
                            b3 = new Pen(Color.FromArgb(R0 + 18, G0 + 25, B0));
                            b4 = new Pen(Color.FromArgb(R0, G0, B0));
                        }
                        else
                        {
                            b2 = new Pen(Color.FromArgb(R0 - 74, G0 - 49, B0 - 15));
                            b3 = new Pen(Color.FromArgb(R0 - 8, G0 - 24, B0 + 10));
                            b4 = new Pen(Color.FromArgb(R0, G0, B0));
                        }


                        DrawTab(TX0, TY0, TXF, TYF);
                        g.DrawPath(b2, path);
                        DrawTab(TX0 + 1, TY0 + 1, TXF - 1, TYF);
                        g.DrawPath(b3, path);
                        DrawTab(TX0 + 2, TY0 + 2, TXF - 2, TYF);
                        g.DrawPath(b4, path);

                    }
                    #endregion

                    #region Show Shadow
                    Point P0 = new Point(bounds.Right - 5, 3); Point PF = new Point(bounds.Right - 5, bounds.Height - 2);
                    Pen ps = new Pen(SetTransparency(Color.Black, 20));
                    g.DrawLine(ps, P0, PF);
                    P0 = new Point(bounds.Right - 4, 4); PF = new Point(bounds.Right - 4, bounds.Height - 1);
                    ps = new Pen(SetTransparency(Color.Black, 10));
                    g.DrawLine(ps, P0, PF);
                    #endregion

                    #region Show Halo
                    Color CH = halocolor; int c_origin = 255; CH = SetTransparency(CH, c_origin - i_opacity);
                    DrawHalo(TX0, TY0, TXF, TYF);
                    g.DrawPath(new Pen(CH), path);
                    DrawHalo(TX0 + 1, TY0 + 1, TXF + 1, TYF);
                    g.DrawPath(new Pen(CH), path);
                    #endregion

                }

            }
            else
            {
                base.OnRenderButtonBackground(e);
            }

        }





        /// <summary>
        /// Control how the border of the toolstrip draws - keep track of if there's 
        /// a selected tab and make that look "connected" by skipping drawing a line through it.
        /// </summary>
        /// <param name="e"></param>
        /*  protected override void OnRenderToolStripBorder(ToolStripRenderEventArgs e) {
             TabStrip tabStrip = e.ToolStrip as TabStrip;
           
              using (Pen outerBlueBorderPen = new Pen(Color.Red)) {
                  using (Pen innerWhiteBorderPen = new Pen(Color.Red))
                  {
                      if (tabStrip != null) {
                          if (tabStrip.SelectedTab != null) {
                              // left border coords
                              Point borderStart1 = new Point(0, tabStrip.SelectedTab.Bounds.Bottom);
                              Point borderStop1 = new Point(tabStrip.SelectedTab.Bounds.Left, tabStrip.SelectedTab.Bounds.Bottom);

                              // right border coords
                              Point borderStart2 = new Point(tabStrip.SelectedTab.Bounds.Right - 1, tabStrip.SelectedTab.Bounds.Bottom);
                              Point borderStop2 = new Point(tabStrip.ClientRectangle.Right, tabStrip.SelectedTab.Bounds.Bottom);

                              e.Graphics.DrawLine(outerBlueBorderPen, borderStart1, borderStop1);
                              e.Graphics.DrawLine(outerBlueBorderPen, borderStart2, borderStop2);

                              // shift all points down one to draw the white line
                              borderStop1.Offset(0, 1);
                              borderStart1.Offset(0, 1);
                              borderStart2.Offset(0, 1);
                              borderStop2.Offset(0, 1);
                              e.Graphics.DrawLine(innerWhiteBorderPen, borderStart1, borderStop1);
                              e.Graphics.DrawLine(innerWhiteBorderPen, borderStart2, borderStop2);

                          }
                          else {
                              e.Graphics.DrawLine(outerBlueBorderPen, new Point(0, tabStrip.DisplayRectangle.Bottom), new Point(tabStrip.Width, tabStrip.DisplayRectangle.Bottom));
                              e.Graphics.DrawLine(innerWhiteBorderPen, new Point(0, tabStrip.DisplayRectangle.Bottom + 1), new Point(tabStrip.Width, tabStrip.DisplayRectangle.Bottom + 1));
                            

                          }   

                      }
                  }
              }
            
          }
       */

        public void DrawArc(int VOff)
        {
            i_Zero = 180; X0 = X0 + D; XF = XF - D; Y0 = Y0 + VOff;
            path = new GraphicsPath();
            Point P0 = new Point(X0, YF); Point PF = new Point(X0, Y0 + T);
            path.AddLine(P0, PF);
            path.AddArc(X0, Y0, T, T, i_Zero, i_Sweep); i_Zero += 90;
            P0 = new Point(X0 + T, Y0); PF = new Point(XF - T, Y0);
            path.AddLine(P0, PF);
            path.AddArc(XF - T - 1, Y0, T, T, i_Zero, i_Sweep);
            P0 = new Point(XF - 1, Y0 + T); PF = new Point(XF - 1, YF);
            path.AddLine(P0, PF);
        }

        public void DrawTab(int _X0, int _Y0, int _XF, int _YF)
        {
            T = 6; i_Zero = 90;
            path = new GraphicsPath();
            Point P0 = new Point(_X0, _YF); Point PF = new Point(_X0 + T, _YF - T);
            path.AddArc(_X0, _YF - T, T, T, i_Zero, -i_Sweep); i_Zero = 180; //path.AddLine(P0, PF); // _/
            path.AddArc(_X0 + T, _Y0, T, T, i_Zero, i_Sweep); i_Zero = 270; // path.AddLine(P0, PF); // /-
            path.AddArc(_XF - 2 * T, _Y0, T, T, i_Zero, i_Sweep); i_Zero = 180; // path.AddLine(P0, PF); // /-
            path.AddArc(_XF - T, _YF - T, T, T, i_Zero, -i_Sweep); // path.AddLine(P0, PF); // /-
        }

        public void DrawHalo(int _X0, int _Y0, int _XF, int _YF)
        {
            path = new GraphicsPath();
            Point P0 = new Point(_X0 + 5, _YF - 3); Point PF = new Point(_X0 + 6, _Y0 + 3);
            path.AddLine(P0, PF);
            P0 = new Point(_X0 + 7, _Y0); PF = new Point(_XF - 8, _Y0);
            path.AddLine(P0, PF);
            P0 = new Point(_XF - 7, _Y0 + 3); PF = new Point(_XF - 6, _YF - 3);
            path.AddLine(P0, PF);
        }

        protected override void OnRenderToolStripBorder(ToolStripRenderEventArgs e)
        {
            //base.OnRenderToolStripBorder(e);
        }

        
    }
}