VS2005DockPaneCaption.cs 17 KB

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