TabControl控件的美化
文件下载:https://files.cnblogs.com/zfanlong1314/TabControlEX.rar
本文转载:http://www.cnblogs.com/lmlblog/archive/2012/03/29/TabControl.html
最近因项目需要 所以就到网上找了一个美化过的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 #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
作者:阿笨
【官方QQ一群:跟着阿笨一起玩NET(已满)】:422315558
【官方QQ二群:跟着阿笨一起玩C#(已满)】:574187616
【官方QQ三群:跟着阿笨一起玩ASP.NET(已满)】:967920586
【官方QQ四群:Asp.Net Core跨平台技术开发(可加入)】:829227829
【官方QQ五群:.NET Core跨平台开发技术(可加入)】:647639415
【网易云课堂】:https://study.163.com/provider/2544628/index.htm?share=2&shareId=2544628
【腾讯课堂】:https://abennet.ke.qq.com
【51CTO学院】:https://edu.51cto.com/sd/66c64
【微信公众号】:微信搜索:跟着阿笨一起玩NET