TabControl控件的美化

文件下载:http://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组件的代码

+ View Code

 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

本文参与腾讯云自媒体分享计划,欢迎正在阅读的你也加入,一起分享。

发表于

我来说两句

0 条评论
登录 后参与评论

相关文章

来自专栏Hongten

My Notepad

I have spent near more two weeks to write this Notepad application. At this mome...

1062
来自专栏菩提树下的杨过

发布一个轻量级的滑块控件

比系统自带的组件体积要小很多,而且支持进度条显示(在做播放器时,显示缓冲进度很有用哦),另外也支持三角形的音量调整显示 使用示例: package { imp...

3788
来自专栏跟着阿笨一起玩NET

c#实现打印功能

5602
来自专栏张善友的专栏

LINQ via C# 系列文章

LINQ via C# Recently I am giving a series of talk on LINQ. the name “LINQ via C...

3715
来自专栏lonelydawn的前端猿区

前端验证码绘制(canvas)

一切尽在代码中 js文件 /** * canvas绘制动画浮动验证码 * code by lonelydawn 2017-04-10 */ var c...

3187
来自专栏技术之路

c# 调用win32模拟点击的两种方法

第一种 using System; using System.Collections.Generic; using System.ComponentModel;...

23510
来自专栏菩提树下的杨过

利用ActiveX实现web页面设置本地默认打印机、纸张大小

通常web技术无法设置本地计算机的默认打印机,包括用代码设置纸张大小,如果业务系统中真遇到这种需求,只能通过其它辅助手段(比如ActiveX)实现。下面这段代码...

2291
来自专栏PPV课数据科学社区

【学习】七天搞定SAS(三):基本模块调用

搞定基本的函数之后,开始鼓捣SAS里面的模型。也就是说,要开始写PROC了。说实话,越学SAS,越觉得SAS像Stata...无论是从输出的样式,还是语法。好不...

3195
来自专栏叁金大数据

EmguCV学习——简单算法 差分与高斯

公司项目需要检测运动物体,我对opencv也没啥研究,google了好久看了好多方法,最简单的就是差分与高斯背景建模了。

1453
来自专栏跟着阿笨一起玩NET

DataTable导入到Excel文件

1741

扫码关注云+社区