欢迎莅临 SUN WU GANG 的园子!!!

世上无难事,只畏有心人。有心之人,即立志之坚午也,志坚则不畏事之不成。

  博客园 :: 首页 :: 博问 :: 闪存 :: 新随笔 :: 联系 :: 订阅 订阅 :: 管理 ::
  470 随笔 :: 0 文章 :: 22 评论 :: 30万 阅读
< 2025年3月 >
23 24 25 26 27 28 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 1 2 3 4 5

实例一:

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
using System;
using System.ComponentModel;
using System.Drawing;
using System.Windows.Forms;
 
namespace KUN.CONTROL.LIB.CONTROL
{
    public partial class KMenuTabControl : System.Windows.Forms.TabControl
    {
        #region 属性、构造
        Color SelectedColor = Color.LightSkyBlue;
        Color MoveColor = Color.LightSeaGreen;
        Color FontColor = Color.Black;
        int TextLeft = 10;
        [Browsable(true)]
        [Description("选项卡标题左边距"), Category("TextLeft"), DefaultValue(typeof(Int32), "10")]
        public int TitleTextLeft
        {
            get { return TextLeft; }
            set { this.TextLeft = value; }
        }
 
        [Browsable(true)]
        [Description("选项卡标题字体颜色"), Category("TitleColor"), DefaultValue(typeof(Color), "Black")]
        public Color TitleFontColor
        {
            get { return FontColor; }
            set { this.FontColor = value; }
        }
 
        [Browsable(true)]
        [Description("选项卡标题字体选中颜色"), Category("TitleColor"), DefaultValue(typeof(Color), "LightSkyBlue")]
        public Color TitleSelectedColor
        {
            get { return SelectedColor; }
            set { this.SelectedColor = value; }
        }
 
        [Browsable(true)]
        [Description("选项卡标题字体悬浮颜色"), Category("TitleColor"), DefaultValue(typeof(Color), "White")]
        public Color TitleMoveColor
        {
            get { return MoveColor; }
            set { this.MoveColor = value; }
        }
 
        [Browsable(true), Description("整个控件的背景色"), Category("外观")]
        public Color TabControlBackColor { get; set; }
 
        [Browsable(true), Description("TabControl ItemSize"), Category("外观")]
        public Size TabControlItemSize { get; set; }
 
        public KMenuTabControl()
        {
            this.SuspendLayout();
            this.DrawMode = TabDrawMode.OwnerDrawFixed;
            this.ResumeLayout(false);
            this.SizeMode = TabSizeMode.Fixed;
            this.Multiline = true;
            this.TabControlBackColor = Color.SeaShell;
            this.TabControlItemSize = new Size(100, 28);
            this.DrawItem += new System.Windows.Forms.DrawItemEventHandler(this.tabMenu_DrawItem);
            this.MouseDown += new System.Windows.Forms.MouseEventHandler(this.MainTabControl_MouseDown);
        }
        #endregion
 
        protected override void OnPaint(PaintEventArgs e)
        {
            Rectangle tcRec = this.ClientRectangle;//整个tabControl的边框
            e.Graphics.FillRectangle(new SolidBrush(this.TabControlBackColor), tcRec);
            if (this.ItemSize!=this.TabControlItemSize)
            {
                this.ItemSize = TabControlItemSize;
            }
 
            StringFormat sf = new StringFormat();//封装文本布局信息
            sf.LineAlignment = StringAlignment.Center;
            sf.Alignment = StringAlignment.Near;
            for (int i = 0; i < this.TabCount; i++)
            {
                Graphics g = e.Graphics;
                // int width = (int)g.MeasureString(this.Controls[i].Text, this.Font).Width + 40;
                Rectangle rect = this.GetTabRect(i);
                //  rect.Width = width;
                if (this.SelectedIndex == i)
                    g.FillRectangle(new SolidBrush(MoveColor), rect);
                else g.FillRectangle(new SolidBrush(SelectedColor), rect);
 
                SolidBrush brush = new SolidBrush(FontColor);
                //  rect.Width = width;
                rect.X += TextLeft;
                g.DrawString(this.Controls[i].Text, this.Font, brush, rect, sf);
                using (Pen objpen = new Pen(Color.Black))
                {
                    int tx = (int)(rect.X + (rect.Width - 30));
                    rect.X = tx - 2;
                    Point p5 = new Point(tx, 8);
                    Font font = new System.Drawing.Font("微软雅黑", 12);
                    g.DrawString("〇", font, brush, rect, sf);
                    font = new System.Drawing.Font("微软雅黑", 11);
                    rect.X = tx + 2;
                    rect.Y = rect.Y - 1;
                    g.DrawString("×", font, brush, rect, sf);
                }
            }
        }
 
