TabControlEx.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314
  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.Gray;
  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), "209, 205, 209")]
  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. Image selectImage = null;
  143. Image notSelected = null;
  144. this._pageColor = LYFZ.ComponentLibrary.UIBlueThemeResources.TabControlBgColor;//关闭选项卡页面颜色属性
  145. this._baseColor = LYFZ.ComponentLibrary.UIBlueThemeResources.TabControlBgColor;
  146. this._borderColor = LYFZ.ComponentLibrary.UIBlueThemeResources.TabControlBorderColor;//关闭边框颜色属性
  147. using (SolidBrush brush = new SolidBrush(this._pageColor))
  148. {
  149. int x = 0;
  150. int y = 0;
  151. int width = 0;
  152. int height = 0;
  153. switch (this.Alignment) {
  154. case TabAlignment.Top:
  155. x = 0;
  156. y = base.ItemSize.Height+2;
  157. width = base.Width;
  158. height = base.Height - base.ItemSize.Height-2;
  159. selectImage = LYFZ.ComponentLibrary.UIBlueThemeResources.tab_top_select;
  160. notSelected = LYFZ.ComponentLibrary.UIBlueThemeResources.tab_top;
  161. break;
  162. case TabAlignment.Bottom:
  163. x = 0;
  164. y = 0;
  165. width = base.Width;
  166. height = base.Height - base.ItemSize.Height-2;
  167. selectImage = LYFZ.ComponentLibrary.UIBlueThemeResources.tab_bottom_select;
  168. notSelected = LYFZ.ComponentLibrary.UIBlueThemeResources.tab_bottom;
  169. break;
  170. case TabAlignment.Left:
  171. x = base.ItemSize.Height+2;
  172. y = 0;
  173. width = base.Width - base.ItemSize.Height-2;
  174. height = base.Height;
  175. selectImage = LYFZ.ComponentLibrary.UIBlueThemeResources.tab_left_select;
  176. notSelected = LYFZ.ComponentLibrary.UIBlueThemeResources.tab_left;
  177. break;
  178. case TabAlignment.Right:
  179. x = 0;
  180. y = 0;
  181. width = base.Width - base.ItemSize.Height-2;
  182. height = base.Height;
  183. selectImage = LYFZ.ComponentLibrary.UIBlueThemeResources.tab_right_select;
  184. notSelected = LYFZ.ComponentLibrary.UIBlueThemeResources.tab_right;
  185. break;
  186. }
  187. g.FillRectangle(brush, x, y, width, height);
  188. g.DrawRectangle(new Pen(this._borderColor), x, y, width - 1, height - 1);
  189. }
  190. Rectangle tabRect = Rectangle.Empty;
  191. Point cursorPoint = base.PointToClient(Control.MousePosition);
  192. /* if (base.TabPages.Count > 0)
  193. {
  194. for (int i = 0; i < base.TabPages.Count; i++)
  195. {
  196. base.TabPages[i].BackColor = this._pageColor;
  197. }
  198. }*/
  199. for (int i = 0; i < base.TabCount; i++)
  200. {
  201. TabPage page = base.TabPages[i];
  202. tabRect = base.GetTabRect(i);
  203. Color arg_AB_0 = Color.Yellow;
  204. this.Icon = ((base.TabPages[i].ImageIndex != -1 && base.ImageList != null) ? base.ImageList.Images[base.TabPages[i].ImageIndex] : null);
  205. Image baseTabHeaderImage =notSelected ; //未选时状态
  206. Image btnArrowImage = null;
  207. if (base.SelectedIndex == i)
  208. {
  209. baseTabHeaderImage = selectImage;// Resources.tab_dots_down;当前选项卡
  210. Point contextMenuLocation = base.PointToScreen(new Point(this._btnArrowRect.Left, this._btnArrowRect.Top + this._btnArrowRect.Height + 5));
  211. ContextMenuStrip contextMenuStrip = base.TabPages[i].ContextMenuStrip;
  212. if (contextMenuStrip != null)
  213. {
  214. contextMenuStrip.Closed -= new ToolStripDropDownClosedEventHandler(this.contextMenuStrip_Closed);
  215. contextMenuStrip.Closed += new ToolStripDropDownClosedEventHandler(this.contextMenuStrip_Closed);
  216. if (contextMenuLocation.X + contextMenuStrip.Width > Screen.PrimaryScreen.WorkingArea.Width - 20)
  217. {
  218. contextMenuLocation.X = Screen.PrimaryScreen.WorkingArea.Width - contextMenuStrip.Width - 50;
  219. }
  220. /* if (tabRect.Contains(cursorPoint))
  221. {
  222. if (this._isFocus)
  223. {
  224. // btnArrowImage = Properties.Resources.tab01;
  225. contextMenuStrip.Show(contextMenuLocation);
  226. }
  227. else
  228. {
  229. // btnArrowImage = Properties.Resources.tab01;
  230. }
  231. this._btnArrowRect = new Rectangle(tabRect.X + tabRect.Width - btnArrowImage.Width, tabRect.Y, btnArrowImage.Width, btnArrowImage.Height);
  232. }
  233. else
  234. {
  235. if (this._isFocus)
  236. {
  237. // btnArrowImage = Properties.Resources.tab01;
  238. contextMenuStrip.Show(contextMenuLocation);
  239. }
  240. }*/
  241. }
  242. }
  243. /* else
  244. {
  245. if (tabRect.Contains(cursorPoint))
  246. {
  247. baseTabHeaderImage = Properties.Resources.tab02; //Resources.tab_dots_mouseover;鼠标移上时状态
  248. }
  249. }*/
  250. if (baseTabHeaderImage != null)
  251. {
  252. if (base.SelectedIndex == i)
  253. {
  254. switch (this.Alignment)
  255. {
  256. case TabAlignment.Top:
  257. tabRect.Inflate(0, 1);
  258. tabRect.Location = new Point(tabRect.Location.X, 2);
  259. break;
  260. case TabAlignment.Bottom:
  261. tabRect.Inflate(0, 1);
  262. tabRect.Location = new Point(tabRect.Location.X, base.Height - base.ItemSize.Height - 4);
  263. break;
  264. case TabAlignment.Left:
  265. tabRect.Inflate(1, 0);
  266. tabRect.Location = new Point(2, tabRect.Location.Y);
  267. break;
  268. case TabAlignment.Right:
  269. tabRect.Inflate(1, 0);
  270. tabRect.Location = new Point(base.Width - base.ItemSize.Height - 4, tabRect.Location.Y);
  271. break;
  272. }
  273. //关闭选项卡页面颜色属性
  274. if (page.BackColor != this._pageColor)
  275. {
  276. page.BackColor = this._pageColor;
  277. }
  278. }
  279. else
  280. {
  281. if (this.Alignment == TabAlignment.Top)
  282. {
  283. if (this.Appearance != TabAppearance.Normal)
  284. {
  285. tabRect.Location = new Point(tabRect.Location.X, 2);
  286. }
  287. }
  288. }
  289. this.DrawImage(g, baseTabHeaderImage, tabRect);
  290. if (btnArrowImage != null)
  291. {
  292. g.DrawImage(btnArrowImage, this._btnArrowRect);
  293. }
  294. }
  295. if (this.Icon != null)
  296. {
  297. g.DrawImage(this.Icon, tabRect.X + (tabRect.Width - this.Icon.Width) / 2, tabRect.Y + (tabRect.Height - this.Icon.Height) / 2);
  298. }
  299. TextRenderer.DrawText(g, page.Text, page.Font, tabRect, page.ForeColor);
  300. }
  301. }
  302. private void contextMenuStrip_Closed(object sender, ToolStripDropDownClosedEventArgs e)
  303. {
  304. //this._isFocus = false;
  305. base.Invalidate(this._btnArrowRect);
  306. }
  307. }
  308. }