chenn
2025-04-11 e98de937b28d42493de5dea6365c853d6b412d3c
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
// *********************************
// 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.ComponentModel;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Security.Permissions;
using System.Windows.Forms.RibbonHelpers;
 
namespace System.Windows.Forms
{
    [ToolboxItem(false)]
    public class RibbonPopup
        : Control
    {
        #region Fields
 
        #endregion
 
        #region Events
 
        public event EventHandler Showed;
 
        /// <summary>
        /// Raised when the popup is closed
        /// </summary>
        public event EventHandler Closed;
 
        /// <summary>
        /// Raised when the popup is about to be closed
        /// </summary>
        public event ToolStripDropDownClosingEventHandler Closing;
 
        /// <summary>
        /// Raised when the Popup is about to be opened
        /// </summary>
        public event CancelEventHandler Opening;
 
        #endregion
 
        #region Ctor
 
        public RibbonPopup()
        {
            SetStyle(ControlStyles.Opaque, true);
            SetStyle(ControlStyles.AllPaintingInWmPaint, true);
            SetStyle(ControlStyles.UserPaint, true);
            SetStyle(ControlStyles.Selectable, false);
            BorderRoundness = 3;
        }
 
        #endregion
 
        #region Props
 
        /// <summary>
        /// Gets or sets the roundness of the border
        /// </summary>
        [Browsable(false)]
        public int BorderRoundness { get; set; }
 
 
        /// <summary>
        /// Gets the related ToolStripDropDown
        /// </summary>
        internal RibbonWrappedDropDown WrappedDropDown { get; set; }
 
        #endregion
 
        #region Methods
 
        /// <summary>
        /// Shows this Popup on the specified location of the screen
        /// </summary>
        /// <param name="screenLocation"></param>
        public void Show(Point screenLocation)
        {
            if (WrappedDropDown == null)
            {
                ToolStripControlHost host = new ToolStripControlHost(this);
                WrappedDropDown = new RibbonWrappedDropDown
                {
                    AutoClose = RibbonDesigner.Current != null
                };
                WrappedDropDown.Items.Add(host);
 
                WrappedDropDown.Padding = Padding.Empty;
                WrappedDropDown.Margin = Padding.Empty;
                host.Padding = Padding.Empty;
                host.Margin = Padding.Empty;
 
                WrappedDropDown.Opening += ToolStripDropDown_Opening;
                WrappedDropDown.Closing += ToolStripDropDown_Closing;
                WrappedDropDown.Closed += ToolStripDropDown_Closed;
                WrappedDropDown.Size = Size;
            }
            WrappedDropDown.Show(screenLocation);
            RibbonPopupManager.Register(this);
 
            OnShowed(EventArgs.Empty);
        }
 
        /// <summary>
        /// Handles the Opening event of the ToolStripDropDown
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void ToolStripDropDown_Opening(object sender, CancelEventArgs e)
        {
            OnOpening(e);
        }
 
        /// <summary>
        /// Called when pop-up is being opened
        /// </summary>
        /// <param name="e"></param>
        protected virtual void OnOpening(CancelEventArgs e)
        {
            if (Opening != null)
            {
                Opening(this, e);
            }
        }
 
        /// <summary>
        /// Handles the Closing event of the ToolStripDropDown
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void ToolStripDropDown_Closing(object sender, ToolStripDropDownClosingEventArgs e)
        {
            OnClosing(e);
        }
 
        /// <summary>
        /// Handles the closed event of the ToolStripDropDown
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void ToolStripDropDown_Closed(object sender, ToolStripDropDownClosedEventArgs e)
        {
            OnClosed(EventArgs.Empty);
        }
 
        /// <summary>
        /// Closes this popup.
        /// </summary>
        public void Close()
        {
            if (WrappedDropDown != null)
            {
                WrappedDropDown.Close();
            }
        }
 
        /// <summary>
        /// Raises the <see cref="Closing"/> event
        /// </summary>
        /// <param name="e"></param>
        protected virtual void OnClosing(ToolStripDropDownClosingEventArgs e)
        {
            if (Closing != null)
            {
                Closing(this, e);
            }
        }
 
        /// <summary>
        /// Raises the <see cref="Closed"/> event.
        /// <remarks>If you override this event don't forget to call base! Otherwise the popup will not be unregistered and hook will not work!</remarks>
        /// </summary>
        /// <param name="e"></param>
        protected virtual void OnClosed(EventArgs e)
        {
            RibbonPopupManager.Unregister(this);
 
            if (Closed != null)
            {
                Closed(this, e);
            }
 
            //if (NextPopup != null)
            //{
            //    NextPopup.CloseForward();
            //    NextPopup = null;
            //}
 
            //if (PreviousPopup != null && PreviousPopup.NextPopup.Equals(this))
            //{
            //    PreviousPopup.NextPopup = null;
            //}
        }
 
        /// <summary>
        /// Raises the Showed event
        /// </summary>
        /// <param name="e"></param>
        protected virtual void OnShowed(EventArgs e)
        {
            if (Showed != null)
            {
                Showed(this, e);
            }
        }
 
        /// <summary>
        /// Raises the <see cref="Control.Paint"/> event
        /// </summary>
        /// <param name="e"></param>
        protected override void OnPaint(PaintEventArgs e)
        {
            base.OnPaint(e);
 
            using (GraphicsPath p = RibbonProfessionalRenderer.RoundRectangle(new Rectangle(Point.Empty, Size), BorderRoundness))
            {
                using (Region r = new Region(p))
                {
                    WrappedDropDown.Region = r;
                }
            }
        }
 
        /// <summary>
        /// Overriden. Used to drop a shadow on the popup
        /// </summary>
        protected override CreateParams CreateParams
        {
            [SecurityPermission(SecurityAction.LinkDemand, Flags = SecurityPermissionFlag.UnmanagedCode)]
            get
            {
                CreateParams cp = base.CreateParams;
 
                if (WinApi.IsXP)
                {
                    cp.ClassStyle |= WinApi.CS_DROPSHADOW;
 
                }
 
                return cp;
            }
        }
 
        #endregion
    }
}