tangxu
2025-01-13 4f7cb65b079d88d5a829688b24d26d5145c5df47
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
using System;
using System.ComponentModel;
using System.Drawing;
using System.Windows.Forms;
namespace DPumpHydr.WinFrmUI.WenSkin.Controls
{
    [ToolboxBitmap(typeof(TabControl))]
    public partial class WenTabControl : System.Windows.Forms.TabControl
    {
        public WenTabControl() : base()
        {
            this.DrawMode = TabDrawMode.OwnerDrawFixed;
            this.SizeMode = TabSizeMode.Fixed;
            this.ItemSize = new Size(120, 25);
 
            base.SetStyle(
                ControlStyles.UserPaint |
                ControlStyles.DoubleBuffer |
                ControlStyles.OptimizedDoubleBuffer |
                ControlStyles.AllPaintingInWmPaint |
                ControlStyles.ResizeRedraw |
                ControlStyles.SupportsTransparentBackColor, true);
            base.UpdateStyles();
        }
 
        #region 私有属性
 
        private int mouseMovePageIndex = -1;
        private bool mouseMovePageColse = false;
        private bool closeButtonEnable = true;
        private bool iconEnable=false;
 
        #endregion
 
        #region 公有属性
 
        [Category("Wen")]
        [Description("背景图片")]
        public Image BackImage { get; set; }
 
        [Category("Wen"), Description("是否显示关闭按钮")]
        public bool CloseButtonEnable { get => closeButtonEnable; set { closeButtonEnable = value; this.Invalidate(); } }
 
        [Category("Wen"), Description("是否显示图标"), DefaultValue(false)]
        public bool IconEnable { get => iconEnable; set => iconEnable = value; }
        #endregion
 
        protected override void OnPaint(PaintEventArgs e)
        {
            Graphics g = e.Graphics;
            g.SetGDIHigh();
            //base.OnPaint(e);
            Rectangle rec = this.ClientRectangle;
 
            Control control = this.Parent;
            Color color = control.BackColor;
            Brush brush = new SolidBrush(color);
 
            if (BackImage != null)
            {
                g.DrawImage(BackImage, new Rectangle(-2, -2, this.Width + 4, this.Height + 4));
            }
            else
            {
                g.FillRectangle(brush, new Rectangle(-2, -2, this.Width + 4, this.Height + 4));
            }
            StringFormat stringFormat = new StringFormat(StringFormatFlags.NoWrap)
            {
                Alignment = StringAlignment.Near,
                LineAlignment = StringAlignment.Center
            };
 
            //画一条长底纹实线条
            Rectangle recLine = new Rectangle(0, this.ItemSize.Height + 2, this.Width, 1);
            using (Brush b = new SolidBrush(Color.FromArgb(0, 122, 204)))
            {
                g.FillRectangle(b, recLine);
            }
 
 
            //获取当前选中项目索引
            int selectIndex = this.SelectedIndex;
            foreach (TabPage tab in this.TabPages)
            {
                int index = this.TabPages.IndexOf(tab);
                Rectangle rectab = this.GetTabRect(index);
 
                //背景颜色
                Brush back = new SolidBrush(color);
 
                if (index == selectIndex)
                    back = new SolidBrush(Color.FromArgb(0, 122, 204));
 
                //绘制选中底纹
                g.FillRectangle(back, rectab);
 
                ////绘制文字区域
                //Rectangle recStr = new Rectangle(rectab.X, rectab.Y, rectab.Width - 20, rectab.Height);
                ////绘制文字
                //g.DrawString(tab.Text, tab.Font, new SolidBrush(tab.ForeColor), recStr, stringFormat);
 
                ImageIcoOrText(tab, g, rectab);
 
                //绘制关闭按钮
                if (index == selectIndex)
                    CloseButton(g, rectab, false);
 
                //注销内存
                back.Dispose();
            }
            stringFormat.Dispose();
        }
 
        //画tab图标和文字
        private void ImageIcoOrText(TabPage tabPage, Graphics g, Rectangle rec)
        {
            StringFormat stringFormat = new StringFormat(StringFormatFlags.NoWrap)
            {
                Alignment = StringAlignment.Near,
                LineAlignment = StringAlignment.Center
            };
            if (IconEnable && tabPage is WenTabPage page && page.Image != null)
            {
                //绘制图标区域
                Rectangle recimage = new Rectangle(rec.X + 3, rec.Y + (rec.Height - 16) / 2, 16, 16);
                g.DrawImage(page.Image, recimage);
 
                //绘制文字区域
                Rectangle recStr = new Rectangle(recimage.X + recimage.Width + 3, rec.Y, rec.Width - 20 - (recimage.Width + 6), rec.Height);
                g.DrawString(page.Text, page.Font, new SolidBrush(page.ForeColor), recStr, stringFormat);
            }
            else
            {
                //绘制文字区域
                Rectangle recStr = new Rectangle(rec.X, rec.Y, rec.Width - 20, rec.Height);
                //绘制文字
                g.DrawString(tabPage.Text, tabPage.Font, new SolidBrush(tabPage.ForeColor), recStr, stringFormat);
            }
        }
 
        //关闭按钮功能
        protected override void OnMouseDown(MouseEventArgs e)
        {
            base.OnMouseDown(e);
 
            //关闭按钮不显示,功能取消
            if (!closeButtonEnable)
                return;
 
            if (e.Button == MouseButtons.Left)
            {
                if (mouseMovePageColse && mouseMovePageIndex > -1)
                {
                    this.TabPages.RemoveAt(mouseMovePageIndex);
                }
            }
        }
 
