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