        public override Rectangle DisplayRectangle
        {
            get
            {
                Rectangle rect = base.DisplayRectangle;
                return new Rectangle(rect.Left - 2, rect.Top - 2, rect.Width + 4, rect.Height + 5);
            }
        }
 
        int index = -1;
        protected override void OnMouseMove(MouseEventArgs e)
        {
            int Count = 0;
            try
            {
                Graphics g = this.CreateGraphics();
                SolidBrush brush = new SolidBrush(FontColor);
                StringFormat sf = new StringFormat();//封装文本布局信息
                sf.LineAlignment = StringAlignment.Center;
                sf.Alignment = StringAlignment.Near;
 
                for (int i = 0; i < this.TabPages.Count; i++)
                {
                    TabPage tp = this.TabPages[i];
                    if (this.GetTabRect(i).Contains(e.Location) && tp != this.SelectedTab)
                    {
                        if (index != i)
                        {
                            if (Count == 0)
                            {
                                if (index != -1 && this.TabPages[index] != this.SelectedTab)
                                {
                                    g.FillRectangle(new SolidBrush(SelectedColor), this.GetTabRect(index));
 
                                    RectangleF tRectangle = this.GetTabRect(index);
                                    tRectangle.X += TextLeft;
                                    g.DrawString(this.Controls[index].Text, this.Font, brush, tRectangle, sf);
                                }
                                Count = 1;
                            }
                            index = i;
                            g.FillRectangle(new SolidBrush(SelectedColor), this.GetTabRect(i));
                            RectangleF tRectangleF = this.GetTabRect(i);
                            tRectangleF.X += TextLeft;
                            g.DrawString(this.Controls[i].Text, this.Font, brush, tRectangleF, sf);
                            using (Pen objpen = new Pen(Color.Black))
                            {
                                int tx = (int)(tRectangleF.X + (tRectangleF.Width - 30));
                                tRectangleF.X = tx - 2;
                                brush.Color = Color.White;
                                Font font = new System.Drawing.Font("微软雅黑", 12);
                                g.DrawString("〇", font, brush, tRectangleF, sf);
                                font = new System.Drawing.Font("微软雅黑", 11);
                                tRectangleF.X = tx + 2;
                                tRectangleF.Y = tRectangleF.Y - 1;
                                g.DrawString("×", font, brush, tRectangleF, sf);
                            }
                        }
                    }
                    if (this.GetTabRect(i).Contains(e.Location) && tp == this.SelectedTab)
                    {
                        if (index != -1 && index != this.SelectedIndex)
                        {
                            g.FillRectangle(new SolidBrush(SelectedColor), this.GetTabRect(index));
                            RectangleF tRectangleF = this.GetTabRect(index);
                            tRectangleF.X += TextLeft;
                            g.DrawString(this.Controls[index].Text, this.Font, brush, tRectangleF, sf);
                            using (Pen objpen = new Pen(Color.Black))
                            {
                                int tx = (int)(tRectangleF.X + (tRectangleF.Width - 30));
                                tRectangleF.X = tx - 2;
                                Font font = new System.Drawing.Font("微软雅黑", 12);
                                g.DrawString("〇", font, brush, tRectangleF, sf);
                                font = new System.Drawing.Font("微软雅黑", 11);
                                tRectangleF.X = tx + 2;
                                tRectangleF.Y = tRectangleF.Y - 1;
                                g.DrawString("×", font, brush, tRectangleF, sf);
                            }
                        }
                        index = -1;
                    }
                }
            }
            catch (Exception)
            {
            }
            Count = 0;
            base.OnMouseMove(e);
        }
 
