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

C#编程

开发平台:

C#

  1. using System;
  2. using System.Drawing;
  3. using System.Windows.Forms;
  4. using System.ComponentModel;
  5. using WeifenLuo.WinFormsUI.Docking;
  6. namespace DockSample.Customization
  7. {
  8. /// <include file='CodeDoc/DockPaneCaptionVS2003.xml' path='//CodeDoc/Class[@name="DockPaneCaptionVS2003"]/ClassDef/*'/>
  9. internal class VS2003DockPaneCaption : DockPaneCaptionBase
  10. {
  11. #region consts
  12. private const int _TextGapTop = 2;
  13. private const int _TextGapBottom = 0;
  14. private const int _TextGapLeft = 3;
  15. private const int _TextGapRight = 3;
  16. private const int _ButtonGapTop = 2;
  17. private const int _ButtonGapBottom = 1;
  18. private const int _ButtonGapBetween = 1;
  19. private const int _ButtonGapLeft = 1;
  20. private const int _ButtonGapRight = 2;
  21. #endregion
  22. private InertButton m_buttonClose;
  23. private InertButton m_buttonAutoHide;
  24. /// <include file='CodeDoc/DockPaneCaptionVS2003.xml' path='//CodeDoc/Class[@name="DockPaneCaptionVS2003"]/Construct[@name="(DockPane)"]/*'/>
  25. protected internal VS2003DockPaneCaption(DockPane pane) : base(pane)
  26. {
  27. SuspendLayout();
  28. Font = SystemInformation.MenuFont;
  29. m_buttonClose = new InertButton(ImageCloseEnabled, ImageCloseDisabled);
  30. m_buttonAutoHide = new InertButton();
  31. m_buttonClose.ToolTipText = ToolTipClose;
  32. m_buttonClose.Anchor = AnchorStyles.Top | AnchorStyles.Right;
  33. m_buttonClose.Click += new EventHandler(this.Close_Click);
  34. m_buttonAutoHide.ToolTipText = ToolTipAutoHide;
  35. m_buttonAutoHide.Anchor = AnchorStyles.Top | AnchorStyles.Right;
  36. m_buttonAutoHide.Click += new EventHandler(AutoHide_Click);
  37. Controls.AddRange(new Control[] { m_buttonClose, m_buttonAutoHide });
  38. ResumeLayout();
  39. }
  40. #region Customizable Properties
  41. /// <include file='CodeDoc/DockPaneCaptionVS2003.xml' path='//CodeDoc/Class[@name="DockPaneCaptionVS2003"]/Property[@name="TextGapTop"]/*'/>
  42. protected virtual int TextGapTop
  43. {
  44. get { return _TextGapTop; }
  45. }
  46. /// <include file='CodeDoc/DockPaneCaptionVS2003.xml' path='//CodeDoc/Class[@name="DockPaneCaptionVS2003"]/Property[@name="TextGapBottom"]/*'/>
  47. protected virtual int TextGapBottom
  48. {
  49. get { return _TextGapBottom; }
  50. }
  51. /// <include file='CodeDoc/DockPaneCaptionVS2003.xml' path='//CodeDoc/Class[@name="DockPaneCaptionVS2003"]/Property[@name="TextGapLeft"]/*'/>
  52. protected virtual int TextGapLeft
  53. {
  54. get { return _TextGapLeft; }
  55. }
  56. /// <include file='CodeDoc/DockPaneCaptionVS2003.xml' path='//CodeDoc/Class[@name="DockPaneCaptionVS2003"]/Property[@name="TextGapRight"]/*'/>
  57. protected virtual int TextGapRight
  58. {
  59. get { return _TextGapRight; }
  60. }
  61. /// <include file='CodeDoc/DockPaneCaptionVS2003.xml' path='//CodeDoc/Class[@name="DockPaneCaptionVS2003"]/Property[@name="ButtonGapTop"]/*'/>
  62. protected virtual int ButtonGapTop
  63. {
  64. get { return _ButtonGapTop; }
  65. }
  66. /// <include file='CodeDoc/DockPaneCaptionVS2003.xml' path='//CodeDoc/Class[@name="DockPaneCaptionVS2003"]/Property[@name="ButtonGapBottom"]/*'/>
  67. protected virtual int ButtonGapBottom
  68. {
  69. get { return _ButtonGapBottom; }
  70. }
  71. /// <include file='CodeDoc/DockPaneCaptionVS2003.xml' path='//CodeDoc/Class[@name="DockPaneCaptionVS2003"]/Property[@name="ButtonGapLeft"]/*'/>
  72. protected virtual int ButtonGapLeft
  73. {
  74. get { return _ButtonGapLeft; }
  75. }
  76. /// <include file='CodeDoc/DockPaneCaptionVS2003.xml' path='//CodeDoc/Class[@name="DockPaneCaptionVS2003"]/Property[@name="ButtonGapRight"]/*'/>
  77. protected virtual int ButtonGapRight
  78. {
  79. get { return _ButtonGapRight; }
  80. }
  81. /// <include file='CodeDoc/DockPaneCaptionVS2003.xml' path='//CodeDoc/Class[@name="DockPaneCaptionVS2003"]/Property[@name="ButtonGapBetween"]/*'/>
  82. protected virtual int ButtonGapBetween
  83. {
  84. get { return _ButtonGapBetween; }
  85. }
  86. private static Image _imageCloseEnabled = null;
  87. /// <include file='CodeDoc/DockPaneCaptionVS2003.xml' path='//CodeDoc/Class[@name="DockPaneCaptionVS2003"]/Property[@name="ImageCloseEnabled"]/*'/>
  88. protected virtual Image ImageCloseEnabled
  89. {
  90. get
  91. {
  92. if (_imageCloseEnabled == null)
  93. _imageCloseEnabled = Resources.DockPaneCaption_CloseEnabled;
  94. return _imageCloseEnabled;
  95. }
  96. }
  97. private static Image _imageCloseDisabled = null;
  98. /// <include file='CodeDoc/DockPaneCaptionVS2003.xml' path='//CodeDoc/Class[@name="DockPaneCaptionVS2003"]/Property[@name="ImageCloseDisabled"]/*'/>
  99. protected virtual Image ImageCloseDisabled
  100. {
  101. get
  102. {
  103. if (_imageCloseDisabled == null)
  104. _imageCloseDisabled = Resources.DockPaneCaption_CloseDisabled;
  105. return _imageCloseDisabled;
  106. }
  107. }
  108. private static Image _imageAutoHideYes = null;
  109. /// <include file='CodeDoc/DockPaneCaptionVS2003.xml' path='//CodeDoc/Class[@name="DockPaneCaptionVS2003"]/Property[@name="ImageAutoHideYes"]/*'/>
  110. protected virtual Image ImageAutoHideYes
  111. {
  112. get
  113. {
  114. if (_imageAutoHideYes == null)
  115. _imageAutoHideYes = Resources.DockPaneCaption_AutoHideYes;
  116. return _imageAutoHideYes;
  117. }
  118. }
  119. private static Image _imageAutoHideNo = null;
  120. /// <include file='CodeDoc/DockPaneCaptionVS2003.xml' path='//CodeDoc/Class[@name="DockPaneCaptionVS2003"]/Property[@name="ImageAutoHideNo"]/*'/>
  121. protected virtual Image ImageAutoHideNo
  122. {
  123. get
  124. {
  125. if (_imageAutoHideNo == null)
  126. _imageAutoHideNo = Resources.DockPaneCaption_AutoHideNo;
  127. return _imageAutoHideNo;
  128. }
  129. }
  130. private static string _toolTipClose = null;
  131. /// <include file='CodeDoc/DockPaneCaptionVS2003.xml' path='//CodeDoc/Class[@name="DockPaneCaptionVS2003"]/Property[@name="ToolTipClose"]/*'/>
  132. protected virtual string ToolTipClose
  133. {
  134. get
  135. {
  136. if (_toolTipClose == null)
  137. _toolTipClose = Strings.DockPaneCaption_ToolTipClose;
  138. return _toolTipClose;
  139. }
  140. }
  141. private static string _toolTipAutoHide = null;
  142. /// <include file='CodeDoc/DockPaneCaptionVS2003.xml' path='//CodeDoc/Class[@name="DockPaneCaptionVS2003"]/Property[@name="ToolTipAutoHide"]/*'/>
  143. protected virtual string ToolTipAutoHide
  144. {
  145. get
  146. {
  147. if (_toolTipAutoHide == null)
  148. _toolTipAutoHide = Strings.DockPaneCaption_ToolTipAutoHide;
  149. return _toolTipAutoHide;
  150. }
  151. }
  152. /// <include file='CodeDoc/DockPaneCaptionVS2003.xml' path='//CodeDoc/Class[@name="DockPaneCaptionVS2003"]/Property[@name="ActiveBackColor"]/*'/>
  153. protected virtual Color ActiveBackColor
  154. {
  155. get { return SystemColors.ActiveCaption; }
  156. }
  157. /// <include file='CodeDoc/DockPaneCaptionVS2003.xml' path='//CodeDoc/Class[@name="DockPaneCaptionVS2003"]/Property[@name="InactiveBackColor"]/*'/>
  158. protected virtual Color InactiveBackColor
  159. {
  160. get { return SystemColors.Control; }
  161. }
  162. /// <include file='CodeDoc/DockPaneCaptionVS2003.xml' path='//CodeDoc/Class[@name="DockPaneCaptionVS2003"]/Property[@name="ActiveTextColor"]/*'/>
  163. protected virtual Color ActiveTextColor
  164. {
  165. get { return SystemColors.ActiveCaptionText; }
  166. }
  167. /// <include file='CodeDoc/DockPaneCaptionVS2003.xml' path='//CodeDoc/Class[@name="DockPaneCaptionVS2003"]/Property[@name="InactiveTextColor"]/*'/>
  168. protected virtual Color InactiveTextColor
  169. {
  170. get { return SystemColors.ControlText; }
  171. }
  172. /// <include file='CodeDoc/DockPaneCaptionVS2003.xml' path='//CodeDoc/Class[@name="DockPaneCaptionVS2003"]/Property[@name="InactiveBorderColor"]/*'/>
  173. protected virtual Color InactiveBorderColor
  174. {
  175. get { return SystemColors.GrayText; }
  176. }
  177. /// <include file='CodeDoc/DockPaneCaptionVS2003.xml' path='//CodeDoc/Class[@name="DockPaneCaptionVS2003"]/Property[@name="ActiveButtonBorderColor"]/*'/>
  178. protected virtual Color ActiveButtonBorderColor
  179. {
  180. get { return ActiveTextColor; }
  181. }
  182. /// <include file='CodeDoc/DockPaneCaptionVS2003.xml' path='//CodeDoc/Class[@name="DockPaneCaptionVS2003"]/Property[@name="InactiveButtonBorderColor"]/*'/>
  183. protected virtual Color InactiveButtonBorderColor
  184. {
  185. get { return Color.Empty; }
  186. }
  187. private static TextFormatFlags _textFormat =
  188.             TextFormatFlags.SingleLine |
  189.             TextFormatFlags.EndEllipsis |
  190.             TextFormatFlags.VerticalCenter;
  191. /// <include file='CodeDoc/DockPaneCaptionVS2003.xml' path='//CodeDoc/Class[@name="DockPaneCaptionVS2003"]/Property[@name="TextStringFormat"]/*'/>
  192. protected virtual TextFormatFlags TextFormat
  193. {
  194.             get
  195.             {
  196.                 return _textFormat;
  197.             }
  198. }
  199. #endregion
  200. /// <exclude/>
  201. protected override int MeasureHeight()
  202. {
  203. int height = Font.Height + TextGapTop + TextGapBottom;
  204. if (height < ImageCloseEnabled.Height + ButtonGapTop + ButtonGapBottom)
  205. height = ImageCloseEnabled.Height + ButtonGapTop + ButtonGapBottom;
  206. return height;
  207. }
  208. /// <exclude/>
  209. protected override void OnPaint(PaintEventArgs e)
  210. {
  211. base.OnPaint (e);
  212. DrawCaption(e.Graphics);
  213. }
  214. private void DrawCaption(Graphics g)
  215. {
  216. BackColor = DockPane.IsActivated ? ActiveBackColor : InactiveBackColor;
  217. Rectangle rectCaption = ClientRectangle;
  218. if (!DockPane.IsActivated)
  219. {
  220. using (Pen pen = new Pen(InactiveBorderColor))
  221. {
  222. g.DrawLine(pen, rectCaption.X + 1, rectCaption.Y, rectCaption.X + rectCaption.Width - 2, rectCaption.Y);
  223. g.DrawLine(pen, rectCaption.X + 1, rectCaption.Y + rectCaption.Height - 1, rectCaption.X + rectCaption.Width - 2, rectCaption.Y + rectCaption.Height - 1);
  224. g.DrawLine(pen, rectCaption.X, rectCaption.Y + 1, rectCaption.X, rectCaption.Y + rectCaption.Height - 2);
  225. g.DrawLine(pen, rectCaption.X + rectCaption.Width - 1, rectCaption.Y + 1, rectCaption.X + rectCaption.Width - 1, rectCaption.Y + rectCaption.Height - 2);
  226. }
  227. }
  228. m_buttonClose.ForeColor = m_buttonAutoHide.ForeColor = (DockPane.IsActivated ? ActiveTextColor : InactiveTextColor);
  229. m_buttonClose.BorderColor = m_buttonAutoHide.BorderColor = (DockPane.IsActivated ? ActiveButtonBorderColor : InactiveButtonBorderColor);
  230. Rectangle rectCaptionText = rectCaption;
  231. rectCaptionText.X += TextGapLeft;
  232. if (ShouldShowCloseButton && ShouldShowAutoHideButton)
  233. rectCaptionText.Width = rectCaption.Width - ButtonGapRight
  234. - ButtonGapLeft - TextGapLeft - TextGapRight -
  235. (m_buttonAutoHide.Width + ButtonGapBetween + m_buttonClose.Width);
  236. else if (ShouldShowCloseButton || ShouldShowAutoHideButton)
  237. rectCaptionText.Width = rectCaption.Width - ButtonGapRight
  238. - ButtonGapLeft - TextGapLeft - TextGapRight - m_buttonClose.Width;
  239. else
  240. rectCaptionText.Width = rectCaption.Width - TextGapLeft - TextGapRight;
  241. rectCaptionText.Y += TextGapTop;
  242. rectCaptionText.Height -= TextGapTop + TextGapBottom;
  243.             TextRenderer.DrawText(g, DockPane.CaptionText, Font, rectCaptionText, DockPane.IsActivated ? ActiveTextColor : InactiveTextColor, TextFormat);
  244. }
  245. /// <exclude/>
  246. protected override void OnLayout(LayoutEventArgs levent)
  247. {
  248. SetButtonsPosition();
  249. base.OnLayout (levent);
  250. }
  251. /// <exclude/>
  252. protected override void OnRefreshChanges()
  253. {
  254. SetButtons();
  255. Invalidate();
  256. }
  257. private bool ShouldShowCloseButton
  258. {
  259. get { return (DockPane.ActiveContent != null)? DockPane.ActiveContent.DockHandler.CloseButton : false; }
  260. }
  261. private bool ShouldShowAutoHideButton
  262. {
  263. get { return !DockPane.IsFloat; }
  264. }
  265. private void SetButtons()
  266. {
  267. m_buttonClose.Visible = ShouldShowCloseButton;
  268. m_buttonAutoHide.Visible = ShouldShowAutoHideButton;
  269. m_buttonAutoHide.ImageEnabled = DockPane.IsAutoHide ? ImageAutoHideYes : ImageAutoHideNo;
  270. SetButtonsPosition();
  271. }
  272. private void SetButtonsPosition()
  273. {
  274. // set the size and location for close and auto-hide buttons
  275. Rectangle rectCaption = ClientRectangle;
  276. int buttonWidth = ImageCloseEnabled.Width;
  277. int buttonHeight = ImageCloseEnabled.Height;
  278. int height = rectCaption.Height - ButtonGapTop - ButtonGapBottom;
  279. if (buttonHeight < height)
  280. {
  281. buttonWidth = buttonWidth * (height / buttonHeight);
  282. buttonHeight = height;
  283. }
  284. m_buttonClose.SuspendLayout();
  285. m_buttonAutoHide.SuspendLayout();
  286. Size buttonSize = new Size(buttonWidth, buttonHeight);
  287. m_buttonClose.Size = m_buttonAutoHide.Size = buttonSize;
  288. int x = rectCaption.X + rectCaption.Width - 1 - ButtonGapRight - m_buttonClose.Width;
  289. int y = rectCaption.Y + ButtonGapTop;
  290. Point point = m_buttonClose.Location = new Point(x, y);
  291. if (ShouldShowCloseButton)
  292. point.Offset(-(m_buttonAutoHide.Width + ButtonGapBetween), 0);
  293. m_buttonAutoHide.Location = point;
  294. m_buttonClose.ResumeLayout();
  295. m_buttonAutoHide.ResumeLayout();
  296. }
  297. private void Close_Click(object sender, EventArgs e)
  298. {
  299. DockPane.CloseActiveContent();
  300. }
  301. private void AutoHide_Click(object sender, EventArgs e)
  302. {
  303. DockPane.DockState = DockHelper.ToggleAutoHideState(DockPane.DockState);
  304.             if (!DockPane.IsAutoHide)
  305.     DockPane.Activate();
  306. }
  307. }
  308. }