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

Revision 968, 3.4 KB checked in by lowjoel, 5 years ago (diff)

Inflate the text rectangle a bit to prevent cropping of the text.

  • 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 : UxThemeMenuRenderer
45        {
46            protected override void Initialize(ToolStrip toolStrip)
47            {
48                base.Initialize(toolStrip);
49                owner = toolStrip;
50            }
51
52            protected override void OnRenderToolStripBackground(ToolStripRenderEventArgs e)
53            {
54                if (e.ToolStrip == owner)
55                    //Draw the parent background image. This is not portable in that it will render
56                    //this code unreusable, but for the lack of anything better this will have to suffice!
57                    e.Graphics.DrawImage(Properties.Resources.BackgroundGradient,
58                        new Point(-owner.Left, -owner.Top));
59                else
60                    base.OnRenderToolStripBackground(e);
61            }
62
63            protected override void OnRenderMenuItemBackground(ToolStripItemRenderEventArgs e)
64            {
65                if (e.ToolStrip != owner)
66                    base.OnRenderMenuItemBackground(e);
67            }
68
69            protected override void OnRenderItemText(ToolStripItemTextRenderEventArgs e)
70            {
71                if (e.ToolStrip != owner)
72                {
73                    base.OnRenderItemText(e);
74                    return;
75                }
76
77                Graphics g = e.Graphics;
78
79                //Draw the actual text
80                Rectangle tempRect = e.TextRectangle;
81                tempRect.Inflate(3, 3);
82                tempRect.Offset(3, 3);
83                e.TextRectangle = tempRect;
84                using (SolidBrush textBrush = new SolidBrush(TextColour))
85                    g.DrawString(e.Text, e.TextFont, textBrush, e.TextRectangle);
86
87                //If the text has got a selection, draw an underline
88                if (e.Item.Selected)
89                {
90                    SizeF textSize = g.MeasureString(e.Text, e.TextFont);
91                    using (Pen underlinePen = new Pen(TextColour))
92                    {
93                        Point underlineStart = e.TextRectangle.Location;
94                        underlineStart.Offset(0, Point.Truncate(textSize.ToPointF()).Y);
95                        Point underlineEnd = underlineStart;
96                        underlineEnd.Offset(e.TextRectangle.Width, 0);
97
98                        g.DrawLine(underlinePen, underlineStart, underlineEnd);
99                    }
100                }
101            }
102
103            /// <summary>
104            /// The margin between a drop-down arrow and the surrounding items.
105            /// </summary>
106            private const int ArrowMargin = 0;
107
108            /// <summary>
109            /// The colour of the menu bar text.
110            /// </summary>
111            private readonly Color TextColour = Color.White;
112
113            private ToolStrip owner;
114        }
115    }
116}
Note: See TracBrowser for help on using the repository browser.