        protected override void OnMouseLeave(EventArgs e)
        {
            try
            {
                Graphics g = this.CreateGraphics();
                if (index != -1 && this.TabPages[index] != this.SelectedTab)
                {
                    g.FillRectangle(new SolidBrush(SelectedColor), this.GetTabRect(index));
                    SolidBrush brush = new SolidBrush(FontColor);
                    RectangleF tRectangleF = this.GetTabRect(index);
                    StringFormat sf = new StringFormat();//封装文本布局信息
                    sf.LineAlignment = StringAlignment.Center;
                    sf.Alignment = StringAlignment.Near;
                    tRectangleF.X += TextLeft;
                    g.DrawString(this.Controls[index].Text, this.Font, brush, tRectangleF, sf);
                    using (Pen objpen = new Pen(Color.Black))
                    {
                        int tx = (int)(tRectangleF.X + (tRectangleF.Width - 30));
                        tRectangleF.X = tx - 2;
                        Point p5 = new Point(tx, 8);
                        Font font = new System.Drawing.Font("微软雅黑", 12);
                        g.DrawString("〇", font, brush, tRectangleF, sf);
                        font = new System.Drawing.Font("微软雅黑", 11);
                        tRectangleF.X = tx + 2;
                        tRectangleF.Y = tRectangleF.Y - 1;
                        g.DrawString("×", font, brush, tRectangleF, sf);
                    }
                }
            }
            catch (Exception)
            {
            }
            index = -1;
            base.OnMouseLeave(e);
        }
        /// <summary>
        /// 重绘控件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void tabMenu_DrawItem(object sender, DrawItemEventArgs e)
        {
            this.SetStyle(
            ControlStyles.UserPaint |                      // 控件将自行绘制,而不是通过操作系统来绘制 
            ControlStyles.OptimizedDoubleBuffer |          // 该控件首先在缓冲区中绘制,而不是直接绘制到屏幕上,这样可以减少闪烁 
            ControlStyles.AllPaintingInWmPaint |           // 控件将忽略 WM_ERASEBKGND 窗口消息以减少闪烁 
            ControlStyles.ResizeRedraw |                   // 在调整控件大小时重绘控件 
            ControlStyles.SupportsTransparentBackColor,    // 控件接受 alpha 组件小于 255 的 BackColor 以模拟透明 
            true);                                         // 设置以上值为 true 
            this.UpdateStyles();
        }
 
        //关闭按钮功能
        private void MainTabControl_MouseDown(object sender, MouseEventArgs e)
        {
            int closeSize = 20;
            if (e.Button == MouseButtons.Left)
            {
                int x = e.X, y = e.Y;
                //计算关闭区域  
                Rectangle tab = this.GetTabRect(this.SelectedIndex);
                tab.Offset(tab.Width - (closeSize + 3), 4);
                tab.Width = closeSize;
                tab.Height = closeSize;
 
                if (this.TabCount == 1) return;
 
                //如果鼠标在区域内就关闭选项卡  
                bool isClose = x > tab.X && x < tab.Right && y > tab.Y && y < tab.Bottom;
                if (isClose == true)
                {
                    this.TabPages.Remove(this.SelectedTab);
                }
            }
        }
 
    }
}

实例二:

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
using System.ComponentModel;
using System.Drawing;
using System.Windows.Forms;
 
namespace KUN.CONTROL.LIB.CONTROL
{
    public partial class KTabControl : System.Windows.Forms.TabControl
    {
        [Browsable(true), Description("整个控件的背景色"), Category("外观")]
        public Color TabControlBackColor { get; set; }
 
        [Browsable(true), Description("Tab的标题栏边框颜色"), Category("外观")]
        public Color TabBorderColor { get; set; }
 
        [Browsable(true), Description("当前激活Tab的标题栏背景色"), Category("外观")]
        public Color ActivedTabBackColor { get; set; }
 
        [Browsable(true), Description("当前激活Tab的标题文字颜色"), Category("外观")]
        public Color ActivedTabLabelColor { get; set; }
 
