CNAS取数仪器端升级
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

391 line
17KB

  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Drawing;
  5. using System.Linq;
  6. using System.Text;
  7. using System.Windows.Forms;
  8. namespace CNAS_RunSync
  9. {
  10. public partial class KMenuTabControl : System.Windows.Forms.TabControl
  11. {
  12. #region 属性、构造
  13. Color SelectedColor = Color.FromArgb(235,235,235);
  14. Color MoveColor = Color.FromArgb(225, 225, 225);
  15. Color FontColor = Color.Black;
  16. int TextLeft = 10;
  17. [Browsable(true)]
  18. [Description("选项卡标题左边距"), Category("TextLeft"), DefaultValue(typeof(Int32), "10")]
  19. public int TitleTextLeft
  20. {
  21. get { return TextLeft; }
  22. set { this.TextLeft = value; }
  23. }
  24. [Browsable(true)]
  25. [Description("选项卡标题字体颜色"), Category("TitleColor"), DefaultValue(typeof(Color), "Black")]
  26. public Color TitleFontColor
  27. {
  28. get { return FontColor; }
  29. set { this.FontColor = value; }
  30. }
  31. [Browsable(true)]
  32. [Description("选项卡标题字体选中颜色"), Category("TitleColor"), DefaultValue(typeof(Color), "LightSkyBlue")]
  33. public Color TitleSelectedColor
  34. {
  35. get { return SelectedColor; }
  36. set { this.SelectedColor = value; }
  37. }
  38. [Browsable(true)]
  39. [Description("选项卡标题字体悬浮颜色"), Category("TitleColor"), DefaultValue(typeof(Color), "White")]
  40. public Color TitleMoveColor
  41. {
  42. get { return MoveColor; }
  43. set { this.MoveColor = value; }
  44. }
  45. [Browsable(true), Description("整个控件的背景色"), Category("外观")]
  46. public Color TabControlBackColor { get; set; }
  47. [Browsable(true), Description("TabControl ItemSize"), Category("外观")]
  48. public Size TabControlItemSize { get; set; }
  49. public KMenuTabControl()
  50. {
  51. this.SuspendLayout();
  52. this.DrawMode = TabDrawMode.OwnerDrawFixed;
  53. this.ResumeLayout(false);
  54. this.SizeMode = TabSizeMode.Fixed;
  55. this.Multiline = false;
  56. this.TabControlBackColor = Color.WhiteSmoke;
  57. this.TabControlItemSize = new Size(100, 28);
  58. this.DrawItem += new System.Windows.Forms.DrawItemEventHandler(this.tabMenu_DrawItem);
  59. this.MouseDown += new System.Windows.Forms.MouseEventHandler(this.MainTabControl_MouseDown);
  60. }
  61. #endregion
  62. protected override void OnPaint(PaintEventArgs e)
  63. {
  64. Rectangle tcRec = this.ClientRectangle;//整个tabControl的边框
  65. e.Graphics.FillRectangle(new SolidBrush(this.TabControlBackColor), tcRec);
  66. int MaxWidth = 100;
  67. for (int i = 0; i < this.TabCount; i++)
  68. {
  69. Graphics gx = Graphics.FromImage(new Bitmap(1, 1));
  70. SizeF size = gx.MeasureString(this.Controls[i].Text, this.Font);
  71. if (MaxWidth < (int)size.Width + 30)
  72. MaxWidth = (int)size.Width + 30;
  73. }
  74. this.TabControlItemSize = new Size(MaxWidth, this.TabControlItemSize.Height);
  75. if (this.ItemSize != this.TabControlItemSize)
  76. {
  77. this.ItemSize = TabControlItemSize;
  78. }
  79. StringFormat sf = new StringFormat();//封装文本布局信息
  80. sf.LineAlignment = StringAlignment.Center;
  81. sf.Alignment = StringAlignment.Near;
  82. for (int i = 0; i < this.TabCount; i++)
  83. {
  84. Graphics g = e.Graphics;
  85. // int width = (int)g.MeasureString(this.Controls[i].Text, this.Font).Width + 40;
  86. Rectangle rect = this.GetTabRect(i);
  87. // rect.Width = width;
  88. if (this.SelectedIndex == i)
  89. g.FillRectangle(new SolidBrush(MoveColor), rect);
  90. else
  91. g.FillRectangle(new SolidBrush(SelectedColor), rect);
  92. SolidBrush brush = new SolidBrush(FontColor);
  93. // rect.Width = width;
  94. rect.X += TextLeft;
  95. g.DrawString(this.Controls[i].Text, this.Font, brush, rect, sf);
  96. using (Pen objpen = new Pen(Color.Black))
  97. {
  98. int tx = (int)(rect.X + (rect.Width - 30));
  99. rect.X = tx - 2;
  100. Point p5 = new Point(tx, 8);
  101. Font font = new System.Drawing.Font("微软雅黑", 12);
  102. //g.DrawString("〇", font, brush, rect, sf);
  103. font = new System.Drawing.Font("微软雅黑", 11);
  104. rect.X = tx + 2;
  105. rect.Y = rect.Y - 1;
  106. g.DrawString("×", font, brush, rect, sf);
  107. }
  108. }
  109. }
  110. public override Rectangle DisplayRectangle
  111. {
  112. get
  113. {
  114. Rectangle rect = base.DisplayRectangle;
  115. return new Rectangle(rect.Left - 2, rect.Top - 2, rect.Width + 4, rect.Height + 5);
  116. }
  117. }
  118. int index = -1;
  119. protected override void OnMouseMove(MouseEventArgs e)
  120. {
  121. int Count = 0;
  122. try
  123. {
  124. Graphics g = this.CreateGraphics();
  125. SolidBrush brush = new SolidBrush(FontColor);
  126. StringFormat sf = new StringFormat();//封装文本布局信息
  127. sf.LineAlignment = StringAlignment.Center;
  128. sf.Alignment = StringAlignment.Near;
  129. for (int i = 0; i < this.TabPages.Count; i++)
  130. {
  131. TabPage tp = this.TabPages[i];
  132. if (this.GetTabRect(i).Contains(e.Location) && tp != this.SelectedTab)
  133. {
  134. if (index != i)
  135. {
  136. if (Count == 0)
  137. {
  138. if (index != -1 && this.TabPages[index] != this.SelectedTab)
  139. {
  140. g.FillRectangle(new SolidBrush(SelectedColor), this.GetTabRect(index));
  141. RectangleF tRectangle = this.GetTabRect(index);
  142. tRectangle.X += TextLeft;
  143. g.DrawString(this.Controls[index].Text, this.Font, brush, tRectangle, sf);
  144. }
  145. Count = 1;
  146. }
  147. index = i;
  148. g.FillRectangle(new SolidBrush(SelectedColor), this.GetTabRect(i));
  149. RectangleF tRectangleF = this.GetTabRect(i);
  150. tRectangleF.X += TextLeft;
  151. g.DrawString(this.Controls[i].Text, this.Font, brush, tRectangleF, sf);
  152. using (Pen objpen = new Pen(Color.Black))
  153. {
  154. int tx = (int)(tRectangleF.X + (tRectangleF.Width - 30));
  155. tRectangleF.X = tx - 2;
  156. brush.Color = Color.White;
  157. Font font = new System.Drawing.Font("微软雅黑", 12);
  158. //g.DrawString("〇", font, brush, tRectangleF, sf);
  159. font = new System.Drawing.Font("微软雅黑", 11);
  160. tRectangleF.X = tx + 2;
  161. tRectangleF.Y = tRectangleF.Y - 1;
  162. g.DrawString("×", font, brush, tRectangleF, sf);
  163. }
  164. }
  165. }
  166. if (this.GetTabRect(i).Contains(e.Location) && tp == this.SelectedTab)
  167. {
  168. if (index != -1 && index != this.SelectedIndex)
  169. {
  170. g.FillRectangle(new SolidBrush(SelectedColor), this.GetTabRect(index));
  171. RectangleF tRectangleF = this.GetTabRect(index);
  172. tRectangleF.X += TextLeft;
  173. g.DrawString(this.Controls[index].Text, this.Font, brush, tRectangleF, sf);
  174. using (Pen objpen = new Pen(Color.Black))
  175. {
  176. int tx = (int)(tRectangleF.X + (tRectangleF.Width - 30));
  177. tRectangleF.X = tx - 2;
  178. Font font = new System.Drawing.Font("微软雅黑", 12);
  179. //g.DrawString("〇", font, brush, tRectangleF, sf);
  180. font = new System.Drawing.Font("微软雅黑", 11);
  181. tRectangleF.X = tx + 2;
  182. tRectangleF.Y = tRectangleF.Y - 1;
  183. g.DrawString("×", font, brush, tRectangleF, sf);
  184. }
  185. }
  186. index = -1;
  187. }
  188. }
  189. }
  190. catch (Exception)
  191. {
  192. }
  193. Count = 0;
  194. base.OnMouseMove(e);
  195. }
  196. protected override void OnMouseLeave(EventArgs e)
  197. {
  198. try
  199. {
  200. Graphics g = this.CreateGraphics();
  201. if (index != -1 && this.TabPages[index] != this.SelectedTab)
  202. {
  203. g.FillRectangle(new SolidBrush(SelectedColor), this.GetTabRect(index));
  204. SolidBrush brush = new SolidBrush(FontColor);
  205. RectangleF tRectangleF = this.GetTabRect(index);
  206. StringFormat sf = new StringFormat();//封装文本布局信息
  207. sf.LineAlignment = StringAlignment.Center;
  208. sf.Alignment = StringAlignment.Near;
  209. tRectangleF.X += TextLeft;
  210. g.DrawString(this.Controls[index].Text, this.Font, brush, tRectangleF, sf);
  211. using (Pen objpen = new Pen(Color.Black))
  212. {
  213. int tx = (int)(tRectangleF.X + (tRectangleF.Width - 30));
  214. tRectangleF.X = tx - 2;
  215. Point p5 = new Point(tx, 8);
  216. Font font = new System.Drawing.Font("微软雅黑", 12);
  217. //g.DrawString("〇", font, brush, tRectangleF, sf);
  218. font = new System.Drawing.Font("微软雅黑", 11);
  219. tRectangleF.X = tx + 2;
  220. tRectangleF.Y = tRectangleF.Y - 1;
  221. g.DrawString("×", font, brush, tRectangleF, sf);
  222. }
  223. }
  224. }
  225. catch (Exception)
  226. {
  227. }
  228. index = -1;
  229. base.OnMouseLeave(e);
  230. }
  231. /// <summary>
  232. /// 重绘控件
  233. /// </summary>
  234. /// <param name="sender"></param>
  235. /// <param name="e"></param>
  236. private void tabMenu_DrawItem(object sender, DrawItemEventArgs e)
  237. {
  238. this.SetStyle(
  239. ControlStyles.UserPaint | // 控件将自行绘制,而不是通过操作系统来绘制
  240. ControlStyles.OptimizedDoubleBuffer | // 该控件首先在缓冲区中绘制,而不是直接绘制到屏幕上,这样可以减少闪烁
  241. ControlStyles.AllPaintingInWmPaint | // 控件将忽略 WM_ERASEBKGND 窗口消息以减少闪烁
  242. ControlStyles.ResizeRedraw | // 在调整控件大小时重绘控件
  243. ControlStyles.SupportsTransparentBackColor, // 控件接受 alpha 组件小于 255 的 BackColor 以模拟透明
  244. true); // 设置以上值为 true
  245. this.UpdateStyles();
  246. }
  247. //关闭按钮功能
  248. private void MainTabControl_MouseDown(object sender, MouseEventArgs e)
  249. {
  250. int closeSize = 20;
  251. if (e.Button == MouseButtons.Left)
  252. {
  253. int x = e.X, y = e.Y;
  254. //计算关闭区域
  255. Rectangle tab = this.GetTabRect(this.SelectedIndex);
  256. tab.Offset(tab.Width - (closeSize + 3), 4);
  257. tab.Width = closeSize;
  258. tab.Height = closeSize;
  259. if (this.TabCount == 1) return;
  260. //如果鼠标在区域内就关闭选项卡
  261. bool isClose = x > tab.X && x < tab.Right && y > tab.Y && y < tab.Bottom;
  262. if (isClose == true)
  263. {
  264. this.TabPages.Remove(this.SelectedTab);
  265. }
  266. }
  267. }
  268. }
  269. public partial class KTabControl : System.Windows.Forms.TabControl
  270. {
  271. [Browsable(true), Description("整个控件的背景色"), Category("外观")]
  272. public Color TabControlBackColor { get; set; }
  273. [Browsable(true), Description("Tab的标题栏边框颜色"), Category("外观")]
  274. public Color TabBorderColor { get; set; }
  275. [Browsable(true), Description("当前激活Tab的标题栏背景色"), Category("外观")]
  276. public Color ActivedTabBackColor { get; set; }
  277. [Browsable(true), Description("当前激活Tab的标题文字颜色"), Category("外观")]
  278. public Color ActivedTabLabelColor { get; set; }
  279. [Browsable(true), Description("未激活Tab的标题栏背景色"), Category("外观")]
  280. public Color InActivedTabBackColor { get; set; }
  281. [Browsable(true), Description("未激活Tab的标题文字颜色"), Category("外观")]
  282. public Color InActivedTabLabelColor { get; set; }
  283. [Browsable(true), Description("Tab标题栏的大小"), Category("外观")]
  284. public Size TabSize { get; set; }
  285. public KTabControl()
  286. {
  287. //this.InitializeComponent();
  288. TabSet();
  289. this.TabBorderColor = Color.Black;
  290. this.ActivedTabLabelColor = Color.Black;
  291. this.InActivedTabLabelColor = Color.Black;
  292. this.ActivedTabBackColor = Color.White;
  293. this.InActivedTabBackColor = Color.FromArgb(0, 192, 192);
  294. this.TabControlBackColor = Color.Transparent;
  295. this.TabSize = new Size(100, 35);
  296. }
  297. protected override void OnMouseDoubleClick(MouseEventArgs e)
  298. {
  299. if (this.TabPages.Count == 1) return;
  300. this.TabPages.RemoveAt(this.SelectedIndex);
  301. }
  302. protected override void OnPaint(PaintEventArgs e)
  303. {
  304. Rectangle tcRec = this.ClientRectangle;//整个tabControl的边框
  305. e.Graphics.FillRectangle(new SolidBrush(this.TabControlBackColor), tcRec);
  306. for (int i = 0; i < this.TabPages.Count; i++)
  307. {
  308. Rectangle tabRectangle = new Rectangle(1, 1 + i * TabSize.Height, TabSize.Width, TabSize.Height);
  309. SolidBrush brush = new SolidBrush(this.InActivedTabLabelColor);
  310. StringFormat sf = new StringFormat();//封装文本布局信息
  311. sf.LineAlignment = StringAlignment.Center;
  312. sf.Alignment = StringAlignment.Center;
  313. if (i == this.SelectedIndex)
  314. {
  315. brush = new SolidBrush(this.ActivedTabLabelColor);
  316. e.Graphics.FillRectangle(new SolidBrush(ActivedTabBackColor), tabRectangle);
  317. e.Graphics.DrawRectangle(new Pen(this.TabBorderColor), tabRectangle);
  318. }
  319. else
  320. {
  321. e.Graphics.FillRectangle(new SolidBrush(InActivedTabBackColor), tabRectangle);
  322. e.Graphics.DrawRectangle(new Pen(this.TabBorderColor), tabRectangle);
  323. }
  324. e.Graphics.DrawString(this.Controls[i].Text, this.Font, brush, tabRectangle, sf);
  325. }
  326. }
  327. /// <summary>
  328. /// 设定控件绘制模式
  329. /// </summary>
  330. private void TabSet()
  331. {
  332. this.DrawMode = TabDrawMode.OwnerDrawFixed;
  333. this.Alignment = TabAlignment.Left;
  334. this.SizeMode = TabSizeMode.Fixed;
  335. this.Multiline = true;
  336. base.SetStyle(
  337. ControlStyles.UserPaint |
  338. ControlStyles.OptimizedDoubleBuffer |
  339. ControlStyles.AllPaintingInWmPaint |
  340. ControlStyles.ResizeRedraw |
  341. ControlStyles.SupportsTransparentBackColor,
  342. true);
  343. base.UpdateStyles();
  344. }
  345. public override Rectangle DisplayRectangle
  346. {
  347. get
  348. {
  349. Rectangle rect = base.DisplayRectangle;
  350. return new Rectangle(rect.Left - 3, rect.Top - 3, rect.Width + 6, rect.Height + 5);
  351. }
  352. }
  353. }
  354. }