// ********************************* // 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; /// /// Raised when the popup is closed /// public event EventHandler Closed; /// /// Raised when the popup is about to be closed /// public event ToolStripDropDownClosingEventHandler Closing; /// /// Raised when the Popup is about to be opened /// 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 /// /// Gets or sets the roundness of the border /// [Browsable(false)] public int BorderRoundness { get; set; } /// /// Gets the related ToolStripDropDown /// internal RibbonWrappedDropDown WrappedDropDown { get; set; } #endregion #region Methods /// /// Shows this Popup on the specified location of the screen /// /// 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); } /// /// Handles the Opening event of the ToolStripDropDown /// /// /// private void ToolStripDropDown_Opening(object sender, CancelEventArgs e) { OnOpening(e); } /// /// Called when pop-up is being opened /// /// protected virtual void OnOpening(CancelEventArgs e) { if (Opening != null) { Opening(this, e); } } /// /// Handles the Closing event of the ToolStripDropDown /// /// /// private void ToolStripDropDown_Closing(object sender, ToolStripDropDownClosingEventArgs e) { OnClosing(e); } /// /// Handles the closed event of the ToolStripDropDown /// /// /// private void ToolStripDropDown_Closed(object sender, ToolStripDropDownClosedEventArgs e) { OnClosed(EventArgs.Empty); } /// /// Closes this popup. /// public void Close() { if (WrappedDropDown != null) { WrappedDropDown.Close(); } } /// /// Raises the event /// /// protected virtual void OnClosing(ToolStripDropDownClosingEventArgs e) { if (Closing != null) { Closing(this, e); } } /// /// Raises the event. /// If you override this event don't forget to call base! Otherwise the popup will not be unregistered and hook will not work! /// /// 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; //} } /// /// Raises the Showed event /// /// protected virtual void OnShowed(EventArgs e) { if (Showed != null) { Showed(this, e); } } /// /// Raises the event /// /// 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; } } } /// /// Overriden. Used to drop a shadow on the popup /// 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 } }