        [Browsable(true), Description("未激活Tab的标题栏背景色"), Category("外观")]
        public Color InActivedTabBackColor { get; set; }
 
        [Browsable(true), Description("未激活Tab的标题文字颜色"), Category("外观")]
        public Color InActivedTabLabelColor { get; set; }
 
        [Browsable(true), Description("Tab标题栏的大小"), Category("外观")]
        public Size TabSize { get; set; }
 
        public KTabControl()
        {
            this.InitializeComponent();
 
            TabSet();
 
            this.TabBorderColor = Color.Black;
            this.ActivedTabLabelColor = Color.Black;
            this.InActivedTabLabelColor = Color.Black;
            this.ActivedTabBackColor = Color.White;
            this.InActivedTabBackColor = Color.FromArgb(0, 192, 192);
            this.TabControlBackColor = Color.Transparent;
            this.TabSize = new Size(100, 35);
        }
 
        protected override void OnMouseDoubleClick(MouseEventArgs e)
        {
            if (this.TabPages.Count == 1) return;
            this.TabPages.RemoveAt(this.SelectedIndex);
        }
 
        protected override void OnPaint(PaintEventArgs e)
        {
            Rectangle tcRec = this.ClientRectangle;//整个tabControl的边框
            e.Graphics.FillRectangle(new SolidBrush(this.TabControlBackColor), tcRec);
 
            for (int i = 0; i < this.TabPages.Count; i++)
            {
                Rectangle tabRectangle = new Rectangle(1, 1 + i * TabSize.Height, TabSize.Width, TabSize.Height);
                SolidBrush brush = new SolidBrush(this.InActivedTabLabelColor);
                StringFormat sf = new StringFormat();//封装文本布局信息
                sf.LineAlignment = StringAlignment.Center;
                sf.Alignment = StringAlignment.Center;
                if (i == this.SelectedIndex)
                {
                    brush = new SolidBrush(this.ActivedTabLabelColor);
                    e.Graphics.FillRectangle(new SolidBrush(ActivedTabBackColor), tabRectangle);
                    e.Graphics.DrawRectangle(new Pen(this.TabBorderColor), tabRectangle);
                }
                else
                {
                    e.Graphics.FillRectangle(new SolidBrush(InActivedTabBackColor), tabRectangle);
                    e.Graphics.DrawRectangle(new Pen(this.TabBorderColor), tabRectangle);
                }
                e.Graphics.DrawString(this.Controls[i].Text, this.Font, brush, tabRectangle, sf);
            }
        }
 
        /// <summary>
        /// 设定控件绘制模式
        /// </summary>
        private void TabSet()
        {
            this.DrawMode = TabDrawMode.OwnerDrawFixed;
            this.Alignment = TabAlignment.Left;
            this.SizeMode = TabSizeMode.Fixed;
            this.Multiline = true;
            base.SetStyle(
                ControlStyles.UserPaint |                     
                ControlStyles.OptimizedDoubleBuffer |          
                ControlStyles.AllPaintingInWmPaint |            
                ControlStyles.ResizeRedraw |                    
                ControlStyles.SupportsTransparentBackColor,     
                true);                                        
            base.UpdateStyles();
        }
 
        public override Rectangle DisplayRectangle
        {
            get
            {
                Rectangle rect = base.DisplayRectangle;
                return new Rectangle(rect.Left - 3, rect.Top - 3, rect.Width + 6, rect.Height + 5);
            }
        }
    }
}

  

posted on   sunwugang  阅读(6077)  评论(0编辑  收藏  举报
编辑推荐:
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
阅读排行:
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· 【自荐】一款简洁、开源的在线白板工具 Drawnix
· 没有Manus邀请码?试试免邀请码的MGX或者开源的OpenManus吧
· 园子的第一款AI主题卫衣上架——"HELLO! HOW CAN I ASSIST YOU TODAY
· 无需6万激活码!GitHub神秘组织3小时极速复刻Manus,手把手教你使用OpenManus搭建本
历史上的今天:
2016-09-09 android学习笔记38——样式和主题
点击右上角即可分享
微信分享提示