using System; using System.Text; using System.Drawing; using System.Drawing.Drawing2D; using System.Windows.Forms; using System.ComponentModel; using Demo.WindowsForms.Properties; namespace BSE.Windows.Forms { /// /// Base class for the panel or xpanderpanel control. /// /// Copyright © 2006-2008 Uwe Eichkorn /// THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY /// KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE /// IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A PARTICULAR /// PURPOSE. IT CAN BE DISTRIBUTED FREE OF CHARGE AS LONG AS THIS HEADER /// REMAINS UNCHANGED. /// [DesignTimeVisibleAttribute(false)] public class BasePanel : ScrollableControl, IPanel { #region Constants /// /// padding value for the panel /// public const int CaptionSpacing = 6; #endregion #region Events /// /// Occurs when the close icon in the caption of the panel or xpanderpanel is clicked. /// [Description("Occurs when the close icon in the caption of the panel or xpanderpanel is clicked.")] public event EventHandler CloseClick; /// /// Occurs when the expand icon in the caption of the panel or xpanderpanel is clicked. /// [Description("Occurs when the expand icon in the caption of the panel or xpanderpanel is clicked.")] public event EventHandler ExpandClick; /// /// Occurs when the panel or xpanderpanel expands. /// [Description("Occurs when the panel or xpanderpanel expands.")] public event EventHandler PanelExpanding; /// /// Occurs when the panel or xpanderpanel collapse. /// [Description("Occurs when the panel or xpanderpanel collapse.")] public event EventHandler PanelCollapsing; /// /// The PanelStyleChanged event occurs when PanelStyle flags have been changed. /// [Description("The PanelStyleChanged event occurs when PanelStyle flags have been changed.")] public event EventHandler PanelStyleChanged; /// /// The ColorSchemeChanged event occurs when ColorScheme flags have been changed. /// [Description("The ColorSchemeChanged event occurs when ColorScheme flags have been changed.")] public event EventHandler ColorSchemeChanged; /// /// Occurs when the value of the CustomColors property changes. /// [Description("Occurs when the value of the CustomColors property changes.")] public event EventHandler CustomColorsChanged; /// /// Occurs when the value of the CaptionHeight property changes. /// [Description("Occurs when the value of the CaptionHeight property changes.")] public event EventHandler CaptionHeightChanged; /// /// Occurs when the value of the CaptionBar HoverState changes. /// [Description("Occurs when the value of the CaptionBar HoverState changes.")] public event EventHandler CaptionBarHoverStateChanged; /// /// Occurs when the value of the CloseIcon HoverState changes. /// [Description("Occurs when the value of the CloseIcon HoverState changes.")] protected event EventHandler CloseIconHoverStateChanged; /// /// Occurs when the value of the ExpandIcon HoverState changes. /// [Description("Occurs when the value of the ExpandIcon HoverState changes.")] protected event EventHandler ExpandIconHoverStateChanged; #endregion #region FieldsPrivate private int m_iCaptionHeight; private Font m_captionFont; private Rectangle m_imageRectangle; private bool m_bShowBorder; private bool m_bExpand; private Size m_imageSize; private BSE.Windows.Forms.ColorScheme m_eColorScheme; private PanelColors m_panelColors; private PanelStyle m_ePanelStyle; private Image m_image; private HoverState m_hoverStateCaptionBar; private HoverState m_hoverStateExpandIcon; private string m_strToolTipTextExpandIconPanelExpanded; private string m_strToolTipTextExpandIconPanelCollapsed; private HoverState m_hoverStateCloseIcon; private string m_strToolTipTextCloseIcon; private bool m_bShowExpandIcon; private bool m_bShowCloseIcon; private System.Windows.Forms.ToolTip m_toolTip; #endregion #region FieldsProtected /// /// The rectangle that contains the expand panel icon /// protected Rectangle RectangleExpandIcon = Rectangle.Empty; /// /// The rectangle that contains the close panel icon /// protected Rectangle RectangleCloseIcon = Rectangle.Empty; #endregion #region Properties /// /// Gets or sets the style of the panel. /// [Description("Style of the panel")] [DefaultValue(0)] [Category("Appearance")] public virtual BSE.Windows.Forms.PanelStyle PanelStyle { get { return this.m_ePanelStyle; } set { if(value.Equals(this.m_ePanelStyle) == false) { this.m_ePanelStyle = value; OnPanelStyleChanged(this, new PanelStyleChangeEventArgs(this.m_ePanelStyle)); } } } /// /// Gets or sets the image that is displayed on a Panels caption. /// [Description("Gets or sets the image that is displayed on a Panels caption.")] [Category("Appearance")] public Image Image { get { return this.m_image; } set { if(value != this.m_image) { this.m_image = value; this.Invalidate(this.CaptionRectangle); } } } /// /// Gets or sets the color schema which is used for the panel. /// [Description("ColorScheme of the Panel")] [DefaultValue(BSE.Windows.Forms.ColorScheme.Professional)] [Browsable(true)] [Category("Appearance")] public virtual BSE.Windows.Forms.ColorScheme ColorScheme { get { return this.m_eColorScheme; } set { if(value.Equals(this.m_eColorScheme) == false) { this.m_eColorScheme = value; OnColorSchemeChanged(this, new ColorSchemeChangeEventArgs(this.m_eColorScheme)); } } } /// /// Gets or sets the height of the panels caption. /// [Description("Gets or sets the height of the panels caption."), DefaultValue(25), Category("Appearance")] public int CaptionHeight { get { return m_iCaptionHeight; } set { if(value < Constants.CaptionMinHeight) { throw new InvalidOperationException( string.Format( System.Globalization.CultureInfo.CurrentUICulture, Resources.IDS_InvalidOperationExceptionInteger, value, "CaptionHeight", Constants.CaptionMinHeight)); } this.m_iCaptionHeight = value; OnCaptionHeightChanged(this, EventArgs.Empty); } } /// /// Gets or sets the font of the text displayed on the caption. /// [Description("Gets or sets the font of the text displayed on the caption.")] [DefaultValue(typeof(Font), "Microsoft Sans Serif; 8,25pt; style=Bold")] [Category("Appearance")] public Font CaptionFont { get { return this.m_captionFont; } set { if(value != null) { if(value.Equals(this.m_captionFont) == false) { this.m_captionFont = value; this.Invalidate(this.CaptionRectangle); } } } } /// /// Expands the panel or xpanderpanel. /// [Description("Expand the panel or xpanderpanel")] [DefaultValue(false)] [Category("Appearance")] [RefreshProperties(RefreshProperties.Repaint)] public virtual bool Expand { get { return this.m_bExpand; } set { if(value.Equals(this.m_bExpand) == false) { this.m_bExpand = value; if(this.m_bExpand == true) { OnPanelExpanding(this, new XPanderStateChangeEventArgs(this.m_bExpand)); } else { OnPanelCollapsing(this, new XPanderStateChangeEventArgs(this.m_bExpand)); } } } } /// /// Gets or sets a value indicating whether the control shows a border. /// [Description("Gets or sets a value indicating whether the control shows a border")] [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)] [DefaultValue(true)] [Browsable(false)] [Category("Appearance")] public virtual bool ShowBorder { get { return this.m_bShowBorder; } set { if(value.Equals(this.m_bShowBorder) == false) { this.m_bShowBorder = value; this.Invalidate(false); } } } /// /// Gets or sets a value indicating whether the expand icon in a Panel or XPanderPanel is visible. /// [Description("Gets or sets a value indicating whether the expand icon in a Panel or XPanderPanel is visible.")] [DefaultValue(false)] [Category("Appearance")] public virtual bool ShowExpandIcon { get { return this.m_bShowExpandIcon; } set { if(value.Equals(this.m_bShowExpandIcon) == false) { this.m_bShowExpandIcon = value; this.Invalidate(false); } } } /// /// Gets or sets the text that appears as a ToolTip on a panel when the mouse /// movers over the closeicon. /// /// /// Type: /// A string representing the ToolTip text when the mouse moves over the closeicon. /// [Description("Specifies the text to show on a ToolTip when the mouse moves over the closeicon.")] [Category("Behavior")] public virtual string ToolTipTextCloseIcon { get { return this.m_strToolTipTextCloseIcon; } set { this.m_strToolTipTextCloseIcon = value; } } /// /// Gets or sets the text that appears as a ToolTip on a panel when the mouse /// movers over the expandicon and the panel is collapsed. /// /// /// Type: /// A string representing the ToolTip text. /// [Description("Specifies the text to show on a ToolTip when the mouse moves over the expandicon and the panel is collapsed.")] [Category("Behavior")] public virtual string ToolTipTextExpandIconPanelCollapsed { get { return this.m_strToolTipTextExpandIconPanelCollapsed; } set { this.m_strToolTipTextExpandIconPanelCollapsed = value; } } /// /// Gets or sets the text that appears as a ToolTip on a panel when the mouse /// movers over the expandicon and the panel is expanded. /// /// /// Type: /// A string representing the ToolTip text when the mouse moves over the expandicon and the panel is expanded. /// [Description("Specifies the text to show on a ToolTip when the mouse moves over the expandicon and the panel is expanded.")] [Category("Behavior")] public virtual string ToolTipTextExpandIconPanelExpanded { get { return this.m_strToolTipTextExpandIconPanelExpanded; } set { this.m_strToolTipTextExpandIconPanelExpanded = value; } } /// /// Gets or sets a value indicating whether the close icon in a Panel or XPanderPanel is visible. /// [Description("Gets or sets a value indicating whether the close icon in a Panel or XPanderPanel is visible.")] [DefaultValue(false)] [Category("Appearance")] public virtual bool ShowCloseIcon { get { return this.m_bShowCloseIcon; } set { if(value.Equals(this.m_bShowCloseIcon) == false) { this.m_bShowCloseIcon = value; this.Invalidate(false); } } } /// /// Gets the panelcolors table. /// protected PanelColors PanelColors { get { return m_panelColors; } } /// /// Gets or sets the HoverState of the CaptionBar at a Panel or XPanderPanel. /// internal HoverState HoverStateCaptionBar { get { return this.m_hoverStateCaptionBar; } set { this.m_hoverStateCaptionBar = value; } } /// /// Gets or sets the HoverState of the CloseIcon in a captionbar at a Panel or XPanderPanel. /// internal HoverState HoverStateCloseIcon { get { return this.m_hoverStateCloseIcon; } set { this.m_hoverStateCloseIcon = value; } } /// /// Gets or sets the HoverState of the ExpandIcon in a captionbar at a Panel or XPanderPanel. /// internal HoverState HoverStateExpandIcon { get { return this.m_hoverStateExpandIcon; } set { this.m_hoverStateExpandIcon = value; } } /// /// Gets or sets the size of an image in the captionbar. /// internal Size ImageSize { get { return this.m_imageSize; } set { this.m_imageSize = value; } } /// /// Gets the size of a captionbar in a Panel or XPanderPanel /// internal Rectangle CaptionRectangle { get { return new Rectangle(0, 0, this.ClientRectangle.Width, this.CaptionHeight); } } /// /// Gets the Rectangle of an Image in a captionbar /// internal Rectangle ImageRectangle { get { if(this.m_imageRectangle == Rectangle.Empty) { this.m_imageRectangle = new Rectangle( CaptionSpacing, this.CaptionHeight, this.m_imageSize.Width, this.m_imageSize.Height); } return this.m_imageRectangle; } } #endregion #region MethodsPublic /// /// Sets the PanelProperties for a Panel or XPanderPanel /// /// The PanelColors table public virtual void SetPanelProperties(PanelColors panelColors) { if(panelColors == null) { throw new ArgumentException( string.Format( System.Globalization.CultureInfo.CurrentUICulture, Resources.IDS_ArgumentException, "panelColors")); } this.m_panelColors = panelColors; this.ColorScheme = ColorScheme.Professional; this.Invalidate(true); } #endregion #region MethodsProtected /// /// Initializes a new instance of the BasePanel class. /// protected BasePanel() { this.SetStyle(ControlStyles.ResizeRedraw, true); this.SetStyle(ControlStyles.AllPaintingInWmPaint, true); this.SetStyle(ControlStyles.UserPaint, true); this.SetStyle(ControlStyles.DoubleBuffer, true); this.SetStyle(ControlStyles.SupportsTransparentBackColor, true); this.SetStyle(ControlStyles.ContainerControl, true); this.CaptionFont = new Font(SystemFonts.CaptionFont.FontFamily, SystemFonts.CaptionFont.SizeInPoints - 1.0F, FontStyle.Bold); this.CaptionHeight = 25; this.PanelStyle = PanelStyle.Default; this.m_panelColors = new PanelColors(this); this.m_imageSize = new Size(16, 16); this.m_imageRectangle = Rectangle.Empty; this.m_toolTip = new System.Windows.Forms.ToolTip(); } /// /// Raises the TextChanged event. /// /// An EventArgs that contains the event data. protected override void OnTextChanged(EventArgs e) { this.Invalidate(this.CaptionRectangle); base.OnTextChanged(e); } /// /// Raises the ColorScheme changed event /// /// The source of the event. /// A EventArgs that contains the event data. protected virtual void OnColorSchemeChanged(object sender, ColorSchemeChangeEventArgs e) { this.PanelColors.Clear(); this.Invalidate(false); if(this.ColorSchemeChanged != null) { this.ColorSchemeChanged(sender, e); } } /// /// Raises the MouseUp event /// /// A MouseEventArgs that contains data about the OnMouseUp event. protected override void OnMouseUp(MouseEventArgs e) { if((this.ShowExpandIcon == true) && (this.RectangleExpandIcon.Contains(e.X, e.Y) == true)) { OnExpandClick(this, EventArgs.Empty); } if((this.ShowCloseIcon == true) && (this.RectangleCloseIcon.Contains(e.X, e.Y) == true)) { OnCloseClick(this, EventArgs.Empty); } base.OnMouseUp(e); } /// /// Raises the MouseMove event /// /// A MouseEventArgs that contains the event data. protected override void OnMouseMove(MouseEventArgs e) { if(this.CaptionRectangle.Contains(e.X, e.Y) == true) { if(this.m_hoverStateCaptionBar == HoverState.None) { this.m_hoverStateCaptionBar = HoverState.Hover; OnCaptionBarHoverStateChanged(this, new HoverStateChangeEventArgs(this.m_hoverStateCaptionBar)); } } else { if(this.m_hoverStateCaptionBar == HoverState.Hover) { this.m_hoverStateCaptionBar = HoverState.None; OnCaptionBarHoverStateChanged(this, new HoverStateChangeEventArgs(this.m_hoverStateCaptionBar)); } } if((this.ShowExpandIcon == true) || (this.ShowCloseIcon == true)) { if(this.RectangleExpandIcon.Contains(e.X, e.Y) == true) { if(this.m_hoverStateExpandIcon == HoverState.None) { this.m_hoverStateExpandIcon = HoverState.Hover; OnExpandIconHoverStateChanged(this, new HoverStateChangeEventArgs(this.m_hoverStateExpandIcon)); } } else { if(this.m_hoverStateExpandIcon == HoverState.Hover) { this.m_hoverStateExpandIcon = HoverState.None; OnExpandIconHoverStateChanged(this, new HoverStateChangeEventArgs(this.m_hoverStateExpandIcon)); } } if(this.RectangleCloseIcon.Contains(e.X, e.Y) == true) { if(this.m_hoverStateCloseIcon == HoverState.None) { this.m_hoverStateCloseIcon = HoverState.Hover; OnCloseIconHoverStateChanged(this, new HoverStateChangeEventArgs(this.m_hoverStateCloseIcon)); } } else { if(this.m_hoverStateCloseIcon == HoverState.Hover) { this.m_hoverStateCloseIcon = HoverState.None; OnCloseIconHoverStateChanged(this, new HoverStateChangeEventArgs(this.m_hoverStateCloseIcon)); } } } base.OnMouseMove(e); } /// /// Raises the MouseLeave event. /// /// An EventArgs that contains the event data. protected override void OnMouseLeave(EventArgs e) { if(this.m_hoverStateCaptionBar == HoverState.Hover) { this.m_hoverStateCaptionBar = HoverState.None; OnCaptionBarHoverStateChanged(this, new HoverStateChangeEventArgs(this.m_hoverStateCaptionBar)); } if(this.m_hoverStateExpandIcon == HoverState.Hover) { this.m_hoverStateExpandIcon = HoverState.None; OnExpandIconHoverStateChanged(this, new HoverStateChangeEventArgs(this.m_hoverStateExpandIcon)); } if(this.m_hoverStateCloseIcon == HoverState.Hover) { this.m_hoverStateCloseIcon = HoverState.None; OnCloseIconHoverStateChanged(this, new HoverStateChangeEventArgs(this.m_hoverStateCloseIcon)); } base.OnMouseLeave(e); } /// /// Raises the PanelExpanding event. /// /// The source of the event. /// A XPanderStateChangeEventArgs that contains the event data. protected virtual void OnPanelExpanding(object sender, XPanderStateChangeEventArgs e) { if(this.PanelExpanding != null) { this.PanelExpanding(sender, e); } } /// /// Raises the PanelCollapsing event. /// /// The source of the event. /// A XPanderStateChangeEventArgs that contains the event data. protected virtual void OnPanelCollapsing(object sender, XPanderStateChangeEventArgs e) { if(this.PanelCollapsing != null) { this.PanelCollapsing(sender, e); } } /// /// Raises the PanelStyle changed event. /// /// The source of the event. /// A PanelStyleChangeEventArgs that contains the event data. protected virtual void OnPanelStyleChanged(object sender, PanelStyleChangeEventArgs e) { PanelStyle panelStyle = e.PanelStyle; switch(panelStyle) { case PanelStyle.Default: m_panelColors = new PanelColors(this); break; case PanelStyle.Office2007: m_panelColors = new PanelColorsOffice2007Blue(this); break; } Invalidate(true); if(this.PanelStyleChanged != null) { this.PanelStyleChanged(sender, e); } } /// /// Raises the CloseClick event. /// /// The source of the event. /// An EventArgs that contains the event data. protected virtual void OnCloseClick(object sender, EventArgs e) { if(this.CloseClick != null) { this.CloseClick(sender, e); } } /// /// Raises the ExpandClick event. /// /// The source of the event. /// An EventArgs that contains the event data. protected virtual void OnExpandClick(object sender, EventArgs e) { this.Invalidate(false); if(this.ExpandClick != null) { this.ExpandClick(sender, e); } } /// /// Raises the ExpandIconHoverState changed event. /// /// The source of the event. /// A HoverStateChangeEventArgs that contains the event data. protected virtual void OnExpandIconHoverStateChanged(object sender, HoverStateChangeEventArgs e) { if(e.HoverState == HoverState.Hover) { if(this.Cursor != Cursors.Hand) { this.Cursor = Cursors.Hand; if(this.Expand == true) { if(this is Panel) { if(string.IsNullOrEmpty(this.m_strToolTipTextExpandIconPanelExpanded) == false) { this.m_toolTip.SetToolTip(this, this.m_strToolTipTextExpandIconPanelExpanded); } } } else { if(string.IsNullOrEmpty(this.m_strToolTipTextExpandIconPanelCollapsed) == false) { this.m_toolTip.SetToolTip(this, this.m_strToolTipTextExpandIconPanelCollapsed); } } } } else { if(this.Cursor == Cursors.Hand) { this.m_toolTip.SetToolTip(this, string.Empty); this.m_toolTip.Hide(this); this.Cursor = Cursors.Default; } } if(this.ExpandIconHoverStateChanged != null) { this.ExpandIconHoverStateChanged(sender, e); } } /// /// Raises the CaptionHeight changed event. /// /// The source of the event. /// A EventArgs that contains the event data. protected virtual void OnCaptionHeightChanged(object sender, EventArgs e) { OnLayout(new LayoutEventArgs(this, null)); this.Invalidate(false); if(this.CaptionHeightChanged != null) { this.CaptionHeightChanged(sender, e); } } /// /// Raises the CaptionBarHoverState changed event. /// /// The source of the event. /// A HoverStateChangeEventArgs that contains the event data. protected virtual void OnCaptionBarHoverStateChanged(object sender, HoverStateChangeEventArgs e) { if(this is XPanderPanel) { if(e.HoverState == HoverState.Hover) { if((this.ShowCloseIcon == false) && (this.ShowExpandIcon == false)) { if(this.Cursor != Cursors.Hand) { this.Cursor = Cursors.Hand; } } } else { if(this.Cursor == Cursors.Hand) { this.Cursor = Cursors.Default; } } this.Invalidate(CaptionRectangle); } if(this.CaptionBarHoverStateChanged != null) { this.CaptionBarHoverStateChanged(sender, e); } } /// /// Raises the CloseIconHoverState changed event. /// /// The source of the event. /// A HoverStateChangeEventArgs that contains the event data. protected virtual void OnCloseIconHoverStateChanged(object sender, HoverStateChangeEventArgs e) { if(e.HoverState == HoverState.Hover) { if(this.Cursor != Cursors.Hand) { this.Cursor = Cursors.Hand; } if(string.IsNullOrEmpty(this.m_strToolTipTextCloseIcon) == false) { this.m_toolTip.SetToolTip(this, this.m_strToolTipTextCloseIcon); } } else { if(this.Cursor == Cursors.Hand) { this.m_toolTip.SetToolTip(this, string.Empty); this.m_toolTip.Hide(this); this.Cursor = Cursors.Default; } } if(this.CloseIconHoverStateChanged != null) { this.CloseIconHoverStateChanged(sender, e); } } /// /// Raises the CustomColors changed event. /// /// The source of the event. /// A EventArgs that contains the event data. protected virtual void OnCustomColorsChanged(object sender, EventArgs e) { if(this.ColorScheme == ColorScheme.Custom) { this.PanelColors.Clear(); this.Invalidate(false); } if(this.CustomColorsChanged != null) { this.CustomColorsChanged(sender, e); } } /// /// Draws the specified text string on the specified caption surface; within the specified bounds; and in the specified font, color. /// /// The Graphics to draw on. /// Rectangle structure that specifies the location of the drawn text. /// Font that defines the text format of the string. /// The color of the string /// String to draw. /// Gets or sets a value indicating whether control's elements are aligned to support locales using right-to-left fonts. /// The alignment of a text string relative to its layout rectangle. protected static void DrawString( Graphics graphics, RectangleF layoutRectangle, Font font, Color fontColor, string strText, RightToLeft rightToLeft, StringAlignment stringAlignment) { if(graphics == null) { throw new ArgumentException( string.Format( System.Globalization.CultureInfo.CurrentUICulture, Resources.IDS_ArgumentException, typeof(Graphics).Name)); } using(SolidBrush stringBrush = new SolidBrush(fontColor)) { using(StringFormat stringFormat = new StringFormat()) { stringFormat.FormatFlags = StringFormatFlags.NoWrap; if(rightToLeft == RightToLeft.Yes) { stringFormat.FormatFlags |= StringFormatFlags.DirectionRightToLeft; } stringFormat.Trimming = StringTrimming.EllipsisCharacter; stringFormat.LineAlignment = StringAlignment.Center; stringFormat.Alignment = stringAlignment; graphics.DrawString(strText, font, stringBrush, layoutRectangle, stringFormat); } } } /// /// Draws the icon image at the specified location. /// /// The Graphics to draw on. /// icon image to draw. /// A rectangle structure that specifies the bounds of the linear gradient. /// The foreground color of this image /// The vertical position for the icon image protected static void DrawIcon(Graphics graphics, Image imgPanelIcon, Rectangle imageRectangle, Color foreColorImage, int iconPositionY) { if(graphics == null) { throw new ArgumentException( string.Format( System.Globalization.CultureInfo.CurrentUICulture, Resources.IDS_ArgumentException, typeof(Graphics).Name)); } if(imgPanelIcon == null) { throw new ArgumentException( string.Format( System.Globalization.CultureInfo.CurrentUICulture, Resources.IDS_ArgumentException, typeof(Image).Name)); } int iconPositionX = imageRectangle.Left; int iconWidth = imgPanelIcon.Width; int iconHeight = imgPanelIcon.Height; Rectangle rectangleIcon = new Rectangle( iconPositionX + (iconWidth / 2) - 1, iconPositionY + (iconHeight / 2) - 1, imgPanelIcon.Width, imgPanelIcon.Height - 1); using(System.Drawing.Imaging.ImageAttributes imageAttribute = new System.Drawing.Imaging.ImageAttributes()) { imageAttribute.SetColorKey(Color.Magenta, Color.Magenta); System.Drawing.Imaging.ColorMap colorMap = new System.Drawing.Imaging.ColorMap(); colorMap.OldColor = Color.FromArgb(0, 60, 166); colorMap.NewColor = foreColorImage; imageAttribute.SetRemapTable(new System.Drawing.Imaging.ColorMap[] { colorMap }); graphics.DrawImage(imgPanelIcon, rectangleIcon, 0, 0, iconWidth, iconHeight, GraphicsUnit.Pixel, imageAttribute); } } /// /// Draws the specified Image at the specified location. /// /// The Graphics to draw on. /// Image to draw. /// Rectangle structure that specifies the location and size of the drawn image. protected static void DrawImage(Graphics graphics, Image image, Rectangle imageRectangle) { if(graphics == null) { throw new ArgumentNullException( string.Format( System.Globalization.CultureInfo.CurrentUICulture, Resources.IDS_ArgumentException, typeof(Graphics).Name)); } if(image != null) { graphics.DrawImage(image, imageRectangle); } } /// /// Draws the text and image objects at the specified location. /// /// The Graphics to draw on. /// The drawing rectangle on a panel's caption. /// The spacing on a panel's caption /// The rectangle of an image displayed on a panel's caption. /// The image that is displayed on a panel's caption. /// A value indicating whether control's elements are aligned to support locales using right-to-left fonts. /// The font of the text displayed on a panel's caption. /// The foreground color of the text displayed on a panel's caption. /// The text which is associated with this caption. protected static void DrawImagesAndText( Graphics graphics, Rectangle captionRectangle, int iSpacing, Rectangle imageRectangle, Image image, RightToLeft rightToLeft, Font fontCaption, Color captionForeColor, string strCaptionText) { //DrawImages int iTextPositionX1 = iSpacing; int iTextPositionX2 = captionRectangle.Right - iSpacing; imageRectangle.Y = (captionRectangle.Height - imageRectangle.Height) / 2; if(rightToLeft == RightToLeft.No) { if(image != null) { DrawImage(graphics, image, imageRectangle); iTextPositionX1 += imageRectangle.Width + iSpacing; } } // // Draw Caption text // Rectangle textRectangle = captionRectangle; textRectangle.X = iTextPositionX1; textRectangle.Width -= iTextPositionX1 + iSpacing; if(rightToLeft == RightToLeft.Yes) { if(image != null) { Rectangle imageRectangleRight = imageRectangle; imageRectangleRight.X = iTextPositionX2 - imageRectangle.Width; DrawImage(graphics, image, imageRectangleRight); iTextPositionX2 = imageRectangleRight.X - iSpacing; } } textRectangle.Width = iTextPositionX2 - iTextPositionX1; DrawString(graphics, textRectangle, fontCaption, captionForeColor, strCaptionText, rightToLeft, StringAlignment.Near); } /// /// Draws the text and image objects at the specified location. /// /// The Graphics to draw on. /// The drawing rectangle on a panel's caption. /// The spacing on a panel's caption /// The rectangle of an image displayed on a panel's caption. /// The image that is displayed on a panel's caption. /// A value indicating whether control's elements are aligned to support locales using right-to-left fonts. /// A value indicating whether the xpanderpanel is closable /// A value indicating whether the close image is displayed /// The close image that is displayed on a panel's caption. /// The foreground color of the close image that is displayed on a panel's caption. /// The rectangle of the close image that is displayed on a panel's caption. /// A value indicating whether the expand image is displayed /// The expand image that is displayed on a panel's caption. /// The foreground color of the expand image displayed by this caption. /// the rectangle of the expand image displayed by this caption. /// The font of the text displayed on a panel's caption. /// The foreground color of the text displayed on a panel's caption. /// The text which is associated with this caption. protected static void DrawImagesAndText( Graphics graphics, Rectangle captionRectangle, int iSpacing, Rectangle imageRectangle, Image image, RightToLeft rightToLeft, bool bIsClosable, bool bShowCloseIcon, Image imageClosePanel, Color foreColorCloseIcon, ref Rectangle rectangleImageClosePanel, bool bShowExpandIcon, Image imageExandPanel, Color foreColorExpandIcon, ref Rectangle rectangleImageExandPanel, Font fontCaption, Color captionForeColor, string strCaptionText) { //DrawImages int iTextPositionX1 = iSpacing; int iTextPositionX2 = captionRectangle.Right - iSpacing; imageRectangle.Y = (captionRectangle.Height - imageRectangle.Height) / 2; if(rightToLeft == RightToLeft.No) { if(image != null) { DrawImage(graphics, image, imageRectangle); iTextPositionX1 += imageRectangle.Width + iSpacing; iTextPositionX2 -= iTextPositionX1; } } else { if((bShowCloseIcon == true) && (imageClosePanel != null)) { rectangleImageClosePanel = imageRectangle; rectangleImageClosePanel.X = imageRectangle.X; if(bIsClosable == true) { DrawIcon(graphics, imageClosePanel, rectangleImageClosePanel, foreColorCloseIcon, imageRectangle.Y); } iTextPositionX1 = rectangleImageClosePanel.X + rectangleImageClosePanel.Width; } if((bShowExpandIcon == true) && (imageExandPanel != null)) { rectangleImageExandPanel = imageRectangle; rectangleImageExandPanel.X = imageRectangle.X; if((bShowCloseIcon == true) && (imageClosePanel != null)) { rectangleImageExandPanel.X = iTextPositionX1 + (iSpacing / 2); } DrawIcon(graphics, imageExandPanel, rectangleImageExandPanel, foreColorExpandIcon, imageRectangle.Y); iTextPositionX1 = rectangleImageExandPanel.X + rectangleImageExandPanel.Width; } } // // Draw Caption text // RectangleF textRectangle = captionRectangle; textRectangle.X = iTextPositionX1; textRectangle.Width -= iTextPositionX1 + iSpacing; if(rightToLeft == RightToLeft.No) { if((bShowCloseIcon == true) && (imageClosePanel != null)) { rectangleImageClosePanel = imageRectangle; rectangleImageClosePanel.X = captionRectangle.Right - iSpacing - imageRectangle.Width; if(bIsClosable == true) { DrawIcon(graphics, imageClosePanel, rectangleImageClosePanel, foreColorCloseIcon, imageRectangle.Y); } iTextPositionX2 = rectangleImageClosePanel.X; } if((bShowExpandIcon == true) && (imageExandPanel != null)) { rectangleImageExandPanel = imageRectangle; rectangleImageExandPanel.X = captionRectangle.Right - iSpacing - imageRectangle.Width; if((bShowCloseIcon == true) && (imageClosePanel != null)) { rectangleImageExandPanel.X = iTextPositionX2 - (iSpacing / 2) - imageRectangle.Width; } DrawIcon(graphics, imageExandPanel, rectangleImageExandPanel, foreColorExpandIcon, imageRectangle.Y); iTextPositionX2 = rectangleImageExandPanel.X; } if((bShowCloseIcon == true) && (imageClosePanel != null) && (bShowExpandIcon == true) && (imageExandPanel != null)) { iTextPositionX2 -= iSpacing; } } else { if(image != null) { Rectangle imageRectangleRight = imageRectangle; imageRectangleRight.X = iTextPositionX2 - imageRectangle.Width; DrawImage(graphics, image, imageRectangleRight); iTextPositionX2 = imageRectangleRight.X - iSpacing; } } textRectangle.Width = iTextPositionX2 - iTextPositionX1; textRectangle.Y = (float) (captionRectangle.Height - fontCaption.Height) / 2 + 1; textRectangle.Height = fontCaption.Height; DrawString(graphics, textRectangle, fontCaption, captionForeColor, strCaptionText, rightToLeft, StringAlignment.Near); //if the XPanderPanel not closable then the RectangleCloseIcon must be empty if(bIsClosable == false) { rectangleImageClosePanel = Rectangle.Empty; } } /// /// Draws the text and image objects at the specified location. /// /// The Graphics to draw on. /// Specifies the position and manner in which a control is docked. /// The spacing on a panel's caption /// The rectangle of the panel's caption bar. /// The rectangle that represents the client area of the panel. /// The rectangle of an image displayed on a panel's caption. /// The image that is displayed on a panel's caption. /// A value indicating whether control's elements are aligned to support locales using right-to-left fonts. /// A value indicating whether the close image is displayed /// The close image that is displayed on a panel's caption. /// The foreground color of the close image that is displayed on a panel's caption. /// The rectangle of the close image that is displayed on a panel's caption. /// A value indicating whether the expand image is displayed /// A value indicating whether the panel is expanded or collapsed /// The expand image that is displayed on a panel's caption. /// The foreground color of the expand image displayed by this caption. /// /// The font of the text displayed on a panel's caption. /// The foreground color of the text displayed on a panel's caption. /// /// The text which is associated with this caption. protected static void DrawImagesAndText( Graphics graphics, DockStyle dockStyle, int iSpacing, Rectangle captionRectangle, Rectangle panelRectangle, Rectangle imageRectangle, Image image, RightToLeft rightToLeft, bool bShowCloseIcon, Image imageClosePanel, Color foreColorCloseIcon, ref Rectangle rectangleImageClosePanel, bool bShowExpandIcon, bool bIsExpanded, Image imageExandPanel, Color foreColorExpandPanel, ref Rectangle rectangleImageExandPanel, Font fontCaption, Color captionForeColor, Color collapsedForeColor, string strCaptionText) { switch(dockStyle) { case DockStyle.Left: case DockStyle.Right: if(bIsExpanded == true) { DrawImagesAndText( graphics, captionRectangle, iSpacing, imageRectangle, image, rightToLeft, true, bShowCloseIcon, imageClosePanel, foreColorCloseIcon, ref rectangleImageClosePanel, bShowExpandIcon, imageExandPanel, foreColorExpandPanel, ref rectangleImageExandPanel, fontCaption, captionForeColor, strCaptionText); } else { rectangleImageClosePanel = Rectangle.Empty; DrawVerticalImagesAndText( graphics, captionRectangle, panelRectangle, imageRectangle, dockStyle, image, rightToLeft, imageExandPanel, foreColorExpandPanel, ref rectangleImageExandPanel, fontCaption, collapsedForeColor, strCaptionText); } break; case DockStyle.Top: case DockStyle.Bottom: DrawImagesAndText( graphics, captionRectangle, iSpacing, imageRectangle, image, rightToLeft, true, bShowCloseIcon, imageClosePanel, foreColorCloseIcon, ref rectangleImageClosePanel, bShowExpandIcon, imageExandPanel, foreColorExpandPanel, ref rectangleImageExandPanel, fontCaption, captionForeColor, strCaptionText); break; } } /// /// Renders the background of the caption bar /// /// The Graphics to draw on. /// Rectangle structure that specifies the location of the caption bar. /// The starting color of the gradient used on the caption bar /// The middle color of the gradient used on the caption bar /// The end color of the gradient used on the caption bar /// Specifies the type of fill a Pen object uses to fill lines. /// protected static void RenderDoubleBackgroundGradient(Graphics graphics, Rectangle bounds, Color beginColor, Color middleColor, Color endColor, LinearGradientMode linearGradientMode, bool flipHorizontal) { int iUpperHeight = bounds.Height / 2; int iLowerHeight = bounds.Height - iUpperHeight; RenderDoubleBackgroundGradient( graphics, bounds, beginColor, middleColor, endColor, iUpperHeight, iLowerHeight, linearGradientMode, flipHorizontal); } /// /// Renders the panel background /// /// The Graphics to draw on. /// Rectangle structure that specifies the backgrounds location. /// The starting color of the gradient used on the panel background /// The end color of the gradient used on the panel background /// Specifies the type of fill a Pen object uses to fill lines. protected static void RenderBackgroundGradient(Graphics graphics, Rectangle bounds, Color beginColor, Color endColor, LinearGradientMode linearGradientMode) { if(graphics == null) { throw new ArgumentException( string.Format( System.Globalization.CultureInfo.CurrentUICulture, Resources.IDS_ArgumentException, typeof(Graphics).Name)); } if(IsZeroWidthOrHeight(bounds)) { return; } using(LinearGradientBrush linearGradientBrush = new LinearGradientBrush(bounds, beginColor, endColor, linearGradientMode)) { graphics.FillRectangle(linearGradientBrush, bounds); } } /// /// Renders the button background /// /// The Graphics to draw on. /// Rectangle structure that specifies the backgrounds location. /// The starting color of the gradient used on the button background /// The middle color of the gradient used on the button background /// The end color of the gradient used on the button background protected static void RenderButtonBackground(Graphics graphics, Rectangle bounds, Color colorGradientBegin, Color colorGradientMiddle, Color colorGradientEnd) { RectangleF upperRectangle = bounds; upperRectangle.Height = bounds.Height * 0.4f; using(LinearGradientBrush upperLinearGradientBrush = new LinearGradientBrush( upperRectangle, colorGradientBegin, colorGradientMiddle, LinearGradientMode.Vertical)) { if(upperLinearGradientBrush != null) { Blend blend = new Blend(); blend.Positions = new float[] { 0.0F, 1.0F }; blend.Factors = new float[] { 0.0F, 0.6F }; upperLinearGradientBrush.Blend = blend; graphics.FillRectangle(upperLinearGradientBrush, upperRectangle); } } RectangleF lowerRectangle = bounds; lowerRectangle.Y = upperRectangle.Height; lowerRectangle.Height = bounds.Height - upperRectangle.Height; using(LinearGradientBrush lowerLinearGradientBrush = new LinearGradientBrush( lowerRectangle, colorGradientMiddle, colorGradientEnd, LinearGradientMode.Vertical)) { if(lowerLinearGradientBrush != null) { graphics.FillRectangle(lowerLinearGradientBrush, lowerRectangle); } } //At some captionheights there are drawing errors. This is the correction RectangleF correctionRectangle = lowerRectangle; correctionRectangle.Y -= 1; correctionRectangle.Height = 2; using(SolidBrush solidBrush = new SolidBrush(colorGradientMiddle)) { graphics.FillRectangle(solidBrush, correctionRectangle); } } /// /// Renders the flat button background /// /// The Graphics to draw on. /// Rectangle structure that specifies the backgrounds location. /// The starting color of the gradient used on the button background /// The end color of the gradient used on the button background /// A indicator that represents when the mouse cursor hovers over protected static void RenderFlatButtonBackground(Graphics graphics, Rectangle bounds, Color colorGradientBegin, Color colorGradientEnd, bool bHover) { using(LinearGradientBrush gradientBrush = GetFlatGradientBackBrush(bounds, colorGradientBegin, colorGradientEnd, bHover)) { if(gradientBrush != null) { graphics.FillRectangle(gradientBrush, bounds); } } } /// /// Gets a GraphicsPath. /// /// Rectangle structure that specifies the backgrounds location. /// The radius in the graphics path /// the specified graphics path protected static GraphicsPath GetPath(Rectangle bounds, int radius) { int x = bounds.X; int y = bounds.Y; int width = bounds.Width; int height = bounds.Height; GraphicsPath graphicsPath = new GraphicsPath(); graphicsPath.AddArc(x, y, radius, radius, 180, 90); //Upper left corner graphicsPath.AddArc(x + width - radius, y, radius, radius, 270, 90); //Upper right corner graphicsPath.AddArc(x + width - radius, y + height - radius, radius, radius, 0, 90);//Lower right corner graphicsPath.AddArc(x, y + height - radius, radius, radius, 90, 90); //Lower left corner graphicsPath.CloseFigure(); return graphicsPath; } /// /// Gets a GraphicsPath with rounded corners on the upper side. /// /// Rectangle structure that specifies the backgrounds location. /// The radius in the graphics path /// the specified graphics path protected static GraphicsPath GetUpperBackgroundPath(Rectangle bounds, int radius) { int x = bounds.X; int y = bounds.Y; int width = bounds.Width; int height = bounds.Height; GraphicsPath graphicsPath = new GraphicsPath(); graphicsPath.AddLine(x, y + height, x, y - radius); //Left Line graphicsPath.AddArc(x, y, radius, radius, 180, 90); //Upper left corner graphicsPath.AddArc(x + width - radius, y, radius, radius, 270, 90);//Upper right corner graphicsPath.AddLine(x + width, y + radius, x + width, y + height); //Right Line graphicsPath.CloseFigure(); return graphicsPath; } /// /// Gets a GraphicsPath. /// /// Rectangle structure that specifies the backgrounds location. /// The radius in the graphics path /// The specified graphics path protected static GraphicsPath GetBackgroundPath(Rectangle bounds, int radius) { int x = bounds.X; int y = bounds.Y; int width = bounds.Width; int height = bounds.Height; GraphicsPath graphicsPath = new GraphicsPath(); graphicsPath.AddArc(x, y, radius, radius, 180, 90); //Upper left corner graphicsPath.AddArc(x + width - radius, y, radius, radius, 270, 90); //Upper right corner graphicsPath.AddArc(x + width - radius, y + height - radius, radius, radius, 0, 90);//Lower right corner graphicsPath.AddArc(x, y + height - radius, radius, radius, 90, 90); //Lower left corner graphicsPath.CloseFigure(); return graphicsPath; } /// /// Gets the linear GradientBackBrush for flat XPanderPanel captions. /// /// Rectangle structure that specifies the bounds of the linear gradient. /// A Color structure that represents the starting color for the gradient. /// A Color structure that represents the end color for the gradient. /// A indicator that represents when the mouse cursor hovers over /// protected static LinearGradientBrush GetFlatGradientBackBrush(Rectangle bounds, Color colorGradientBegin, Color colorGradientEnd, bool bHover) { LinearGradientBrush linearGradientBrush = null; Blend blend = new Blend(); blend.Positions = new float[] { 0.0F, 0.2F, 0.3F, 0.4F, 0.5F, 0.6F, 0.7F, 0.8F, 1.0F }; if(bHover == false) { blend.Factors = new float[] { 0.0F, 0.0F, 0.2F, 0.4F, 0.6F, 0.4F, 0.2F, 0.0F, 0.0F }; } else { blend.Factors = new float[] { 0.4F, 0.5F, 0.6F, 0.8F, 1.0F, 0.8F, 0.6F, 0.5F, 0.4F }; } linearGradientBrush = linearGradientBrush = new LinearGradientBrush(bounds, colorGradientBegin, colorGradientEnd, LinearGradientMode.Horizontal); if(linearGradientBrush != null) { linearGradientBrush.Blend = blend; } return linearGradientBrush; } /// /// Checks if the rectangle width or height is equal to 0. /// /// the rectangle to check /// true if the with or height of the rectangle is 0 else false protected static bool IsZeroWidthOrHeight(Rectangle rectangle) { if(rectangle.Width != 0) { return (rectangle.Height == 0); } return true; } #endregion #region MethodsPrivate private static void RenderDoubleBackgroundGradient(Graphics graphics, Rectangle bounds, Color beginColor, Color middleColor, Color endColor, int firstGradientWidth, int secondGradientWidth, LinearGradientMode mode, bool flipHorizontal) { if((bounds.Width != 0) && (bounds.Height != 0)) { Rectangle rectangle1 = bounds; Rectangle rectangle2 = bounds; bool flag1 = true; if(mode == LinearGradientMode.Horizontal) { if(flipHorizontal) { Color color1 = endColor; endColor = beginColor; beginColor = color1; } rectangle2.Width = firstGradientWidth; rectangle1.Width = secondGradientWidth + 1; rectangle1.X = bounds.Right - rectangle1.Width; flag1 = bounds.Width > (firstGradientWidth + secondGradientWidth); } else { rectangle2.Height = firstGradientWidth; rectangle1.Height = secondGradientWidth + 1; rectangle1.Y = bounds.Bottom - rectangle1.Height; flag1 = bounds.Height > (firstGradientWidth + secondGradientWidth); } if(flag1) { using(Brush brush1 = new SolidBrush(middleColor)) { graphics.FillRectangle(brush1, bounds); } using(Brush brush2 = new LinearGradientBrush(rectangle2, beginColor, middleColor, mode)) { graphics.FillRectangle(brush2, rectangle2); } using(LinearGradientBrush brush3 = new LinearGradientBrush(rectangle1, middleColor, endColor, mode)) { if(mode == LinearGradientMode.Horizontal) { rectangle1.X++; rectangle1.Width--; } else { rectangle1.Y++; rectangle1.Height--; } graphics.FillRectangle(brush3, rectangle1); return; } } using(Brush brush4 = new LinearGradientBrush(bounds, beginColor, endColor, mode)) { graphics.FillRectangle(brush4, bounds); } } } private static void DrawVerticalImagesAndText( Graphics graphics, Rectangle captionRectangle, Rectangle panelRectangle, Rectangle imageRectangle, DockStyle dockStyle, Image image, RightToLeft rightToLeft, Image imageExandPanel, Color foreColorExpandPanel, ref Rectangle rectangleImageExandPanel, Font captionFont, Color collapsedCaptionForeColor, string strCaptionText) { imageRectangle.Y = (captionRectangle.Height - imageRectangle.Height) / 2; if(imageExandPanel != null) { rectangleImageExandPanel = imageRectangle; rectangleImageExandPanel.X = (panelRectangle.Width - imageRectangle.Width) / 2; DrawIcon(graphics, imageExandPanel, rectangleImageExandPanel, foreColorExpandPanel, imageRectangle.Y); } int iTextPositionY1 = CaptionSpacing; int iTextPositionY2 = panelRectangle.Height - CaptionSpacing; if(image != null) { imageRectangle.Y = iTextPositionY2 - imageRectangle.Height; imageRectangle.X = (panelRectangle.Width - imageRectangle.Width) / 2; DrawImage(graphics, image, imageRectangle); iTextPositionY1 += imageRectangle.Height + CaptionSpacing; } iTextPositionY2 -= captionRectangle.Height + iTextPositionY1; Rectangle textRectangle = new Rectangle( iTextPositionY1, panelRectangle.Y, iTextPositionY2, captionRectangle.Height); using(SolidBrush textBrush = new SolidBrush(collapsedCaptionForeColor)) { if(dockStyle == DockStyle.Left) { graphics.TranslateTransform(0, panelRectangle.Height); graphics.RotateTransform(-90); DrawString( graphics, textRectangle, captionFont, collapsedCaptionForeColor, strCaptionText, rightToLeft, StringAlignment.Center); graphics.ResetTransform(); } if(dockStyle == DockStyle.Right) { graphics.TranslateTransform(panelRectangle.Width, 0); graphics.RotateTransform(90); DrawString( graphics, textRectangle, captionFont, collapsedCaptionForeColor, strCaptionText, rightToLeft, StringAlignment.Center); graphics.ResetTransform(); } } } #endregion } }