tangxu
2024-10-14 6cd995b71dfc74d4d96347d0bc535fddf36fa9df
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
// *********************************
// Message from Original Author:
//
// 2008 Jose Menendez Poo
// Please give me credit if you use this code. It's all I ask.
// Contact me for more info: menendezpoo@gmail.com
// *********************************
//
// Original project from http://ribbon.codeplex.com/
// Continue to support and maintain by http://officeribbon.codeplex.com/
 
using System.Drawing;
using System.Windows.Forms.RibbonHelpers;
 
namespace System.Windows.Forms
{
    /// <summary>
    /// Minimise, Maximise, Restore and Close buttons in the Ribbon caption area
    /// </summary>
    public class RibbonCaptionButton
       : RibbonButton
    {
        #region Subclasses
 
        /// <summary>
        /// Defines the possible caption buttons
        /// </summary>
        public enum CaptionButton
        {
            Minimize,
            Maximize,
            Restore,
            Close
        }
        #endregion
 
        #region Static
 
        public const string WindowsIconsFont = "Marlett";
 
        /// <summary>
        /// Gets the character to render the specified button type
        /// </summary>
        /// <param name="type">type of button</param>
        /// <returns>Character to use with "Marlett" font in Windows, some other representative characters when in other O.S.</returns>
        public static string GetCharFor(CaptionButton type)
        {
            if (WinApi.IsWindows)
            {
                switch (type)
                {
                    case CaptionButton.Minimize:
                        return "0";
                    case CaptionButton.Maximize:
                        return "1";
                    case CaptionButton.Restore:
                        return "2";
                    case CaptionButton.Close:
                        return "r";
                    default:
                        return "?";
                }
            }
 
            switch (type)
            {
                case CaptionButton.Minimize:
                    return "_";
                case CaptionButton.Maximize:
                    return "+";
                case CaptionButton.Restore:
                    return "^";
                case CaptionButton.Close:
                    return "X";
                default:
                    return "?";
            }
        }
        #endregion
 
        #region Fields
 
        #endregion
 
        #region Ctor
        /// <summary>
        /// Creates a new CaptionButton
        /// </summary>
        /// <param name="buttonType"></param>
        public RibbonCaptionButton(CaptionButton buttonType)
        {
            SetCaptionButtonType(buttonType);
        }
        #endregion
 
        #region Prop
        /// <summary>
        /// Gets the type of caption button this is
        /// </summary>
        public CaptionButton CaptionButtonType { get; private set; }
 
        #endregion
 
        #region Methods
 
        public override void OnClick(EventArgs e)
        {
            base.OnClick(e);
 
            Form f = Owner.FindForm();
 
            if (f == null) return;
 
            switch (CaptionButtonType)
            {
                case CaptionButton.Minimize:
                    f.WindowState = FormWindowState.Minimized;
                    break;
                case CaptionButton.Maximize:
                    if (f.WindowState == FormWindowState.Normal)
                    {
                        f.WindowState = FormWindowState.Maximized;
                        f.Refresh();
                    }
                    else
                    {
                        f.WindowState = FormWindowState.Normal;
                        f.Refresh();
                    }
                    break;
                case CaptionButton.Restore:
                    f.WindowState = FormWindowState.Normal;
                    break;
                case CaptionButton.Close:
                    f.Close();
                    break;
                default:
                    break;
            }
        }
 
        /// <summary>
        /// Sets value to the type of caption button
        /// </summary>
        /// <param name="buttonType"></param>
        internal void SetCaptionButtonType(CaptionButton buttonType)
        {
            Text = GetCharFor(buttonType);
            CaptionButtonType = buttonType;
        }
 
        internal override Rectangle OnGetTextBounds(RibbonElementSizeMode sMode, Rectangle bounds)
        {
 
            Rectangle r = bounds;
            r.X = bounds.Left + 3;
            return r;
        }
 
        #endregion
    }
}