TabControl控件的美化
最近因项目需要 所以就到网上找了一个美化过的TabControl控件 只不过这个控件没有实现TabPage的关闭功能 所以就自己添加了一个关闭功能
好了废话不多说
直接贴代码
UpDownButtonPaintEventArgs 类的代码

1 using System;
2 using System.Collections.Generic;
3 using System.Text;
4 using System.Windows.Forms;
5 using System.Drawing;
6
7 namespace StyleWinForm.TabControls
8 {
9 public delegate void UpDownButtonPaintEventHandler(
10 object sender,
11 UpDownButtonPaintEventArgs e);
12
13 public class UpDownButtonPaintEventArgs : PaintEventArgs
14 {
15 private bool _mouseOver;
16 private bool _mousePress;
17 private bool _mouseInUpButton;
18
19 public UpDownButtonPaintEventArgs(
20 Graphics graphics,
21 Rectangle clipRect,
22 bool mouseOver,
23 bool mousePress,
24 bool mouseInUpButton)
25 : base(graphics, clipRect)
26 {
27 _mouseOver = mouseOver;
28 _mousePress = mousePress;
29 _mouseInUpButton = mouseInUpButton;
30 }
31
32 public bool MouseOver
33 {
34 get { return _mouseOver; }
35 }
36
37 public bool MousePress
38 {
39 get { return _mousePress; }
40 }
41
42 public bool MouseInUpButton
43 {
44 get { return _mouseInUpButton; }
45 }
46 }
47 }
NativeMethods 类的代码