        //绘制关闭按钮
        private void CloseButton(Graphics g, Rectangle rec, bool close)
        {
            //当不显示关闭按钮直接返回
            if (!closeButtonEnable)
                return;
 
            int closeSize = 8;
 
            //获取关闭按钮Y需要移动区域 便于按钮居中
            int yMove = (ItemSize.Height - closeSize) / 2;
 
            //偏移矩形框
            rec.Offset(rec.Width - closeSize - 5, yMove);
            //重新设置矩形框的大小
            rec.Width = closeSize;
            rec.Height = closeSize;
 
            //如果关闭为True绘制圆圈背景效果
            if (close)
                g.FillEllipse(Brushes.DarkGray, rec.X - 4, rec.Y - 4, 16, 16);
 
            //用DrawLine绘制两条线段  实现X  样子效果
            using Pen pen = new Pen(Color.Gray)
            {
                Width = 2
            };
            g.DrawLine(pen, rec.X, rec.Y, rec.X + closeSize, rec.Y + closeSize);
            g.DrawLine(pen, rec.X, rec.Y + closeSize, rec.X + closeSize, rec.Y);
        }
        //鼠标移动事件
        protected override void OnMouseMove(MouseEventArgs e)
        {
            base.OnMouseMove(e);
 
            int beforeMouseIndex = mouseMovePageIndex;
            foreach (TabPage tab in this.TabPages)
            {
                int index = this.TabPages.IndexOf(tab);
                if (this.GetTabRect(index).Contains(e.Location))
                {
                    mouseMovePageIndex = index;
                }
            }
 
            if (mouseMovePageIndex == -1 || mouseMovePageIndex >= this.TabPages.Count)
                return;
            //获取关闭区域
            int closeSize = 20;
            Rectangle rec = this.GetTabRect(mouseMovePageIndex);
 
            //检查关闭区域
            Rectangle recClose = this.GetTabRect(mouseMovePageIndex);
            recClose.Offset(recClose.Width - closeSize, 4);
            recClose.Width = closeSize;
            recClose.Height = closeSize;
 
            bool mouseMovePageColse = this.mouseMovePageColse;
 
            if (recClose.Contains(e.Location))
            {
                this.mouseMovePageColse = true;
            }
            else
            {
                this.mouseMovePageColse = false;
            }
 
            //判定鼠标是否已经改变项
            if (beforeMouseIndex == mouseMovePageIndex && mouseMovePageColse == this.mouseMovePageColse)
            {
                return;
            }
 
            Graphics g = this.CreateGraphics();
            g.SetClip(new Rectangle(rec.X + 1, rec.Y + 1, rec.Width - 2, rec.Height - 2));
            //鼠标移动改变两处重绘
            if (this.mouseMovePageColse)
            {
                DrawTabPageLable(g, this.TabPages[mouseMovePageIndex], rec, Color.FromArgb(28, 151, 234), true);
            }
            else
            {
                DrawTabPageLable(g, this.TabPages[mouseMovePageIndex], rec, Color.FromArgb(28, 151, 234), false);
            }
 
            int selectIndex = this.SelectedIndex;
            Control control = this.Parent;
            Color color = control.BackColor;
 
            if (beforeMouseIndex == selectIndex)
                color = Color.DodgerBlue;
 
            //改变后的上一个信息
            if (beforeMouseIndex != -1 && beforeMouseIndex != mouseMovePageIndex && beforeMouseIndex < this.TabPages.Count)
            {
                Rectangle recBefore = this.GetTabRect(beforeMouseIndex);
                g.SetClip(recBefore);
                DrawTabPageLable(g, this.TabPages[beforeMouseIndex], recBefore, color, null);
            }
        }
 
        //画标签
        private void DrawTabPageLable(Graphics g, TabPage tab, Rectangle rec, Color color, bool? colse)
        {
            g.SetGDIHigh();
            using StringFormat stringFormat = new StringFormat(StringFormatFlags.NoWrap)
            {
                Alignment = StringAlignment.Near,
                LineAlignment = StringAlignment.Center
            };
 
            //声明画笔
            using Brush brushe = new SolidBrush(color);
            using Brush brusheStr = new SolidBrush(tab.ForeColor);
            //绘制选中底纹
            g.FillRectangle(brushe, rec);
 
            ////绘制文字区域
            //Rectangle recStr = new Rectangle(rec.X, rec.Y, rec.Width - 20, rec.Height);
            ////绘制文字
            //g.DrawString(tab.Text, tab.Font, brusheStr, recStr, stringFormat);
 
            ImageIcoOrText(tab, g, rec);
 
            //绘制关闭按钮
            if (colse == null)
            {
                if (this.SelectedTab == tab)
                {
                    CloseButton(g, rec, false);
                }
                return;
            }
 
            if (colse == true)
                CloseButton(g, rec, true);
            else
                CloseButton(g, rec, false);
 
        }
 
        //鼠标离开事件
        protected override void OnMouseLeave(EventArgs e)
        {
            base.OnMouseLeave(e);
 
            if (mouseMovePageIndex == -1 || mouseMovePageIndex >= this.TabPages.Count)
                return;
 
            Graphics g = this.CreateGraphics();
 
            int selectIndex = this.SelectedIndex;
            Control control = this.Parent;
            Color color = control.BackColor;
 
            if (mouseMovePageIndex == selectIndex)
                color = Color.DodgerBlue;
            //设置rec大小
            Rectangle recBefore = this.GetTabRect(mouseMovePageIndex);
            g.SetClip(recBefore);
 
            DrawTabPageLable(g, this.TabPages[mouseMovePageIndex], recBefore, color, null);
            mouseMovePageIndex = -1;
        }
 
        protected override void OnControlRemoved(ControlEventArgs e)
        {
            base.OnControlRemoved(e);
            mouseMovePageIndex = -1;
        }
    }
}