VS2005DockPaneCaption.cs
上传用户:szlfmled
上传日期:2020-11-22
资源大小:978k
文件大小:15k
源码类别:

C#编程

开发平台:

C#

  1. using System;
  2. using System.Drawing;
  3. using System.Drawing.Drawing2D;
  4. using System.Windows.Forms;
  5. using System.ComponentModel;
  6. using System.Windows.Forms.VisualStyles;
  7. namespace WeifenLuo.WinFormsUI.Docking
  8. {
  9. internal class VS2005DockPaneCaption : DockPaneCaptionBase
  10. {
  11.         private sealed class InertButton : InertButtonBase
  12.         {
  13.             private Bitmap m_image, m_imageAutoHide;
  14.             public InertButton(VS2005DockPaneCaption dockPaneCaption, Bitmap image, Bitmap imageAutoHide)
  15.                 : base()
  16.             {
  17.                 m_dockPaneCaption = dockPaneCaption;
  18.                 m_image = image;
  19.                 m_imageAutoHide = imageAutoHide;
  20.                 RefreshChanges();
  21.             }
  22.             private VS2005DockPaneCaption m_dockPaneCaption;
  23.             private VS2005DockPaneCaption DockPaneCaption
  24.             {
  25.                 get { return m_dockPaneCaption; }
  26.             }
  27.             public bool IsAutoHide
  28.             {
  29.                 get { return DockPaneCaption.DockPane.IsAutoHide; }
  30.             }
  31.             public override Bitmap Image
  32.             {
  33.                 get { return IsAutoHide ? m_imageAutoHide : m_image; }
  34.             }
  35.             protected override void OnRefreshChanges()
  36.             {
  37.                 if (DockPaneCaption.DockPane.DockPanel != null)
  38.                 {
  39.                     if (DockPaneCaption.TextColor != ForeColor)
  40.                     {
  41.                         ForeColor = DockPaneCaption.TextColor;
  42.                         Invalidate();
  43.                     }
  44.                 }
  45.             }
  46.         }
  47. #region consts
  48. private const int _TextGapTop = 2;
  49. private const int _TextGapBottom = 0;
  50. private const int _TextGapLeft = 3;
  51. private const int _TextGapRight = 3;
  52. private const int _ButtonGapTop = 2;
  53. private const int _ButtonGapBottom = 1;
  54. private const int _ButtonGapBetween = 1;
  55. private const int _ButtonGapLeft = 1;
  56. private const int _ButtonGapRight = 2;
  57. #endregion
  58.         private static Bitmap _imageButtonClose;
  59.         private static Bitmap ImageButtonClose
  60.         {
  61.             get
  62.             {
  63.                 if (_imageButtonClose == null)
  64.                     _imageButtonClose = Resources.DockPane_Close;
  65.                 return _imageButtonClose;
  66.             }
  67.         }
  68. private InertButton m_buttonClose;
  69.         private InertButton ButtonClose
  70.         {
  71.             get
  72.             {
  73.                 if (m_buttonClose == null)
  74.                 {
  75.                     m_buttonClose = new InertButton(this, ImageButtonClose, ImageButtonClose);
  76.                     m_toolTip.SetToolTip(m_buttonClose, ToolTipClose);
  77.                     m_buttonClose.Click += new EventHandler(Close_Click);
  78.                     Controls.Add(m_buttonClose);
  79.                 }
  80.                 return m_buttonClose;
  81.             }
  82.         }
  83.         private static Bitmap _imageButtonAutoHide;
  84.         private static Bitmap ImageButtonAutoHide
  85.         {
  86.             get
  87.             {
  88.                 if (_imageButtonAutoHide == null)
  89.                     _imageButtonAutoHide = Resources.DockPane_AutoHide;
  90.                 return _imageButtonAutoHide;
  91.             }
  92.         }
  93.         private static Bitmap _imageButtonDock;
  94.         private static Bitmap ImageButtonDock
  95.         {
  96.             get
  97.             {
  98.                 if (_imageButtonDock == null)
  99.                     _imageButtonDock = Resources.DockPane_Dock;
  100.                 return _imageButtonDock;
  101.             }
  102.         }
  103.         private InertButton m_buttonAutoHide;
  104.         private InertButton ButtonAutoHide
  105.         {
  106.             get
  107.             {
  108.                 if (m_buttonAutoHide == null)
  109.                 {
  110.                     m_buttonAutoHide = new InertButton(this, ImageButtonDock, ImageButtonAutoHide);
  111.                     m_toolTip.SetToolTip(m_buttonAutoHide, ToolTipAutoHide);
  112.                     m_buttonAutoHide.Click += new EventHandler(AutoHide_Click);
  113.                     Controls.Add(m_buttonAutoHide);
  114.                 }
  115.                 return m_buttonAutoHide;
  116.             }
  117.         }
  118.         private static Bitmap _imageButtonOptions;
  119.         private static Bitmap ImageButtonOptions
  120.         {
  121.             get
  122.             {
  123.                 if (_imageButtonOptions == null)
  124.                     _imageButtonOptions = Resources.DockPane_Option;
  125.                 return _imageButtonOptions;
  126.             }
  127.         }
  128.         private InertButton m_buttonOptions;
  129.         private InertButton ButtonOptions
  130.         {
  131.             get
  132.             {
  133.                 if (m_buttonOptions == null)
  134.                 {
  135.                     m_buttonOptions = new InertButton(this, ImageButtonOptions, ImageButtonOptions);
  136.                     m_toolTip.SetToolTip(m_buttonOptions, ToolTipOptions);
  137.                     m_buttonOptions.Click += new EventHandler(Options_Click);
  138.                     Controls.Add(m_buttonOptions);
  139.                 }
  140.                 return m_buttonOptions;
  141.             }
  142.         }
  143.         private IContainer m_components;
  144.         private IContainer Components
  145.         {
  146.             get { return m_components; }
  147.         }
  148. private ToolTip m_toolTip;
  149. public VS2005DockPaneCaption(DockPane pane) : base(pane)
  150. {
  151. SuspendLayout();
  152.             m_components = new Container();
  153.             m_toolTip = new ToolTip(Components);
  154. ResumeLayout();
  155. }
  156.         protected override void Dispose(bool disposing)
  157.         {
  158.             if (disposing)
  159.                 Components.Dispose();
  160.             base.Dispose(disposing);
  161.         }
  162. private static int TextGapTop
  163. {
  164. get { return _TextGapTop; }
  165. }
  166.         private static Font TextFont
  167.         {
  168.             get { return SystemInformation.MenuFont; }
  169.         }
  170. private static int TextGapBottom
  171. {
  172. get { return _TextGapBottom; }
  173. }
  174. private static int TextGapLeft
  175. {
  176. get { return _TextGapLeft; }
  177. }
  178. private static int TextGapRight
  179. {
  180. get { return _TextGapRight; }
  181. }
  182. private static int ButtonGapTop
  183. {
  184. get { return _ButtonGapTop; }
  185. }
  186. private static int ButtonGapBottom
  187. {
  188. get { return _ButtonGapBottom; }
  189. }
  190. private static int ButtonGapLeft
  191. {
  192. get { return _ButtonGapLeft; }
  193. }
  194. private static int ButtonGapRight
  195. {
  196. get { return _ButtonGapRight; }
  197. }
  198. private static int ButtonGapBetween
  199. {
  200. get { return _ButtonGapBetween; }
  201. }
  202. private static string _toolTipClose;
  203. private static string ToolTipClose
  204. {
  205. get
  206. {
  207. if (_toolTipClose == null)
  208. _toolTipClose = Strings.DockPaneCaption_ToolTipClose;
  209. return _toolTipClose;
  210. }
  211. }
  212.         private static string _toolTipOptions;
  213.         private static string ToolTipOptions
  214.         {
  215.             get
  216.             {
  217.                 if (_toolTipOptions == null)
  218.                     _toolTipOptions = Strings.DockPaneCaption_ToolTipOptions;
  219.                 return _toolTipOptions;
  220.             }
  221.         }
  222. private static string _toolTipAutoHide;
  223. private static string ToolTipAutoHide
  224. {
  225. get
  226. {
  227. if (_toolTipAutoHide == null)
  228. _toolTipAutoHide = Strings.DockPaneCaption_ToolTipAutoHide;
  229. return _toolTipAutoHide;
  230. }
  231. }
  232.         private static Blend _activeBackColorGradientBlend;
  233.         private static Blend ActiveBackColorGradientBlend
  234.         {
  235.             get
  236.             {
  237.                 if (_activeBackColorGradientBlend == null)
  238.                 {
  239.                     Blend blend = new Blend(2);
  240.                     blend.Factors = new float[]{0.5F, 1.0F};
  241.                     blend.Positions = new float[]{0.0F, 1.0F};
  242.                     _activeBackColorGradientBlend = blend;
  243.                 }
  244.                 return _activeBackColorGradientBlend;
  245.             }
  246.         }
  247.         private Color TextColor
  248.         {
  249.             get
  250.             {
  251.                 if (DockPane.IsActivated)
  252.                     return DockPane.DockPanel.Skin.DockPaneStripSkin.ToolWindowGradient.ActiveCaptionGradient.TextColor;
  253.                 else
  254.                     return DockPane.DockPanel.Skin.DockPaneStripSkin.ToolWindowGradient.InactiveCaptionGradient.TextColor;
  255.             }
  256.         }
  257. private static TextFormatFlags _textFormat =
  258.             TextFormatFlags.SingleLine |
  259.             TextFormatFlags.EndEllipsis |
  260.             TextFormatFlags.VerticalCenter;
  261. private TextFormatFlags TextFormat
  262. {
  263.             get
  264.             {
  265.                 if (RightToLeft == RightToLeft.No)
  266.                     return _textFormat;
  267.                 else
  268.                     return _textFormat | TextFormatFlags.RightToLeft | TextFormatFlags.Right;
  269.             }
  270. }
  271. protected internal override int MeasureHeight()
  272. {
  273. int height = TextFont.Height + TextGapTop + TextGapBottom;
  274. if (height < ButtonClose.Image.Height + ButtonGapTop + ButtonGapBottom)
  275. height = ButtonClose.Image.Height + ButtonGapTop + ButtonGapBottom;
  276. return height;
  277. }
  278. protected override void OnPaint(PaintEventArgs e)
  279. {
  280. base.OnPaint (e);
  281. DrawCaption(e.Graphics);
  282. }
  283. private void DrawCaption(Graphics g)
  284. {
  285.             if (ClientRectangle.Width == 0 || ClientRectangle.Height == 0)
  286.                 return;
  287.             if (DockPane.IsActivated)
  288.             {
  289.                 Color startColor = DockPane.DockPanel.Skin.DockPaneStripSkin.ToolWindowGradient.ActiveCaptionGradient.StartColor;
  290.                 Color endColor = DockPane.DockPanel.Skin.DockPaneStripSkin.ToolWindowGradient.ActiveCaptionGradient.EndColor;
  291.                 LinearGradientMode gradientMode = DockPane.DockPanel.Skin.DockPaneStripSkin.ToolWindowGradient.ActiveCaptionGradient.LinearGradientMode;
  292.                 using (LinearGradientBrush brush = new LinearGradientBrush(ClientRectangle, startColor, endColor, gradientMode))
  293.                 {
  294.                     brush.Blend = ActiveBackColorGradientBlend;
  295.                     g.FillRectangle(brush, ClientRectangle);
  296.                 }
  297.             }
  298.             else
  299.             {
  300.                 Color startColor = DockPane.DockPanel.Skin.DockPaneStripSkin.ToolWindowGradient.InactiveCaptionGradient.StartColor;
  301.                 Color endColor = DockPane.DockPanel.Skin.DockPaneStripSkin.ToolWindowGradient.InactiveCaptionGradient.EndColor;
  302.                 LinearGradientMode gradientMode = DockPane.DockPanel.Skin.DockPaneStripSkin.ToolWindowGradient.InactiveCaptionGradient.LinearGradientMode;
  303.                 using (LinearGradientBrush brush = new LinearGradientBrush(ClientRectangle, startColor, endColor, gradientMode))
  304.                 {
  305.                     g.FillRectangle(brush, ClientRectangle);
  306.                 }
  307.             }
  308. Rectangle rectCaption = ClientRectangle;
  309. Rectangle rectCaptionText = rectCaption;
  310.             rectCaptionText.X += TextGapLeft;
  311.             rectCaptionText.Width -= TextGapLeft + TextGapRight;
  312.             rectCaptionText.Width -= ButtonGapLeft + ButtonClose.Width + ButtonGapRight;
  313.             if (ShouldShowAutoHideButton)
  314.                 rectCaptionText.Width -= ButtonAutoHide.Width + ButtonGapBetween;
  315.             if (HasTabPageContextMenu)
  316.                 rectCaptionText.Width -= ButtonOptions.Width + ButtonGapBetween;
  317. rectCaptionText.Y += TextGapTop;
  318. rectCaptionText.Height -= TextGapTop + TextGapBottom;
  319.             Color colorText;
  320.             if (DockPane.IsActivated)
  321.                 colorText = DockPane.DockPanel.Skin.DockPaneStripSkin.ToolWindowGradient.ActiveCaptionGradient.TextColor;
  322.             else
  323.                 colorText = DockPane.DockPanel.Skin.DockPaneStripSkin.ToolWindowGradient.InactiveCaptionGradient.TextColor;
  324.             TextRenderer.DrawText(g, DockPane.CaptionText, TextFont, DrawHelper.RtlTransform(this, rectCaptionText), colorText, TextFormat);
  325. }
  326. protected override void OnLayout(LayoutEventArgs levent)
  327. {
  328. SetButtonsPosition();
  329. base.OnLayout (levent);
  330. }
  331. protected override void OnRefreshChanges()
  332. {
  333. SetButtons();
  334. Invalidate();
  335. }
  336. private bool CloseButtonEnabled
  337. {
  338. get { return (DockPane.ActiveContent != null)? DockPane.ActiveContent.DockHandler.CloseButton : false; }
  339. }
  340.         /// <summary>
  341.         /// Determines whether the close button is visible on the content
  342.         /// </summary>
  343.         private bool CloseButtonVisible
  344.         {
  345.             get { return (DockPane.ActiveContent != null) ? DockPane.ActiveContent.DockHandler.CloseButtonVisible : false; }
  346.         }
  347. private bool ShouldShowAutoHideButton
  348. {
  349. get { return !DockPane.IsFloat; }
  350. }
  351. private void SetButtons()
  352. {
  353. ButtonClose.Enabled = CloseButtonEnabled;
  354.             ButtonClose.Visible = CloseButtonVisible;
  355. ButtonAutoHide.Visible = ShouldShowAutoHideButton;
  356.             ButtonOptions.Visible = HasTabPageContextMenu;
  357.             ButtonClose.RefreshChanges();
  358.             ButtonAutoHide.RefreshChanges();
  359.             ButtonOptions.RefreshChanges();
  360. SetButtonsPosition();
  361. }
  362. private void SetButtonsPosition()
  363. {
  364. // set the size and location for close and auto-hide buttons
  365. Rectangle rectCaption = ClientRectangle;
  366. int buttonWidth = ButtonClose.Image.Width;
  367. int buttonHeight = ButtonClose.Image.Height;
  368. int height = rectCaption.Height - ButtonGapTop - ButtonGapBottom;
  369. if (buttonHeight < height)
  370. {
  371. buttonWidth = buttonWidth * (height / buttonHeight);
  372. buttonHeight = height;
  373. }
  374. Size buttonSize = new Size(buttonWidth, buttonHeight);
  375. int x = rectCaption.X + rectCaption.Width - 1 - ButtonGapRight - m_buttonClose.Width;
  376. int y = rectCaption.Y + ButtonGapTop;
  377. Point point = new Point(x, y);
  378.             ButtonClose.Bounds = DrawHelper.RtlTransform(this, new Rectangle(point, buttonSize));
  379.             // If the close button is not visible draw the auto hide button overtop.
  380.             // Otherwise it is drawn to the left of the close button.
  381.             if (CloseButtonVisible)
  382.     point.Offset(-(buttonWidth + ButtonGapBetween), 0);
  383.             
  384.             ButtonAutoHide.Bounds = DrawHelper.RtlTransform(this, new Rectangle(point, buttonSize));
  385.             if (ShouldShowAutoHideButton)
  386.                 point.Offset(-(buttonWidth + ButtonGapBetween), 0);
  387.             ButtonOptions.Bounds = DrawHelper.RtlTransform(this, new Rectangle(point, buttonSize));
  388. }
  389. private void Close_Click(object sender, EventArgs e)
  390. {
  391. DockPane.CloseActiveContent();
  392. }
  393. private void AutoHide_Click(object sender, EventArgs e)
  394. {
  395. DockPane.DockState = DockHelper.ToggleAutoHideState(DockPane.DockState);
  396.             if (DockHelper.IsDockStateAutoHide(DockPane.DockState))
  397.                 DockPane.DockPanel.ActiveAutoHideContent = null;
  398. }
  399.         private void Options_Click(object sender, EventArgs e)
  400.         {
  401.             ShowTabPageContextMenu(PointToClient(Control.MousePosition));
  402.         }
  403.         protected override void OnRightToLeftChanged(EventArgs e)
  404.         {
  405.             base.OnRightToLeftChanged(e);
  406.             PerformLayout();
  407.         }
  408. }
  409. }