TabControlEx.cs 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Text;
  4. using System.Windows.Forms;
  5. using System.IO;
  6. using System.Drawing;
  7. using System.Drawing.Drawing2D;
  8. using System.Drawing.Text;
  9. using System.ComponentModel;
  10. namespace LYFZ.ComponentLibrary
  11. {
  12. [ToolboxBitmap(typeof(TabControl))]
  13. public class TabControlEx : System.Windows.Forms.TabControl
  14. {
  15. // private Image _titleBackground = Properties.Resources.tab02;
  16. private Color _baseColor = Color.White;
  17. private Color _backColor = Color.Transparent;
  18. private Color _borderColor = Color.White;
  19. private Color _pageColor = Color.White;
  20. private bool _isFocus;
  21. private Rectangle _btnArrowRect = Rectangle.Empty;
  22. private Image Icon;
  23. [Category("组件扩展属性"), DefaultValue(typeof(Color), "102, 180, 228")]
  24. public Color BaseColor
  25. {
  26. get
  27. {
  28. return this._baseColor;
  29. }
  30. set
  31. {
  32. this._baseColor = value;
  33. base.Invalidate(true);
  34. }
  35. }
  36. [Browsable(true), Category("组件扩展属性"), DefaultValue(typeof(Color), "Transparent")]
  37. public override Color BackColor
  38. {
  39. get
  40. {
  41. return this._backColor;
  42. }
  43. set
  44. {
  45. this._backColor = value;
  46. base.Invalidate(true);
  47. }
  48. }
  49. [Category("组件扩展属性"), DefaultValue(typeof(Color), "102, 180, 228")]
  50. public Color BorderColor
  51. {
  52. get
  53. {
  54. return this._borderColor;
  55. }
  56. set
  57. {
  58. this._borderColor = value;
  59. base.Invalidate(true);
  60. }
  61. }
  62. [Category("组件扩展属性"), Description("所有TabPage的背景颜色")]
  63. public Color PageColor
  64. {
  65. get
  66. {
  67. return this._pageColor;
  68. }
  69. set
  70. {
  71. this._pageColor = value;
  72. if (base.TabPages.Count > 0)
  73. {
  74. for (int i = 0; i < base.TabPages.Count; i++)
  75. {
  76. base.TabPages[i].BackColor = this._pageColor;
  77. }
  78. }
  79. base.Invalidate(true);
  80. }
  81. }
  82. public TabControlEx()
  83. {
  84. base.SetStyle(ControlStyles.UserPaint | ControlStyles.ResizeRedraw | ControlStyles.SupportsTransparentBackColor | ControlStyles.AllPaintingInWmPaint | ControlStyles.DoubleBuffer | ControlStyles.OptimizedDoubleBuffer, true);
  85. base.SizeMode = TabSizeMode.Fixed;
  86. base.ItemSize = new Size(70, 36);
  87. base.UpdateStyles();
  88. }
  89. protected override void OnPaint(PaintEventArgs e)
  90. {
  91. base.OnPaint(e);
  92. Graphics g = e.Graphics;
  93. g.SmoothingMode = SmoothingMode.AntiAlias;
  94. g.InterpolationMode = InterpolationMode.HighQualityBilinear;
  95. g.TextRenderingHint = TextRenderingHint.AntiAlias;
  96. this.DrawBackground(g);
  97. this.DrawTabPages(g);
  98. }
  99. protected override void OnMouseMove(MouseEventArgs e)
  100. {
  101. base.OnMouseMove(e);
  102. }
  103. protected override void OnMouseLeave(EventArgs e)
  104. {
  105. base.OnMouseLeave(e);
  106. }
  107. protected override void OnMouseDown(MouseEventArgs e)
  108. {
  109. base.OnMouseDown(e);
  110. if (!base.DesignMode && e.Button == MouseButtons.Left && this._btnArrowRect.Contains(e.Location))
  111. {
  112. this._isFocus = true;
  113. base.Invalidate(this._btnArrowRect);
  114. }
  115. }
  116. protected override void WndProc(ref Message m)
  117. {
  118. if (m.Msg != 123)
  119. {
  120. base.WndProc(ref m);
  121. }
  122. }
  123. private void DrawBackground(Graphics g)
  124. {
  125. int arg_0E_0 = base.ClientRectangle.Width;
  126. int arg_1D_0 = base.ClientRectangle.Height;
  127. int arg_2D_0 = this.DisplayRectangle.Height;
  128. Color backColor = base.Enabled ? this._backColor : SystemColors.Control;
  129. using (SolidBrush brush = new SolidBrush(backColor))
  130. {
  131. g.FillRectangle(brush, base.ClientRectangle);
  132. }
  133. }
  134. private void DrawImage(Graphics g, Image image, Rectangle rect)
  135. {
  136. g.DrawImage(image, new Rectangle(rect.X, rect.Y, 5, rect.Height), 0, 0, 5, image.Height, GraphicsUnit.Pixel);
  137. g.DrawImage(image, new Rectangle(rect.X + 5, rect.Y, rect.Width - 10, rect.Height), 5, 0, image.Width - 10, image.Height, GraphicsUnit.Pixel);
  138. g.DrawImage(image, new Rectangle(rect.X + rect.Width - 5, rect.Y, 5, rect.Height), image.Width - 5, 0, 5, image.Height, GraphicsUnit.Pixel);
  139. }
  140. private void DrawTabPages(Graphics g)
  141. {
  142. using (SolidBrush brush = new SolidBrush(this._pageColor))
  143. {
  144. int x = 2;
  145. int y = base.ItemSize.Height;
  146. int width = base.Width - 2;
  147. int height = base.Height - base.ItemSize.Height;
  148. g.FillRectangle(brush, x, y, width, height);
  149. g.DrawRectangle(new Pen(this._borderColor), x, y, width - 1, height - 1);
  150. }
  151. Rectangle tabRect = Rectangle.Empty;
  152. Point cursorPoint = base.PointToClient(Control.MousePosition);
  153. for (int i = 0; i < base.TabCount; i++)
  154. {
  155. TabPage page = base.TabPages[i];
  156. tabRect = base.GetTabRect(i);
  157. Color arg_AB_0 = Color.Yellow;
  158. this.Icon = ((base.TabPages[i].ImageIndex != -1 && base.ImageList != null) ? base.ImageList.Images[base.TabPages[i].ImageIndex] : null);
  159. Image baseTabHeaderImage = Properties.Resources.tab02;//;
  160. Image btnArrowImage = null;
  161. if (base.SelectedIndex == i)
  162. {
  163. baseTabHeaderImage =Properties.Resources.tab01;// Resources.tab_dots_down;
  164. Point contextMenuLocation = base.PointToScreen(new Point(this._btnArrowRect.Left, this._btnArrowRect.Top + this._btnArrowRect.Height + 5));
  165. ContextMenuStrip contextMenuStrip = base.TabPages[i].ContextMenuStrip;
  166. if (contextMenuStrip != null)
  167. {
  168. contextMenuStrip.Closed -= new ToolStripDropDownClosedEventHandler(this.contextMenuStrip_Closed);
  169. contextMenuStrip.Closed += new ToolStripDropDownClosedEventHandler(this.contextMenuStrip_Closed);
  170. if (contextMenuLocation.X + contextMenuStrip.Width > Screen.PrimaryScreen.WorkingArea.Width - 20)
  171. {
  172. contextMenuLocation.X = Screen.PrimaryScreen.WorkingArea.Width - contextMenuStrip.Width - 50;
  173. }
  174. if (tabRect.Contains(cursorPoint))
  175. {
  176. if (this._isFocus)
  177. {
  178. btnArrowImage = Properties.Resources.tab01; ;
  179. contextMenuStrip.Show(contextMenuLocation);
  180. }
  181. else
  182. {
  183. btnArrowImage = Properties.Resources.tab01; ;
  184. }
  185. this._btnArrowRect = new Rectangle(tabRect.X + tabRect.Width - btnArrowImage.Width, tabRect.Y, btnArrowImage.Width, btnArrowImage.Height);
  186. }
  187. else
  188. {
  189. if (this._isFocus)
  190. {
  191. btnArrowImage = Properties.Resources.tab01; ;
  192. contextMenuStrip.Show(contextMenuLocation);
  193. }
  194. }
  195. }
  196. }
  197. else
  198. {
  199. if (tabRect.Contains(cursorPoint))
  200. {
  201. baseTabHeaderImage = Properties.Resources.tab02; ;//Resources.tab_dots_mouseover;
  202. }
  203. }
  204. if (baseTabHeaderImage != null)
  205. {
  206. if (base.SelectedIndex == i)
  207. {
  208. if (base.SelectedIndex == base.TabCount - 1)
  209. {
  210. tabRect.Inflate(2, 0);
  211. }
  212. else
  213. {
  214. tabRect.Inflate(1, 0);
  215. }
  216. }
  217. this.DrawImage(g, baseTabHeaderImage, tabRect);
  218. if (btnArrowImage != null)
  219. {
  220. g.DrawImage(btnArrowImage, this._btnArrowRect);
  221. }
  222. }
  223. if (this.Icon != null)
  224. {
  225. g.DrawImage(this.Icon, tabRect.X + (tabRect.Width - this.Icon.Width) / 2, tabRect.Y + (tabRect.Height - this.Icon.Height) / 2);
  226. }
  227. TextRenderer.DrawText(g, page.Text, page.Font, tabRect, page.ForeColor);
  228. }
  229. }
  230. private void contextMenuStrip_Closed(object sender, ToolStripDropDownClosedEventArgs e)
  231. {
  232. this._isFocus = false;
  233. base.Invalidate(this._btnArrowRect);
  234. }
  235. }
  236. }