source: branches/eraser6/Eraser/ToolBar.cs @ 970

Revision 970, 3.2 KB checked in by lowjoel, 6 years ago (diff)

-Separated the ToolBar? drop-down menus from the toolbar itself - I didn't want to use the same renderer because the renderer will have to implement menu item rendering too which is what UxThemeMenuRenderer? does. But that only works on Vista+
-Fixed all the overloads for menus in UpdateControlTheme?. Menus/toolbars all use the same interface now.

  • Property svn:keywords set to Id
Line 
1/*
2 * $Id$
3 * Copyright 2008 The Eraser Project
4 * Original Author: Joel Low <lowjoel@users.sourceforge.net>
5 * Modified By:
6 *
7 * This file is part of Eraser.
8 *
9 * Eraser is free software: you can redistribute it and/or modify it under the
10 * terms of the GNU General Public License as published by the Free Software
11 * Foundation, either version 3 of the License, or (at your option) any later
12 * version.
13 *
14 * Eraser is distributed in the hope that it will be useful, but WITHOUT ANY
15 * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
16 * A PARTICULAR PURPOSE. See the GNU General Public License for more details.
17 *
18 * A copy of the GNU General Public License can be found at
19 * <http://www.gnu.org/licenses/>.
20 */
21
22using System;
23using System.Collections.Generic;
24using System.ComponentModel;
25using System.Data;
26using System.Drawing;
27using System.Text;
28using System.Windows.Forms;
29using System.Runtime.InteropServices;
30using System.Collections.ObjectModel;
31using Eraser.Util;
32
33namespace Eraser
34{
35    public partial class ToolBar : System.Windows.Forms.MenuStrip
36    {
37        public ToolBar()
38        {
39            //Create the base component
40            InitializeComponent();
41            Renderer = new EraserToolStripRenderer();
42        }
43
44        private class EraserToolStripRenderer : ToolStripRenderer
45        {
46            protected override void Initialize(ToolStrip toolStrip)
47            {
48                base.Initialize(toolStrip);
49                owner = toolStrip;
50            }
51
52            protected override void InitializeItem(ToolStripItem item)
53            {
54                base.InitializeItem(item);
55            }
56
57            protected override void OnRenderToolStripBackground(ToolStripRenderEventArgs e)
58            {
59                //Draw the parent background image. This is not portable in that it will render
60                //this code unreusable, but for the lack of anything better this will have to suffice!
61                e.Graphics.DrawImage(Properties.Resources.BackgroundGradient,
62                    new Point(-owner.Left, -owner.Top));
63            }
64
65            protected override void OnRenderItemText(ToolStripItemTextRenderEventArgs e)
66            {
67                Graphics g = e.Graphics;
68
69                //Draw the actual text
70                Rectangle tempRect = e.TextRectangle;
71                tempRect.Inflate(3, 3);
72                tempRect.Offset(3, 3);
73                e.TextRectangle = tempRect;
74                using (SolidBrush textBrush = new SolidBrush(TextColour))
75                    g.DrawString(e.Text, e.TextFont, textBrush, e.TextRectangle);
76
77                //If the text has got a selection, draw an underline
78                if (e.Item.Selected)
79                {
80                    SizeF textSize = g.MeasureString(e.Text, e.TextFont);
81                    using (Pen underlinePen = new Pen(TextColour))
82                    {
83                        Point underlineStart = e.TextRectangle.Location;
84                        underlineStart.Offset(0, Point.Truncate(textSize.ToPointF()).Y);
85                        Point underlineEnd = underlineStart;
86                        underlineEnd.Offset(e.TextRectangle.Width, 0);
87
88                        g.DrawLine(underlinePen, underlineStart, underlineEnd);
89                    }
90                }
91            }
92
93            /// <summary>
94            /// The margin between a drop-down arrow and the surrounding items.
95            /// </summary>
96            private const int ArrowMargin = 0;
97
98            /// <summary>
99            /// The colour of the menu bar text.
100            /// </summary>
101            private readonly Color TextColour = Color.White;
102
103            /// <summary>
104            /// The toolstrip using this renderer.
105            /// </summary>
106            private ToolStrip owner;
107        }
108    }
109}
Note: See TracBrowser for help on using the repository browser.