tangxu
2024-10-22 6a07c4c846ffbb1e93afdf0260e123e4c145f419
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
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Windows.Forms;
using DPumpHydr.WinFrmUI.WenSkin.Controls;
 
 
namespace DPumpHydr.WinFrmUI.WenSkin.Forms
{
    public partial class WenTabControlForm : LeftButtonForm
    {
        public WenTabControlForm()
        {
            RefreshPadding();
            this.Name = "WenTabControlForm";
            this.Text = "WenTabControlForm";
 
            tabControl = new WenTabControl()
            {
                Dock = DockStyle.Fill,
                IconEnable=true,
            };
            tabControl.SelectedIndexChanged += (s, e) =>
            {
                this.Text = $"{DefaultText} - {tabControl.SelectedTab?.Text}";
            };
            tabControl.ControlRemoved += TabControl_ControlRemoved;
            this.Controls.Add(tabControl);
        }
 
        private void TabControl_ControlRemoved(object sender, ControlEventArgs e)
        {
            e.Control.Dispose();
        }
 
        private WenTabControl tabControl;
 
        #region 公有属性
        [Category("Wen"), Description("选项卡点击后默认显示内容"), DefaultValue(null)]
        public string DefaultText { get; set; }
 
        #endregion
 
        public void TabPageAdd(Control c, Image image = null)
        {
            TabControlAddEventArgs e = new TabControlAddEventArgs(c)
            {
                Image = image,
            };
            OnTabControlAdd(e);
        }
 
        protected override void OnLeftMenuClick(LeftMenuItem item)
        {
            base.OnLeftMenuClick(item);
            TabControlAddEventArgs e = new TabControlAddEventArgs(item);
            OnTabControlAdd(e);
        }
 
        //检查TabPage 是否存在
        public TabPage GetTabPage(string text)
        {
            foreach (TabPage item in tabControl.TabPages)
            {
                if (item.Text == text)
                    return item;
            }
            return null;
        }
 
        //显示指定页
        public void ShowTabPage(string text)
        {
            ShowTabPage(GetTabPage(text));
        }
        //显示指定页
        public void ShowTabPage(TabPage tab)
        {
            tabControl.SelectedTab = tab;
        }
 
        #region 委托事件
 
        protected virtual void OnTabControlAdd(TabControlAddEventArgs e)
        {
 
            foreach (WenTabPage p in tabControl.TabPages)
            {
                if (p.Text == e.Text)
                {
                    if (p.Controls.Count > 0 && e.Control == null)
                    {
                        e.Control = p.Controls[0];
                    }
                    tabControl.SelectedTab = p;
                    e.TabPage = p;
                    e.Show = true;
                    e.Refresh = false;
                    e.NewAdd = false;     
                }
            }
            if (e.TabPage == null)
            {
                e.TabPage = new WenTabPage(e.Text) { BackColor = Color.Transparent, Image = e.Image ?? e.Item.Image };
            }
 
            TabControlAdd?.Invoke(this, e);
 
            if (e.State)
            {
                if (e.NewAdd && !tabControl.TabPages.Contains(e.TabPage))
                {
                    tabControl.TabPages.Add(e.TabPage);
                    tabControl.SelectedTab = e.TabPage;
                    this.Text = $"{DefaultText} - {e.TabPage.Text}";
                }
                if (e.Control != null && e.Refresh)
                {
                    e.TabPage.Controls.Clear();
                    e.Control.Dock = DockStyle.Fill;
                    e.TabPage.Controls.Add(e.Control);    
 
                    Point mouseOff = new Point(0, 0); //鼠标移动位置变量
                    bool leftFlag = false; //标签是否为左键
 
                    e.Control.MouseDown += (s, e) =>
                    {
                        if (e.Button == MouseButtons.Left)
                        {
                            var point = this.PointToClient(MousePosition);
                            mouseOff = new Point(-point.X, -point.Y);
                            leftFlag = true;
                            //点击左键按下时标注为true;
                        }
                    };
                    e.Control.MouseMove += (s, e) =>
                    {
                        if (leftFlag)
                        {
                            Point mouseSet = MousePosition;
                            mouseSet.Offset(mouseOff.X, mouseOff.Y);
                            //设置移动后的位置
                            Location = mouseSet;
                        }
                    };
                    e.Control.MouseUp += (s, e) =>
                    {
                        if (leftFlag)
                        {
                            leftFlag = false;
                            //释放鼠标后标注为false;
                        }
                    };
                }
            }
        }
        //声明 API 函数
        [DllImport("User32.dll", EntryPoint = "SendMessage")]
        private static extern IntPtr SendMessage(IntPtr hWnd, int msg, IntPtr wParam, IntPtr lParam);
        public class TabControlAddEventArgs : EventArgs
        {
            public TabControlAddEventArgs()
            {
                TabPage = null;
                Control = null;
                Show = false;
                State = true;
                Refresh = true;
                NewAdd = true;
            }
            public TabControlAddEventArgs(string text) : this()
            {
                Item = new LeftMenuItem(text);
                Text = text;
            }
            public TabControlAddEventArgs(Control c) : this(c.Text)
            {
                Control = c;
            }
            public TabControlAddEventArgs(LeftMenuItem item) : this()
            {
                Item = item;
                Text = item.Text;
            }
 
            private Control control;
            private bool newAdd;
            private string text;
            private Image image;
 
            public LeftMenuItem Item { get; set; }
            public Control Control { get => control; set { control = value; Refresh = true; } }
            [Category("Wen"), Description("当前选项卡"), DefaultValue(null)]
            public WenTabPage TabPage { get; set; }
            [Category("Wen"), Description("是否已经显示"), DefaultValue(null)]
            public bool Show { get; set; }
            [Category("Wen"), Description("是否继续处理事件"), DefaultValue(null)]
            public bool State { get; set; }
            [Category("Wen"), Description("标签页显示内容"), DefaultValue(null)]
            public string Text
            {
                get => text;
                set
                {
                    text = value;
                    if (TabPage != null)
                        TabPage.Text = value;
                }
            }
            [Category("Wen"), Description("标签页文字显示内容"), DefaultValue(null)]
            public string TabPageText { get => TabPage.Text; set => TabPage.Text = value; }
 
            [Category("Wen"), Description("是否刷新内容"), DefaultValue(null)]
            public bool Refresh { get; set; }
            [Category("Wen"), Description("是否添加新标签页"), DefaultValue(null)]
            public bool NewAdd { get => newAdd; set { newAdd = value; if (!value) State = false; } }
 
            [Category("Wen"), Description("标签图标"), DefaultValue(null)]
            public Image Image { get => image; set { image = value; if (TabPage != null) TabPage.Image = value; } }
        }
 
        #endregion
 
        #region 委托
 
        public delegate void TabControlAddEventHandler(object sender, TabControlAddEventArgs e);
 
        [Category("Wen"), Description("选项卡添加事件")]
 
        public event TabControlAddEventHandler TabControlAdd;
 
        #endregion
    }
}