1 using System;
2 using System.Collections.Generic;
3 using System.Text;
4 using System.Runtime.InteropServices;
5 using System.Drawing;
6
7 namespace StyleWinForm.TabControls
8 {
9 internal class NativeMethods
10 {
11 public const int WM_PAINT = 0xF;
12
13 public const int VK_LBUTTON = 0x1;
14 public const int VK_RBUTTON = 0x2;
15
16 private const int TCM_FIRST = 0x1300;
17 public const int TCM_GETITEMRECT = (TCM_FIRST + 10);
18
19 public static readonly IntPtr TRUE = new IntPtr(1);
20
21 [StructLayout(LayoutKind.Sequential)]
22 public struct PAINTSTRUCT
23 {
24 internal IntPtr hdc;
25 internal int fErase;
26 internal RECT rcPaint;
27 internal int fRestore;
28 internal int fIncUpdate;
29 internal int Reserved1;
30 internal int Reserved2;
31 internal int Reserved3;
32 internal int Reserved4;
33 internal int Reserved5;
34 internal int Reserved6;
35 internal int Reserved7;
36 internal int Reserved8;
37 }
38
39 [StructLayout(LayoutKind.Sequential)]
40 public struct RECT
41 {
42 internal RECT(int X, int Y, int Width, int Height)
43 {
44 this.Left = X;
45 this.Top = Y;
46 this.Right = Width;
47 this.Bottom = Height;
48 }
49 internal int Left;
50 internal int Top;
51 internal int Right;
52 internal int Bottom;
53 }
54
55 [DllImport("user32.dll")]
56 public static extern IntPtr FindWindowEx(
57 IntPtr hwndParent,
58 IntPtr hwndChildAfter,
59 string lpszClass,
60 string lpszWindow);
61
62 [DllImport("user32.dll")]
63 public static extern IntPtr BeginPaint(IntPtr hWnd, ref PAINTSTRUCT ps);
64
65 [DllImport("user32.dll")]
66 [return: MarshalAs(UnmanagedType.Bool)]
67 public static extern bool EndPaint(IntPtr hWnd, ref PAINTSTRUCT ps);
68
69 [DllImport("user32.dll")]
70 public static extern short GetKeyState(int nVirtKey);
71
72 [DllImport("user32.dll")]
73 public static extern IntPtr SendMessage(
74 IntPtr hWnd, int Msg, int wParam, ref RECT lParam);
75
76 [DllImport("user32.dll")]
77 [return: MarshalAs(UnmanagedType.Bool)]
78 public static extern bool GetCursorPos(ref Point lpPoint);
79
80 [DllImport("user32.dll")]
81 public extern static int OffsetRect(ref RECT lpRect, int x, int y);
82
83 [DllImport("user32.dll")]
84 [return: MarshalAs(UnmanagedType.Bool)]
85 public static extern bool PtInRect([In] ref RECT lprc, Point pt);
86
87 [DllImport("user32.dll")]
88 [return: MarshalAs(UnmanagedType.Bool)]
89 public static extern bool GetWindowRect(IntPtr hWnd, ref RECT lpRect);
90
91 [DllImport("user32.dll")]
92 [return: MarshalAs(UnmanagedType.Bool)]
93 public static extern bool GetClientRect(IntPtr hWnd, ref RECT r);
94
95 [DllImport("User32.dll", CharSet = CharSet.Auto)]
96 public static extern bool IsWindowVisible(IntPtr hwnd);
97 }
98 }
TabControl组件的代码
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 | using System; using System.Collections.Generic; using System.Text; using System.Windows.Forms; using System.Drawing; using System.Drawing.Drawing2D; using System.ComponentModel; using System.Drawing.Text; namespace StyleWinForm.TabControls { public class TabControlEx : TabControl { #region Fields private UpDownButtonNativeWindow _upDownButtonNativeWindow; private Color _baseColor = Color.FromArgb(166, 222, 255); private Color _backColor = Color.FromArgb(234, 247, 254); private Color _borderColor = Color.FromArgb(23, 169, 254); private Color _arrowColor = Color.FromArgb(0, 79, 125); private bool showClose = false ; private const string UpDownButtonClassName = "msctls_updown32" ; private static readonly int Radius = 8; private static readonly object EventPaintUpDownButton = new object (); #endregion #region Constructors public TabControlEx() : base () { SetStyles(); } #endregion #region Events public event UpDownButtonPaintEventHandler PaintUpDownButton { add { base .Events.AddHandler(EventPaintUpDownButton, value); } remove { base .Events.RemoveHandler(EventPaintUpDownButton, value); } } #endregion #region Properties [DefaultValue( typeof (Color), "166, 222, 255" )] public Color BaseColor { get { return _baseColor; } set { _baseColor = value; base .Invalidate( true ); } } public bool ShowClose { get { return showClose; } set { showClose = value; } } [Browsable( true )] [EditorBrowsable(EditorBrowsableState.Always)] [DefaultValue( typeof (Color), "234, 247, 254" )] public override Color BackColor { get { return _backColor; } set { _backColor = value; base .Invalidate( true ); } } [DefaultValue( typeof (Color), "23, 169, 254" )] public Color BorderColor { get { return _borderColor; } set { _borderColor = value; base .Invalidate( true ); } } [DefaultValue( typeof (Color), "0, 95, 152" )] public Color ArrowColor { get { return _arrowColor; } set { _arrowColor = value; base .Invalidate( true ); } } internal IntPtr UpDownButtonHandle { get { return FindUpDownButton(); } } #endregion #region Protected Methods protected virtual void OnPaintUpDownButton( UpDownButtonPaintEventArgs e) { Graphics g = e.Graphics; Rectangle rect = e.ClipRectangle; Color upButtonBaseColor = _baseColor; Color upButtonBorderColor = _borderColor; Color upButtonArrowColor = _arrowColor; Color downButtonBaseColor = _baseColor; Color downButtonBorderColor = _borderColor; Color downButtonArrowColor = _arrowColor; Rectangle upButtonRect = rect; upButtonRect.X += 4; upButtonRect.Y += 4; upButtonRect.Width = rect.Width / 2 - 8; upButtonRect.Height -= 8; Rectangle downButtonRect = rect; downButtonRect.X = upButtonRect.Right + 2; downButtonRect.Y += 4; downButtonRect.Width = rect.Width / 2 - 8; downButtonRect.Height -= 8; if (Enabled) { if (e.MouseOver) { if (e.MousePress) { if (e.MouseInUpButton) { upButtonBaseColor = GetColor(_baseColor, 0, -35, -24, -9); } else { downButtonBaseColor = GetColor(_baseColor, 0, -35, -24, -9); } } else { if (e.MouseInUpButton) { upButtonBaseColor = GetColor(_baseColor, 0, 35, 24, 9); } else { downButtonBaseColor = GetColor(_baseColor, 0, 35, 24, 9); } } } } else { upButtonBaseColor = SystemColors.Control; upButtonBorderColor = SystemColors.ControlDark; upButtonArrowColor = SystemColors.ControlDark; downButtonBaseColor = SystemColors.Control; downButtonBorderColor = SystemColors.ControlDark; downButtonArrowColor = SystemColors.ControlDark; } g.SmoothingMode = SmoothingMode.AntiAlias; Color backColor = Enabled ? _backColor : SystemColors.Control; using (SolidBrush brush = new SolidBrush(_backColor)) { rect.Inflate(1, 1); g.FillRectangle(brush, rect); } RenderButton( g, upButtonRect, upButtonBaseColor, upButtonBorderColor, upButtonArrowColor, ArrowDirection.Left); RenderButton( g, downButtonRect, downButtonBaseColor, downButtonBorderColor, downButtonArrowColor, ArrowDirection.Right); UpDownButtonPaintEventHandler handler = base .Events[EventPaintUpDownButton] as UpDownButtonPaintEventHandler; if (handler != null ) { handler( this , e); } } protected override void OnMouseMove(MouseEventArgs e) { base .OnMouseMove(e); base .Invalidate(); } protected override void OnMouseLeave(EventArgs e) { base .OnMouseLeave(e); base .Invalidate(); } protected override void OnPaint(PaintEventArgs e) { base .OnPaint(e); DrawTabContrl(e.Graphics); } protected override void OnDrawItem(DrawItemEventArgs e) { } protected override void OnHandleCreated(EventArgs e) { base .OnHandleCreated(e); if (UpDownButtonHandle != IntPtr.Zero) { if (_upDownButtonNativeWindow == null ) { _upDownButtonNativeWindow = new UpDownButtonNativeWindow( this ); } } } protected override void OnCreateControl() { base .OnCreateControl(); if (UpDownButtonHandle != IntPtr.Zero) { if (_upDownButtonNativeWindow == null ) { _upDownButtonNativeWindow = new UpDownButtonNativeWindow( this ); } } } protected override void OnHandleDestroyed(EventArgs e) { base .OnHandleDestroyed(e); if (_upDownButtonNativeWindow != null ) { _upDownButtonNativeWindow.Dispose(); _upDownButtonNativeWindow = null ; } } protected override void OnControlAdded(ControlEventArgs e) { base .OnControlAdded(e); if (UpDownButtonHandle != IntPtr.Zero) { if (_upDownButtonNativeWindow == null ) { _upDownButtonNativeWindow = new UpDownButtonNativeWindow( this ); } } } protected override void OnSizeChanged(EventArgs e) { base .OnSizeChanged(e); if (UpDownButtonHandle != IntPtr.Zero) { if (_upDownButtonNativeWindow == null ) { _upDownButtonNativeWindow = new UpDownButtonNativeWindow( this ); } } } #endregion #region Help Methods private IntPtr FindUpDownButton() { return NativeMethods.FindWindowEx( base .Handle, IntPtr.Zero, UpDownButtonClassName, null ); } private void SetStyles() { base .SetStyle( ControlStyles.UserPaint | ControlStyles.OptimizedDoubleBuffer | ControlStyles.AllPaintingInWmPaint | ControlStyles.ResizeRedraw | ControlStyles.SupportsTransparentBackColor, true ); base .UpdateStyles(); } private void DrawTabContrl(Graphics g) { g.SmoothingMode = SmoothingMode.AntiAlias; g.InterpolationMode = InterpolationMode.HighQualityBilinear; g.TextRenderingHint = TextRenderingHint.AntiAlias; DrawDrawBackgroundAndHeader(g); DrawTabPages(g); DrawBorder(g); } private void DrawDrawBackgroundAndHeader(Graphics g) { int x = 0; int y = 0; int width = 0; int height = 0; switch (Alignment) { case TabAlignment.Top: x = 0; y = 0; width = ClientRectangle.Width; height = ClientRectangle.Height - DisplayRectangle.Height; break ; case TabAlignment.Bottom: x = 0; y = DisplayRectangle.Height; width = ClientRectangle.Width; height = ClientRectangle.Height - DisplayRectangle.Height; break ; case TabAlignment.Left: x = 0; y = 0; width = ClientRectangle.Width - DisplayRectangle.Width; height = ClientRectangle.Height; break ; case TabAlignment.Right: x = DisplayRectangle.Width; y = 0; width = ClientRectangle.Width - DisplayRectangle.Width; height = ClientRectangle.Height; break ; } Rectangle headerRect = new Rectangle(x, y, width, height); Color backColor = Enabled ? _backColor : SystemColors.Control; using (SolidBrush brush = new SolidBrush(backColor)) { g.FillRectangle(brush, ClientRectangle); g.FillRectangle(brush, headerRect); } } const int CLOSE_SIZE = 13; private void DrawTabPages(Graphics g) { Rectangle tabRect; Point cusorPoint = PointToClient(MousePosition); bool hover; bool selected; bool hasSetClip = false ; bool alignHorizontal = (Alignment == TabAlignment.Top || Alignment == TabAlignment.Bottom); LinearGradientMode mode = alignHorizontal ? LinearGradientMode.Vertical : LinearGradientMode.Horizontal; if (alignHorizontal) { IntPtr upDownButtonHandle = UpDownButtonHandle; bool hasUpDown = upDownButtonHandle != IntPtr.Zero; if (hasUpDown) { if (NativeMethods.IsWindowVisible(upDownButtonHandle)) { NativeMethods.RECT upDownButtonRect = new NativeMethods.RECT(); NativeMethods.GetWindowRect( upDownButtonHandle, ref upDownButtonRect); Rectangle upDownRect = Rectangle.FromLTRB( upDownButtonRect.Left, upDownButtonRect.Top, upDownButtonRect.Right, upDownButtonRect.Bottom); upDownRect = RectangleToClient(upDownRect); switch (Alignment) { case TabAlignment.Top: upDownRect.Y = 0; break ; case TabAlignment.Bottom: upDownRect.Y = ClientRectangle.Height - DisplayRectangle.Height; break ; } upDownRect.Height = ClientRectangle.Height; g.SetClip(upDownRect, CombineMode.Exclude); hasSetClip = true ; } } } for ( int index = 0; index < base .TabCount; index++) { TabPage page = TabPages[index]; tabRect = GetTabRect(index); hover = tabRect.Contains(cusorPoint); selected = SelectedIndex == index; Color baseColor = _baseColor; Color borderColor = _borderColor; if (selected) { baseColor = GetColor(_baseColor, 0, -45, -30, -14); } else if (hover) { baseColor = GetColor(_baseColor, 0, 35, 24, 9); } RenderTabBackgroundInternal( g, tabRect, baseColor, borderColor, .45F, mode); bool hasImage = DrawTabImage(g, page, tabRect); DrawtabText(g, page, tabRect, hasImage); if (ShowClose) { ////再画一个矩形框 //using (Pen p = new Pen(Color.Black)) //{ tabRect.Offset(tabRect.Width - (CLOSE_SIZE + 3), 5); tabRect.Width = CLOSE_SIZE; tabRect.Height = CLOSE_SIZE; // g.DrawRectangle(p, tabRect); //} //画关闭符号 using (Pen p = new Pen(Color.Black,2)) { //画"\"线 Point p1 = new Point(tabRect.X + 3, tabRect.Y + 3); Point p2 = new Point(tabRect.X + tabRect.Width - 3, tabRect.Y + tabRect.Height - 3); g.DrawLine(p, p1, p2); //画"/"线 Point p3 = new Point(tabRect.X + 3, tabRect.Y + tabRect.Height - 3); Point p4 = new Point(tabRect.X + tabRect.Width - 3, tabRect.Y + 3); g.DrawLine(p, p3, p4); } } } if (hasSetClip) { g.ResetClip(); } } private void DrawtabText( Graphics g, TabPage page, Rectangle tabRect, bool hasImage) { Rectangle textRect = tabRect; RectangleF newTextRect; StringFormat sf; switch (Alignment) { case TabAlignment.Top: case TabAlignment.Bottom: if (hasImage) { textRect.X = tabRect.X + Radius / 2 + tabRect.Height - 2; textRect.Width = tabRect.Width - Radius - tabRect.Height; } TextRenderer.DrawText( g, page.Text, page.Font, textRect, page.ForeColor); break ; case TabAlignment.Left: if (hasImage) { textRect.Height = tabRect.Height - tabRect.Width + 2; } g.TranslateTransform(textRect.X, textRect.Bottom); g.RotateTransform(270F); sf = new StringFormat(StringFormatFlags.NoWrap); sf.Alignment = StringAlignment.Center; sf.LineAlignment = StringAlignment.Center; sf.Trimming = StringTrimming.Character; newTextRect = textRect; newTextRect.X = 0; newTextRect.Y = 0; newTextRect.Width = textRect.Height; newTextRect.Height = textRect.Width; using (Brush brush = new SolidBrush(page.ForeColor)) { g.DrawString( page.Text, page.Font, brush, newTextRect, sf); } g.ResetTransform(); break ; case TabAlignment.Right: if (hasImage) { textRect.Y = tabRect.Y + Radius / 2 + tabRect.Width - 2; textRect.Height = tabRect.Height - Radius - tabRect.Width; } g.TranslateTransform(textRect.Right, textRect.Y); g.RotateTransform(90F); sf = new StringFormat(StringFormatFlags.NoWrap); sf.Alignment = StringAlignment.Center; sf.LineAlignment = StringAlignment.Center; sf.Trimming = StringTrimming.Character; newTextRect = textRect; newTextRect.X = 0; newTextRect.Y = 0; newTextRect.Width = textRect.Height; newTextRect.Height = textRect.Width; using (Brush brush = new SolidBrush(page.ForeColor)) { g.DrawString( page.Text, page.Font, brush, newTextRect, sf); } g.ResetTransform(); break ; } } private void DrawBorder(Graphics g) { if (SelectedIndex != -1) { Rectangle tabRect = GetTabRect(SelectedIndex); Rectangle clipRect = ClientRectangle; Point[] points = new Point[6]; IntPtr upDownButtonHandle = UpDownButtonHandle; bool hasUpDown = upDownButtonHandle != IntPtr.Zero; if (hasUpDown) { if (NativeMethods.IsWindowVisible(upDownButtonHandle)) { NativeMethods.RECT upDownButtonRect = new NativeMethods.RECT(); NativeMethods.GetWindowRect( upDownButtonHandle, ref upDownButtonRect); Rectangle upDownRect = Rectangle.FromLTRB( upDownButtonRect.Left, upDownButtonRect.Top, upDownButtonRect.Right, upDownButtonRect.Bottom); upDownRect = RectangleToClient(upDownRect); tabRect.X = tabRect.X > upDownRect.X ? upDownRect.X : tabRect.X; tabRect.Width = tabRect.Right > upDownRect.X ? upDownRect.X - tabRect.X : tabRect.Width; } } switch (Alignment) { case TabAlignment.Top: points[0] = new Point( tabRect.X, tabRect.Bottom); points[1] = new Point( clipRect.X, tabRect.Bottom); points[2] = new Point( clipRect.X, clipRect.Bottom - 1); points[3] = new Point( clipRect.Right - 1, clipRect.Bottom - 1); points[4] = new Point( clipRect.Right - 1, tabRect.Bottom); points[5] = new Point( tabRect.Right, tabRect.Bottom); break ; case TabAlignment.Bottom: points[0] = new Point( tabRect.X, tabRect.Y); points[1] = new Point( clipRect.X, tabRect.Y); points[2] = new Point( clipRect.X, clipRect.Y); points[3] = new Point( clipRect.Right - 1, clipRect.Y); points[4] = new Point( clipRect.Right - 1, tabRect.Y); points[5] = new Point( tabRect.Right, tabRect.Y); break ; case TabAlignment.Left: points[0] = new Point( tabRect.Right, tabRect.Y); points[1] = new Point( tabRect.Right, clipRect.Y); points[2] = new Point( clipRect.Right - 1, clipRect.Y); points[3] = new Point( clipRect.Right - 1, clipRect.Bottom - 1); points[4] = new Point( tabRect.Right, clipRect.Bottom - 1); points[5] = new Point( tabRect.Right, tabRect.Bottom); break ; case TabAlignment.Right: points[0] = new Point( tabRect.X, tabRect.Y); points[1] = new Point( tabRect.X, clipRect.Y); points[2] = new Point( clipRect.X, clipRect.Y); points[3] = new Point( clipRect.X, clipRect.Bottom - 1); points[4] = new Point( tabRect.X, clipRect.Bottom - 1); points[5] = new Point( tabRect.X, tabRect.Bottom); break ; } using (Pen pen = new Pen(_borderColor)) { g.DrawLines(pen, points); } } } internal void RenderArrowInternal( Graphics g, Rectangle dropDownRect, ArrowDirection direction, Brush brush) { Point point = new Point( dropDownRect.Left + (dropDownRect.Width / 2), dropDownRect.Top + (dropDownRect.Height / 2)); Point[] points = null ; switch (direction) { case ArrowDirection.Left: points = new Point[] { new Point(point.X + 1, point.Y - 4), new Point(point.X + 1, point.Y + 4), new Point(point.X - 2, point.Y) }; break ; case ArrowDirection.Up: points = new Point[] { new Point(point.X - 3, point.Y + 1), new Point(point.X + 3, point.Y + 1), new Point(point.X, point.Y - 1) }; break ; case ArrowDirection.Right: points = new Point[] { new Point(point.X - 1, point.Y - 4), new Point(point.X - 1, point.Y + 4), new Point(point.X + 2, point.Y) }; break ; default : points = new Point[] { new Point(point.X - 3, point.Y - 1), new Point(point.X + 3, point.Y - 1), new Point(point.X, point.Y + 1) }; break ; } g.FillPolygon(brush, points); } internal void RenderButton( Graphics g, Rectangle rect, Color baseColor, Color borderColor, Color arrowColor, ArrowDirection direction) { RenderBackgroundInternal( g, rect, baseColor, borderColor, 0.45f, true , LinearGradientMode.Vertical); using (SolidBrush brush = new SolidBrush(arrowColor)) { RenderArrowInternal( g, rect, direction, brush); } } internal void RenderBackgroundInternal( Graphics g, Rectangle rect, Color baseColor, Color borderColor, float basePosition, bool drawBorder, LinearGradientMode mode) { using (LinearGradientBrush brush = new LinearGradientBrush( rect, Color.Transparent, Color.Transparent, mode)) { Color[] colors = new Color[4]; colors[0] = GetColor(baseColor, 0, 35, 24, 9); colors[1] = GetColor(baseColor, 0, 13, 8, 3); colors[2] = baseColor; colors[3] = GetColor(baseColor, 0, 68, 69, 54); ColorBlend blend = new ColorBlend(); blend.Positions = new float [] { 0.0f, basePosition, basePosition + 0.05f, 1.0f }; blend.Colors = colors; brush.InterpolationColors = blend; g.FillRectangle(brush, rect); } if (baseColor.A > 80) { Rectangle rectTop = rect; if (mode == LinearGradientMode.Vertical) { rectTop.Height = ( int )(rectTop.Height * basePosition); } else { rectTop.Width = ( int )(rect.Width * basePosition); } using (SolidBrush brushAlpha = new SolidBrush(Color.FromArgb(80, 255, 255, 255))) { g.FillRectangle(brushAlpha, rectTop); } } if (drawBorder) { using (Pen pen = new Pen(borderColor)) { g.DrawRectangle(pen, rect); } } } internal void RenderTabBackgroundInternal( Graphics g, Rectangle rect, Color baseColor, Color borderColor, float basePosition, LinearGradientMode mode) { using (GraphicsPath path = CreateTabPath(rect)) { using (LinearGradientBrush brush = new LinearGradientBrush( rect, Color.Transparent, Color.Transparent, mode)) { Color[] colors = new Color[4]; colors[0] = GetColor(baseColor, 0, 35, 24, 9); colors[1] = GetColor(baseColor, 0, 13, 8, 3); colors[2] = baseColor; colors[3] = GetColor(baseColor, 0, 68, 69, 54); ColorBlend blend = new ColorBlend(); blend.Positions = new float [] { 0.0f, basePosition, basePosition + 0.05f, 1.0f }; blend.Colors = colors; brush.InterpolationColors = blend; g.FillPath(brush, path); } if (baseColor.A > 80) { Rectangle rectTop = rect; if (mode == LinearGradientMode.Vertical) { rectTop.Height = ( int )(rectTop.Height * basePosition); } else { rectTop.Width = ( int )(rect.Width * basePosition); } using (SolidBrush brushAlpha = new SolidBrush(Color.FromArgb(80, 255, 255, 255))) { g.FillRectangle(brushAlpha, rectTop); } } rect.Inflate(-1, -1); using (GraphicsPath path1 = CreateTabPath(rect)) { using (Pen pen = new Pen(Color.FromArgb(255, 255, 255))) { if (Multiline) { g.DrawPath(pen, path1); } else { g.DrawLines(pen, path1.PathPoints); } } } using (Pen pen = new Pen(borderColor)) { if (Multiline) { g.DrawPath(pen, path); } { g.DrawLines(pen, path.PathPoints); } } } } private bool DrawTabImage(Graphics g, TabPage page, Rectangle rect) { bool hasImage = false ; if (ImageList != null ) { Image image = null ; if (page.ImageIndex != -1) { image = ImageList.Images[page.ImageIndex]; } else if (page.ImageKey != null ) { image = ImageList.Images[page.ImageKey]; } if (image != null ) { hasImage = true ; Rectangle destRect = Rectangle.Empty; Rectangle srcRect = new Rectangle(Point.Empty, image.Size); switch (Alignment) { case TabAlignment.Top: case TabAlignment.Bottom: destRect = new Rectangle( rect.X + Radius / 2 + 2, rect.Y + 2, rect.Height - 4, rect.Height - 4); break ; case TabAlignment.Left: destRect = new Rectangle( rect.X + 2, rect.Bottom - (rect.Width - 4) - Radius / 2 - 2, rect.Width - 4, rect.Width - 4); break ; case TabAlignment.Right: destRect = new Rectangle( rect.X + 2, rect.Y + Radius / 2 + 2, rect.Width - 4, rect.Width - 4); break ; } g.DrawImage( image, destRect, srcRect, GraphicsUnit.Pixel); } } return hasImage; } private GraphicsPath CreateTabPath(Rectangle rect) { GraphicsPath path = new GraphicsPath(); switch (Alignment) { case TabAlignment.Top: rect.X++; rect.Width -= 2; path.AddLine( rect.X, rect.Bottom, rect.X, rect.Y + Radius / 2); path.AddArc( rect.X, rect.Y, Radius, Radius, 180F, 90F); path.AddArc( rect.Right - Radius, rect.Y, Radius, Radius, 270F, 90F); path.AddLine( rect.Right, rect.Y + Radius / 2, rect.Right, rect.Bottom); break ; case TabAlignment.Bottom: rect.X++; rect.Width -= 2; path.AddLine( rect.X, rect.Y, rect.X, rect.Bottom - Radius / 2); path.AddArc( rect.X, rect.Bottom - Radius, Radius, Radius, 180, -90); path.AddArc( rect.Right - Radius, rect.Bottom - Radius, Radius, Radius, 90, -90); path.AddLine( rect.Right, rect.Bottom - Radius / 2, rect.Right, rect.Y); break ; case TabAlignment.Left: rect.Y++; rect.Height -= 2; path.AddLine( rect.Right, rect.Y, rect.X + Radius / 2, rect.Y); path.AddArc( rect.X, rect.Y, Radius, Radius, 270F, -90F); path.AddArc( rect.X, rect.Bottom - Radius, Radius, Radius, 180F, -90F); path.AddLine( rect.X + Radius / 2, rect.Bottom, rect.Right, rect.Bottom); break ; case TabAlignment.Right: rect.Y++; rect.Height -= 2; path.AddLine( rect.X, rect.Y, rect.Right - Radius / 2, rect.Y); path.AddArc( rect.Right - Radius, rect.Y, Radius, Radius, 270F, 90F); path.AddArc( rect.Right - Radius, rect.Bottom - Radius, Radius, Radius, 0F, 90F); path.AddLine( rect.Right - Radius / 2, rect.Bottom, rect.X, rect.Bottom); break ; } path.CloseFigure(); return path; } private Color GetColor(Color colorBase, int a, int r, int g, int b) { int a0 = colorBase.A; int r0 = colorBase.R; int g0 = colorBase.G; int b0 = colorBase.B; if (a + a0 > 255) { a = 255; } else { a = Math.Max(a + a0, 0); } if (r + r0 > 255) { r = 255; } else { r = Math.Max(r + r0, 0); } if (g + g0 > 255) { g = 255; } else { g = Math.Max(g + g0, 0); } if (b + b0 > 255) { b = 255; } else { b = Math.Max(b + b0, 0); } return Color.FromArgb(a, r, g, b); } #endregion #region UpDownButtonNativeWindow private class UpDownButtonNativeWindow : NativeWindow, IDisposable { private TabControlEx _owner; private bool _bPainting; public UpDownButtonNativeWindow(TabControlEx owner) : base () { _owner = owner; base .AssignHandle(owner.UpDownButtonHandle); } private bool LeftKeyPressed() { if (SystemInformation.MouseButtonsSwapped) { return (NativeMethods.GetKeyState(NativeMethods.VK_RBUTTON) < 0); } else { return (NativeMethods.GetKeyState(NativeMethods.VK_LBUTTON) < 0); } } private void DrawUpDownButton() { bool mouseOver = false ; bool mousePress = LeftKeyPressed(); bool mouseInUpButton = false ; NativeMethods.RECT rect = new NativeMethods.RECT(); NativeMethods.GetClientRect( base .Handle, ref rect); Rectangle clipRect = Rectangle.FromLTRB( rect.Top, rect.Left, rect.Right, rect.Bottom); Point cursorPoint = new Point(); NativeMethods.GetCursorPos( ref cursorPoint); NativeMethods.GetWindowRect( base .Handle, ref rect); mouseOver = NativeMethods.PtInRect( ref rect, cursorPoint); cursorPoint.X -= rect.Left; cursorPoint.Y -= rect.Top; mouseInUpButton = cursorPoint.X < clipRect.Width / 2; using (Graphics g = Graphics.FromHwnd( base .Handle)) { UpDownButtonPaintEventArgs e = new UpDownButtonPaintEventArgs( g, clipRect, mouseOver, mousePress, mouseInUpButton); _owner.OnPaintUpDownButton(e); } } protected override void WndProc( ref Message m) { switch (m.Msg) { case NativeMethods.WM_PAINT: if (!_bPainting) { NativeMethods.PAINTSTRUCT ps = new NativeMethods.PAINTSTRUCT(); _bPainting = true ; NativeMethods.BeginPaint(m.HWnd, ref ps); DrawUpDownButton(); NativeMethods.EndPaint(m.HWnd, ref ps); _bPainting = false ; m.Result = NativeMethods.TRUE; } else { base .WndProc( ref m); } break ; default : base .WndProc( ref m); break ; } } #region IDisposable 成员 public void Dispose() { _owner = null ; base .ReleaseHandle(); } #endregion } #endregion } } |
1 #region 为TabControl添加关闭按钮
2 const int CLOSE_SIZE = 15;
3 //关闭按钮功能
4 private void MainTabControl_MouseDown(object sender, MouseEventArgs e)
5 {
6 if (tabContent.SelectedTab.Name != "tabPageWelcome")
7 {
8 if (e.Button == MouseButtons.Left)
9 {
10 int x = e.X, y = e.Y;
11 //计算关闭区域
12 Rectangle myTabRect = this.tabContent.GetTabRect(this.tabContent.SelectedIndex);
13
14 myTabRect.Offset(myTabRect.Width - (CLOSE_SIZE + 3), 2);
15 myTabRect.Width = CLOSE_SIZE;
16 myTabRect.Height = CLOSE_SIZE;
17
18 //如果鼠标在区域内就关闭选项卡
19 bool isClose = x > myTabRect.X && x < myTabRect.Right && y > myTabRect.Y && y < myTabRect.Bottom;
20 if (isClose == true)
21 {
22 this.tabContent.TabPages.Remove(this.tabContent.SelectedTab);
23 }
24 }
25 }
26 }
27 #endregion
源码下载地址:https://files.cnblogs.com/lmlblog/TabControlEX.rar
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】博客园社区专享云产品让利特惠,阿里云新客6.5折上折
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步