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

C#编程

开发平台:

C#

  1. using System;
  2. using System.Drawing;
  3. using System.Windows.Forms;
  4. using System.Drawing.Drawing2D;
  5. using System.ComponentModel;
  6. namespace WeifenLuo.WinFormsUI.Docking
  7. {
  8. internal class VS2005AutoHideStrip : AutoHideStripBase
  9. {
  10.         private class TabVS2005 : Tab
  11.         {
  12.             internal TabVS2005(IDockContent content)
  13.                 : base(content)
  14.             {
  15.             }
  16.             private int m_tabX = 0;
  17.             public int TabX
  18.             {
  19.                 get { return m_tabX; }
  20.                 set { m_tabX = value; }
  21.             }
  22.             private int m_tabWidth = 0;
  23.             public int TabWidth
  24.             {
  25.                 get { return m_tabWidth; }
  26.                 set { m_tabWidth = value; }
  27.             }
  28.         }
  29. private const int _ImageHeight = 16;
  30. private const int _ImageWidth = 16;
  31. private const int _ImageGapTop = 2;
  32. private const int _ImageGapLeft = 4;
  33. private const int _ImageGapRight = 2;
  34. private const int _ImageGapBottom = 2;
  35. private const int _TextGapLeft = 0;
  36. private const int _TextGapRight = 0;
  37. private const int _TabGapTop = 3;
  38. private const int _TabGapLeft = 4;
  39. private const int _TabGapBetween = 10;
  40. #region Customizable Properties
  41.         private static Font TextFont
  42.         {
  43.             get { return SystemInformation.MenuFont; }
  44.         }
  45. private static StringFormat _stringFormatTabHorizontal;
  46. private StringFormat StringFormatTabHorizontal
  47. {
  48. get
  49. {
  50. if (_stringFormatTabHorizontal == null)
  51. {
  52. _stringFormatTabHorizontal = new StringFormat();
  53. _stringFormatTabHorizontal.Alignment = StringAlignment.Near;
  54. _stringFormatTabHorizontal.LineAlignment = StringAlignment.Center;
  55. _stringFormatTabHorizontal.FormatFlags = StringFormatFlags.NoWrap;
  56.     _stringFormatTabHorizontal.Trimming = StringTrimming.None;
  57. }
  58.                 if (RightToLeft == RightToLeft.Yes)
  59.                     _stringFormatTabHorizontal.FormatFlags |= StringFormatFlags.DirectionRightToLeft;
  60.                 else
  61.                     _stringFormatTabHorizontal.FormatFlags &= ~StringFormatFlags.DirectionRightToLeft;
  62. return _stringFormatTabHorizontal;
  63. }
  64. }
  65. private static StringFormat _stringFormatTabVertical;
  66. private StringFormat StringFormatTabVertical
  67. {
  68. get
  69. {
  70. if (_stringFormatTabVertical == null)
  71. {
  72. _stringFormatTabVertical = new StringFormat();
  73. _stringFormatTabVertical.Alignment = StringAlignment.Near;
  74. _stringFormatTabVertical.LineAlignment = StringAlignment.Center;
  75. _stringFormatTabVertical.FormatFlags = StringFormatFlags.NoWrap | StringFormatFlags.DirectionVertical;
  76.     _stringFormatTabVertical.Trimming = StringTrimming.None;
  77. }
  78.                 if (RightToLeft == RightToLeft.Yes)
  79.                     _stringFormatTabVertical.FormatFlags |= StringFormatFlags.DirectionRightToLeft;
  80.                 else
  81.                     _stringFormatTabVertical.FormatFlags &= ~StringFormatFlags.DirectionRightToLeft;
  82.                 return _stringFormatTabVertical;
  83. }
  84. }
  85. private static int ImageHeight
  86. {
  87. get { return _ImageHeight; }
  88. }
  89. private static int ImageWidth
  90. {
  91. get { return _ImageWidth; }
  92. }
  93. private static int ImageGapTop
  94. {
  95. get { return _ImageGapTop; }
  96. }
  97. private static int ImageGapLeft
  98. {
  99. get { return _ImageGapLeft; }
  100. }
  101. private static int ImageGapRight
  102. {
  103. get { return _ImageGapRight; }
  104. }
  105. private static int ImageGapBottom
  106. {
  107. get { return _ImageGapBottom; }
  108. }
  109. private static int TextGapLeft
  110. {
  111. get { return _TextGapLeft; }
  112. }
  113. private static int TextGapRight
  114. {
  115. get { return _TextGapRight; }
  116. }
  117. private static int TabGapTop
  118. {
  119. get { return _TabGapTop; }
  120. }
  121. private static int TabGapLeft
  122. {
  123. get { return _TabGapLeft; }
  124. }
  125. private static int TabGapBetween
  126. {
  127. get { return _TabGapBetween; }
  128. }
  129. private static Pen PenTabBorder
  130. {
  131. get { return SystemPens.GrayText; }
  132. }
  133. #endregion
  134.         private static Matrix _matrixIdentity = new Matrix();
  135.         private static Matrix MatrixIdentity
  136. {
  137.             get { return _matrixIdentity; }
  138. }
  139.         private static DockState[] _dockStates;
  140.         private static DockState[] DockStates
  141. {
  142. get
  143.             {
  144.                 if (_dockStates == null)
  145.                 {
  146.                     _dockStates = new DockState[4];
  147.                     _dockStates[0] = DockState.DockLeftAutoHide;
  148.                     _dockStates[1] = DockState.DockRightAutoHide;
  149.                     _dockStates[2] = DockState.DockTopAutoHide;
  150.                     _dockStates[3] = DockState.DockBottomAutoHide;
  151.                 }
  152.                 return _dockStates;
  153.             }
  154. }
  155.         private static GraphicsPath _graphicsPath;
  156.         internal static GraphicsPath GraphicsPath
  157.         {
  158.             get
  159.             {
  160.                 if (_graphicsPath == null)
  161.                     _graphicsPath = new GraphicsPath();
  162.                 return _graphicsPath;
  163.             }
  164.         }
  165. public VS2005AutoHideStrip(DockPanel panel) : base(panel)
  166. {
  167. SetStyle(ControlStyles.ResizeRedraw | 
  168.                 ControlStyles.UserPaint |
  169.                 ControlStyles.AllPaintingInWmPaint |
  170.                 ControlStyles.OptimizedDoubleBuffer, true);
  171.             BackColor = SystemColors.ControlLight;
  172. }
  173. protected override void OnPaint(PaintEventArgs e)
  174. {
  175. Graphics g = e.Graphics;
  176.             
  177.             Color startColor = DockPanel.Skin.AutoHideStripSkin.DockStripGradient.StartColor;
  178.             Color endColor = DockPanel.Skin.AutoHideStripSkin.DockStripGradient.EndColor;
  179.             LinearGradientMode gradientMode = DockPanel.Skin.AutoHideStripSkin.DockStripGradient.LinearGradientMode;
  180.             using (LinearGradientBrush brush = new LinearGradientBrush(ClientRectangle, startColor, endColor, gradientMode))
  181.             {
  182.                 g.FillRectangle(brush, ClientRectangle);
  183.             }
  184.             DrawTabStrip(g);
  185. }
  186. protected override void OnLayout(LayoutEventArgs levent)
  187. {
  188. CalculateTabs();
  189. base.OnLayout (levent);
  190. }
  191. private void DrawTabStrip(Graphics g)
  192. {
  193. DrawTabStrip(g, DockState.DockTopAutoHide);
  194. DrawTabStrip(g, DockState.DockBottomAutoHide);
  195. DrawTabStrip(g, DockState.DockLeftAutoHide);
  196. DrawTabStrip(g, DockState.DockRightAutoHide);
  197. }
  198. private void DrawTabStrip(Graphics g, DockState dockState)
  199. {
  200. Rectangle rectTabStrip = GetLogicalTabStripRectangle(dockState);
  201. if (rectTabStrip.IsEmpty)
  202. return;
  203.             Matrix matrixIdentity = g.Transform;
  204.             if (dockState == DockState.DockLeftAutoHide || dockState == DockState.DockRightAutoHide)
  205. {
  206. Matrix matrixRotated = new Matrix();
  207. matrixRotated.RotateAt(90, new PointF((float)rectTabStrip.X + (float)rectTabStrip.Height / 2,
  208. (float)rectTabStrip.Y + (float)rectTabStrip.Height / 2));
  209. g.Transform = matrixRotated;
  210. }
  211. foreach (Pane pane in GetPanes(dockState))
  212. {
  213. foreach (TabVS2005 tab in pane.AutoHideTabs)
  214. DrawTab(g, tab);
  215. }
  216. g.Transform = matrixIdentity;
  217. }
  218. private void CalculateTabs()
  219. {
  220. CalculateTabs(DockState.DockTopAutoHide);
  221. CalculateTabs(DockState.DockBottomAutoHide);
  222. CalculateTabs(DockState.DockLeftAutoHide);
  223. CalculateTabs(DockState.DockRightAutoHide);
  224. }
  225.         private void CalculateTabs(DockState dockState)
  226.         {
  227.             Rectangle rectTabStrip = GetLogicalTabStripRectangle(dockState);
  228.             int imageHeight = rectTabStrip.Height - ImageGapTop - ImageGapBottom;
  229.             int imageWidth = ImageWidth;
  230.             if (imageHeight > ImageHeight)
  231.                 imageWidth = ImageWidth * (imageHeight / ImageHeight);
  232.             int x = TabGapLeft + rectTabStrip.X;
  233.             foreach (Pane pane in GetPanes(dockState))
  234.             {
  235.                 foreach (TabVS2005 tab in pane.AutoHideTabs)
  236.                 {
  237.                     int width = imageWidth + ImageGapLeft + ImageGapRight +
  238.                         TextRenderer.MeasureText(tab.Content.DockHandler.TabText, TextFont).Width +
  239.                         TextGapLeft + TextGapRight;
  240.                     tab.TabX = x;
  241.                     tab.TabWidth = width;
  242.                     x += width;
  243.                 }
  244.                 x += TabGapBetween;
  245.             }
  246.         }
  247.         private Rectangle RtlTransform(Rectangle rect, DockState dockState)
  248.         {
  249.             Rectangle rectTransformed;
  250.             if (dockState == DockState.DockLeftAutoHide || dockState == DockState.DockRightAutoHide)
  251.                 rectTransformed = rect;
  252.             else
  253.                 rectTransformed = DrawHelper.RtlTransform(this, rect);
  254.             return rectTransformed;
  255.         }
  256.         private GraphicsPath GetTabOutline(TabVS2005 tab, bool transformed, bool rtlTransform)
  257.         {
  258.             DockState dockState = tab.Content.DockHandler.DockState;
  259.             Rectangle rectTab = GetTabRectangle(tab, transformed);
  260.             if (rtlTransform)
  261.                 rectTab = RtlTransform(rectTab, dockState);
  262.             bool upTab = (dockState == DockState.DockLeftAutoHide || dockState == DockState.DockBottomAutoHide);
  263.             DrawHelper.GetRoundedCornerTab(GraphicsPath, rectTab, upTab);
  264.             return GraphicsPath;
  265.         }
  266. private void DrawTab(Graphics g, TabVS2005 tab)
  267. {
  268. Rectangle rectTabOrigin = GetTabRectangle(tab);
  269. if (rectTabOrigin.IsEmpty)
  270. return;
  271. DockState dockState = tab.Content.DockHandler.DockState;
  272. IDockContent content = tab.Content;
  273.             GraphicsPath path = GetTabOutline(tab, false, true);
  274.             Color startColor = DockPanel.Skin.AutoHideStripSkin.TabGradient.StartColor;
  275.             Color endColor = DockPanel.Skin.AutoHideStripSkin.TabGradient.EndColor;
  276.             LinearGradientMode gradientMode = DockPanel.Skin.AutoHideStripSkin.TabGradient.LinearGradientMode;
  277.             g.FillPath(new LinearGradientBrush(rectTabOrigin, startColor, endColor, gradientMode), path);
  278.             g.DrawPath(PenTabBorder, path);
  279.             // Set no rotate for drawing icon and text
  280. Matrix matrixRotate = g.Transform;
  281. g.Transform = MatrixIdentity;
  282. // Draw the icon
  283. Rectangle rectImage = rectTabOrigin;
  284. rectImage.X += ImageGapLeft;
  285. rectImage.Y += ImageGapTop;
  286. int imageHeight = rectTabOrigin.Height - ImageGapTop - ImageGapBottom;
  287. int imageWidth = ImageWidth;
  288. if (imageHeight > ImageHeight)
  289. imageWidth = ImageWidth * (imageHeight/ImageHeight);
  290. rectImage.Height = imageHeight;
  291. rectImage.Width = imageWidth;
  292. rectImage = GetTransformedRectangle(dockState, rectImage);
  293. g.DrawIcon(((Form)content).Icon, RtlTransform(rectImage, dockState));
  294. // Draw the text
  295. Rectangle rectText = rectTabOrigin;
  296. rectText.X += ImageGapLeft + imageWidth + ImageGapRight + TextGapLeft;
  297. rectText.Width -= ImageGapLeft + imageWidth + ImageGapRight + TextGapLeft;
  298. rectText = RtlTransform(GetTransformedRectangle(dockState, rectText), dockState);
  299.             Color textColor = DockPanel.Skin.AutoHideStripSkin.TabGradient.TextColor;
  300.             
  301. if (dockState == DockState.DockLeftAutoHide || dockState == DockState.DockRightAutoHide)
  302. g.DrawString(content.DockHandler.TabText, TextFont, new SolidBrush(textColor), rectText, StringFormatTabVertical);
  303. else
  304.                 g.DrawString(content.DockHandler.TabText, TextFont, new SolidBrush(textColor), rectText, StringFormatTabHorizontal);
  305. // Set rotate back
  306. g.Transform = matrixRotate;
  307. }
  308. private Rectangle GetLogicalTabStripRectangle(DockState dockState)
  309. {
  310. return GetLogicalTabStripRectangle(dockState, false);
  311. }
  312. private Rectangle GetLogicalTabStripRectangle(DockState dockState, bool transformed)
  313. {
  314. if (!DockHelper.IsDockStateAutoHide(dockState))
  315. return Rectangle.Empty;
  316. int leftPanes = GetPanes(DockState.DockLeftAutoHide).Count;
  317. int rightPanes = GetPanes(DockState.DockRightAutoHide).Count;
  318. int topPanes = GetPanes(DockState.DockTopAutoHide).Count;
  319. int bottomPanes = GetPanes(DockState.DockBottomAutoHide).Count;
  320. int x, y, width, height;
  321. height = MeasureHeight();
  322. if (dockState == DockState.DockLeftAutoHide && leftPanes > 0)
  323. {
  324. x = 0;
  325. y = (topPanes == 0) ? 0 : height;
  326. width = Height - (topPanes == 0 ? 0 : height) - (bottomPanes == 0 ? 0 :height);
  327. }
  328. else if (dockState == DockState.DockRightAutoHide && rightPanes > 0)
  329. {
  330. x = Width - height;
  331. if (leftPanes != 0 && x < height)
  332. x = height;
  333. y = (topPanes == 0) ? 0 : height;
  334. width = Height - (topPanes == 0 ? 0 : height) - (bottomPanes == 0 ? 0 :height);
  335. }
  336. else if (dockState == DockState.DockTopAutoHide && topPanes > 0)
  337. {
  338. x = leftPanes == 0 ? 0 : height;
  339. y = 0;
  340. width = Width - (leftPanes == 0 ? 0 : height) - (rightPanes == 0 ? 0 : height);
  341. }
  342. else if (dockState == DockState.DockBottomAutoHide && bottomPanes > 0)
  343. {
  344. x = leftPanes == 0 ? 0 : height;
  345. y = Height - height;
  346. if (topPanes != 0 && y < height)
  347. y = height;
  348. width = Width - (leftPanes == 0 ? 0 : height) - (rightPanes == 0 ? 0 : height);
  349. }
  350. else
  351. return Rectangle.Empty;
  352. if (!transformed)
  353. return new Rectangle(x, y, width, height);
  354. else
  355. return GetTransformedRectangle(dockState, new Rectangle(x, y, width, height));
  356. }
  357. private Rectangle GetTabRectangle(TabVS2005 tab)
  358. {
  359. return GetTabRectangle(tab, false);
  360. }
  361. private Rectangle GetTabRectangle(TabVS2005 tab, bool transformed)
  362. {
  363. DockState dockState = tab.Content.DockHandler.DockState;
  364. Rectangle rectTabStrip = GetLogicalTabStripRectangle(dockState);
  365. if (rectTabStrip.IsEmpty)
  366. return Rectangle.Empty;
  367. int x = tab.TabX;
  368. int y = rectTabStrip.Y + 
  369. (dockState == DockState.DockTopAutoHide || dockState == DockState.DockRightAutoHide ?
  370. 0 : TabGapTop);
  371. int width = tab.TabWidth;
  372. int height = rectTabStrip.Height - TabGapTop;
  373. if (!transformed)
  374. return new Rectangle(x, y, width, height);
  375. else
  376. return GetTransformedRectangle(dockState, new Rectangle(x, y, width, height));
  377. }
  378. private Rectangle GetTransformedRectangle(DockState dockState, Rectangle rect)
  379. {
  380. if (dockState != DockState.DockLeftAutoHide && dockState != DockState.DockRightAutoHide)
  381. return rect;
  382. PointF[] pts = new PointF[1];
  383. // the center of the rectangle
  384. pts[0].X = (float)rect.X + (float)rect.Width / 2;
  385. pts[0].Y = (float)rect.Y + (float)rect.Height / 2;
  386. Rectangle rectTabStrip = GetLogicalTabStripRectangle(dockState);
  387. Matrix matrix = new Matrix();
  388. matrix.RotateAt(90, new PointF((float)rectTabStrip.X + (float)rectTabStrip.Height / 2,
  389. (float)rectTabStrip.Y + (float)rectTabStrip.Height / 2));
  390. matrix.TransformPoints(pts);
  391. return new Rectangle((int)(pts[0].X - (float)rect.Height / 2 + .5F),
  392. (int)(pts[0].Y - (float)rect.Width / 2 + .5F),
  393. rect.Height, rect.Width);
  394. }
  395. protected override IDockContent HitTest(Point ptMouse)
  396. {
  397. foreach(DockState state in DockStates)
  398. {
  399. Rectangle rectTabStrip = GetLogicalTabStripRectangle(state, true);
  400. if (!rectTabStrip.Contains(ptMouse))
  401. continue;
  402. foreach(Pane pane in GetPanes(state))
  403. {
  404.                     DockState dockState = pane.DockPane.DockState;
  405. foreach(TabVS2005 tab in pane.AutoHideTabs)
  406. {
  407.                         GraphicsPath path = GetTabOutline(tab, true, true);
  408.                         if (path.IsVisible(ptMouse))
  409. return tab.Content;
  410. }
  411. }
  412. }
  413. return null;
  414. }
  415. protected internal override int MeasureHeight()
  416. {
  417. return Math.Max(ImageGapBottom +
  418. ImageGapTop + ImageHeight,
  419. TextFont.Height) + TabGapTop;
  420. }
  421. protected override void OnRefreshChanges()
  422. {
  423. CalculateTabs();
  424. Invalidate();
  425. }
  426.         protected override AutoHideStripBase.Tab CreateTab(IDockContent content)
  427.         {
  428.             return new TabVS2005(content);
  429.         }
  430. }
  431. }