using System; using System.Drawing; using System.Windows.Forms; using System.ComponentModel; using WeifenLuo.WinFormsUI.Docking; namespace DockSample.Customization { /// internal class VS2003DockPaneCaption : DockPaneCaptionBase { #region consts private const int _TextGapTop = 2; private const int _TextGapBottom = 0; private const int _TextGapLeft = 3; private const int _TextGapRight = 3; private const int _ButtonGapTop = 2; private const int _ButtonGapBottom = 1; private const int _ButtonGapBetween = 1; private const int _ButtonGapLeft = 1; private const int _ButtonGapRight = 2; #endregion private InertButton m_buttonClose; private InertButton m_buttonAutoHide; /// protected internal VS2003DockPaneCaption(DockPane pane) : base(pane) { SuspendLayout(); Font = SystemInformation.MenuFont; m_buttonClose = new InertButton(ImageCloseEnabled, ImageCloseDisabled); m_buttonAutoHide = new InertButton(); m_buttonClose.ToolTipText = ToolTipClose; m_buttonClose.Anchor = AnchorStyles.Top | AnchorStyles.Right; m_buttonClose.Click += new EventHandler(this.Close_Click); m_buttonAutoHide.ToolTipText = ToolTipAutoHide; m_buttonAutoHide.Anchor = AnchorStyles.Top | AnchorStyles.Right; m_buttonAutoHide.Click += new EventHandler(AutoHide_Click); Controls.AddRange(new Control[] { m_buttonClose, m_buttonAutoHide }); ResumeLayout(); } #region Customizable Properties /// protected virtual int TextGapTop { get { return _TextGapTop; } } /// protected virtual int TextGapBottom { get { return _TextGapBottom; } } /// protected virtual int TextGapLeft { get { return _TextGapLeft; } } /// protected virtual int TextGapRight { get { return _TextGapRight; } } /// protected virtual int ButtonGapTop { get { return _ButtonGapTop; } } /// protected virtual int ButtonGapBottom { get { return _ButtonGapBottom; } } /// protected virtual int ButtonGapLeft { get { return _ButtonGapLeft; } } /// protected virtual int ButtonGapRight { get { return _ButtonGapRight; } } /// protected virtual int ButtonGapBetween { get { return _ButtonGapBetween; } } private static Image _imageCloseEnabled = null; /// protected virtual Image ImageCloseEnabled { get { if (_imageCloseEnabled == null) _imageCloseEnabled = Resources.DockPaneCaption_CloseEnabled; return _imageCloseEnabled; } } private static Image _imageCloseDisabled = null; /// protected virtual Image ImageCloseDisabled { get { if (_imageCloseDisabled == null) _imageCloseDisabled = Resources.DockPaneCaption_CloseDisabled; return _imageCloseDisabled; } } private static Image _imageAutoHideYes = null; /// protected virtual Image ImageAutoHideYes { get { if (_imageAutoHideYes == null) _imageAutoHideYes = Resources.DockPaneCaption_AutoHideYes; return _imageAutoHideYes; } } private static Image _imageAutoHideNo = null; /// protected virtual Image ImageAutoHideNo { get { if (_imageAutoHideNo == null) _imageAutoHideNo = Resources.DockPaneCaption_AutoHideNo; return _imageAutoHideNo; } } private static string _toolTipClose = null; /// protected virtual string ToolTipClose { get { if (_toolTipClose == null) _toolTipClose = Strings.DockPaneCaption_ToolTipClose; return _toolTipClose; } } private static string _toolTipAutoHide = null; /// protected virtual string ToolTipAutoHide { get { if (_toolTipAutoHide == null) _toolTipAutoHide = Strings.DockPaneCaption_ToolTipAutoHide; return _toolTipAutoHide; } } /// protected virtual Color ActiveBackColor { get { return SystemColors.ActiveCaption; } } /// protected virtual Color InactiveBackColor { get { return SystemColors.Control; } } /// protected virtual Color ActiveTextColor { get { return SystemColors.ActiveCaptionText; } } /// protected virtual Color InactiveTextColor { get { return SystemColors.ControlText; } } /// protected virtual Color InactiveBorderColor { get { return SystemColors.GrayText; } } /// protected virtual Color ActiveButtonBorderColor { get { return ActiveTextColor; } } /// protected virtual Color InactiveButtonBorderColor { get { return Color.Empty; } } private static TextFormatFlags _textFormat = TextFormatFlags.SingleLine | TextFormatFlags.EndEllipsis | TextFormatFlags.VerticalCenter; /// protected virtual TextFormatFlags TextFormat { get { return _textFormat; } } #endregion /// protected override int MeasureHeight() { int height = Font.Height + TextGapTop + TextGapBottom; if (height < ImageCloseEnabled.Height + ButtonGapTop + ButtonGapBottom) height = ImageCloseEnabled.Height + ButtonGapTop + ButtonGapBottom; return height; } /// protected override void OnPaint(PaintEventArgs e) { base.OnPaint (e); DrawCaption(e.Graphics); } private void DrawCaption(Graphics g) { BackColor = DockPane.IsActivated ? ActiveBackColor : InactiveBackColor; Rectangle rectCaption = ClientRectangle; if (!DockPane.IsActivated) { using (Pen pen = new Pen(InactiveBorderColor)) { g.DrawLine(pen, rectCaption.X + 1, rectCaption.Y, rectCaption.X + rectCaption.Width - 2, rectCaption.Y); g.DrawLine(pen, rectCaption.X + 1, rectCaption.Y + rectCaption.Height - 1, rectCaption.X + rectCaption.Width - 2, rectCaption.Y + rectCaption.Height - 1); g.DrawLine(pen, rectCaption.X, rectCaption.Y + 1, rectCaption.X, rectCaption.Y + rectCaption.Height - 2); g.DrawLine(pen, rectCaption.X + rectCaption.Width - 1, rectCaption.Y + 1, rectCaption.X + rectCaption.Width - 1, rectCaption.Y + rectCaption.Height - 2); } } m_buttonClose.ForeColor = m_buttonAutoHide.ForeColor = (DockPane.IsActivated ? ActiveTextColor : InactiveTextColor); m_buttonClose.BorderColor = m_buttonAutoHide.BorderColor = (DockPane.IsActivated ? ActiveButtonBorderColor : InactiveButtonBorderColor); Rectangle rectCaptionText = rectCaption; rectCaptionText.X += TextGapLeft; if (ShouldShowCloseButton && ShouldShowAutoHideButton) rectCaptionText.Width = rectCaption.Width - ButtonGapRight - ButtonGapLeft - TextGapLeft - TextGapRight - (m_buttonAutoHide.Width + ButtonGapBetween + m_buttonClose.Width); else if (ShouldShowCloseButton || ShouldShowAutoHideButton) rectCaptionText.Width = rectCaption.Width - ButtonGapRight - ButtonGapLeft - TextGapLeft - TextGapRight - m_buttonClose.Width; else rectCaptionText.Width = rectCaption.Width - TextGapLeft - TextGapRight; rectCaptionText.Y += TextGapTop; rectCaptionText.Height -= TextGapTop + TextGapBottom; TextRenderer.DrawText(g, DockPane.CaptionText, Font, rectCaptionText, DockPane.IsActivated ? ActiveTextColor : InactiveTextColor, TextFormat); } /// protected override void OnLayout(LayoutEventArgs levent) { SetButtonsPosition(); base.OnLayout (levent); } /// protected override void OnRefreshChanges() { SetButtons(); Invalidate(); } private bool ShouldShowCloseButton { get { return (DockPane.ActiveContent != null)? DockPane.ActiveContent.DockHandler.CloseButton : false; } } private bool ShouldShowAutoHideButton { get { return !DockPane.IsFloat; } } private void SetButtons() { m_buttonClose.Visible = ShouldShowCloseButton; m_buttonAutoHide.Visible = ShouldShowAutoHideButton; m_buttonAutoHide.ImageEnabled = DockPane.IsAutoHide ? ImageAutoHideYes : ImageAutoHideNo; SetButtonsPosition(); } private void SetButtonsPosition() { // set the size and location for close and auto-hide buttons Rectangle rectCaption = ClientRectangle; int buttonWidth = ImageCloseEnabled.Width; int buttonHeight = ImageCloseEnabled.Height; int height = rectCaption.Height - ButtonGapTop - ButtonGapBottom; if (buttonHeight < height) { buttonWidth = buttonWidth * (height / buttonHeight); buttonHeight = height; } m_buttonClose.SuspendLayout(); m_buttonAutoHide.SuspendLayout(); Size buttonSize = new Size(buttonWidth, buttonHeight); m_buttonClose.Size = m_buttonAutoHide.Size = buttonSize; int x = rectCaption.X + rectCaption.Width - 1 - ButtonGapRight - m_buttonClose.Width; int y = rectCaption.Y + ButtonGapTop; Point point = m_buttonClose.Location = new Point(x, y); if (ShouldShowCloseButton) point.Offset(-(m_buttonAutoHide.Width + ButtonGapBetween), 0); m_buttonAutoHide.Location = point; m_buttonClose.ResumeLayout(); m_buttonAutoHide.ResumeLayout(); } private void Close_Click(object sender, EventArgs e) { DockPane.CloseActiveContent(); } private void AutoHide_Click(object sender, EventArgs e) { DockPane.DockState = DockHelper.ToggleAutoHideState(DockPane.DockState); if (!DockPane.IsAutoHide) DockPane.Activate(); } } }