tangxu
2024-11-04 ebd031e3bed6c1cfddce8fc9b98f7f9e95fb9e32
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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Drawing;
using System.ComponentModel;
using static DPumpHydr.WinFrmUI.WenSkin.Controls.WenButton;
 
namespace DPumpHydr.WinFrmUI.WenSkin.Controls
{
    public class WenPaging : WenControl, ISupportInitialize
    {
        public WenPaging() : base()
        {
            int width = 414;
            int height = 32;
            int buttonWidth = 68;
 
            this.Size = new Size(width, height);
            WenButton bePageButton = new WenButton()
            {
                Location = new Point(1, 1),
                Size = new Size(buttonWidth, height - 2),
                Text = "上一页",
            };
            bePageButton.Click += (s, e) =>
            {
                if (PageIndex > 1)
                {
                    OnPagingButtonClick(new PagingButtonClickEventArgs(PageIndex - 1));
                }
            };
            WenButton nextPageButton = new WenButton()
            {
                Location = new Point(width - buttonWidth - 1, 1),
                Size = new Size(buttonWidth, height - 2),
                Text = "下一页",
                Anchor = AnchorStyles.Right | AnchorStyles.Top
            };
            nextPageButton.Click += (s, e) =>
            {
                if (PageIndex < MaxPageCout)
                {
                    OnPagingButtonClick(new PagingButtonClickEventArgs(PageIndex + 1));
                }
            };
 
            flow = new FlowLayoutPanel()
            {
                Location = new Point(1 + buttonWidth + 1, 1),
                Size = new Size(width - buttonWidth * 2 - 2, height - 2),
                Anchor = AnchorStyles.Left | AnchorStyles.Right | AnchorStyles.Top | AnchorStyles.Bottom,
            };
 
            this.Controls.Add(bePageButton);
            this.Controls.Add(nextPageButton);
            this.Controls.Add(flow);
        }
 
        #region 私有属性
 
        private readonly FlowLayoutPanel flow;
        private int pageIndex = 1;
        //private int maxPageButtonCount = 6;
        private readonly int pageButtonWidth = 38;
        private int maxPageCout = 1;
        private Color selectedPageColor = Color.FromArgb(63, 63, 70);
 
        #endregion
 
        #region 公有属性
 
        [Category("Wen"), Description("获取当前页数"), DefaultValue(1)]
        public int PageIndex { get => pageIndex; set { pageIndex = value; RefreshButton(); } }
        [Category("Wen"), Description("获取或设置最大页数"), DefaultValue(1)]
        public int MaxPageCout { get => maxPageCout; set { maxPageCout = value; RefreshButton(); } }
 
        [Category("Wen"), Description("是否自动调节控件宽度"), DefaultValue(false)]
        public bool AutoSizeWidth { get; set; }
 
        [Category("Wen"), Description("是否自动调节控件宽度"), DefaultValue(typeof(Color), "63,63,70")]
        public Color SelectedPageColor { get => selectedPageColor; set { selectedPageColor = value;this.Invalidate(); } }
        #endregion
 
        #region 按钮刷新
 
        private void RefreshButton()
        {
            flow.Controls.Clear();
            //先绑定第一页
            WenButton wenButtonOne = new WenButton("1")
            {
                Width = pageButtonWidth,
                Margin = new Padding(1, 0, 0, 0),
            };
            if (PageIndex == 1)
            {
                wenButtonOne.BackColor = SelectedPageColor;
            }
            wenButtonOne.Click += (s, e) =>
            {
                OnPagingButtonClick(new PagingButtonClickEventArgs(1));
            };
            flow.Controls.Add(wenButtonOne);
 
            if (maxPageCout < 2)
                return;
 
            int startIndex = 2;
            int endIndex = maxPageCout - 1;
 
            if (PageIndex - 2 > 1 && PageIndex + 2 < maxPageCout)
            {
                endIndex = PageIndex + 2;
                startIndex = PageIndex - 2;
            }
            else if (PageIndex - 2 < 2 && 7 - 1 < maxPageCout)
            {
                startIndex = 2;
                endIndex = 7 - 1;
            }
            else if (PageIndex - 2 < 1 && 7 - 1 > maxPageCout)
            {
                startIndex = 2;
                endIndex = maxPageCout - 1;
            }
            else if (PageIndex + 2 >= maxPageCout && maxPageCout - (7 - 2) > 1)
            {
                startIndex = maxPageCout - (7 - 2);
                endIndex = maxPageCout - 1;
            }
 
            for (int i = startIndex; i <= endIndex; i++)
            {
                int index = i;
                WenButton wenButton = new WenButton((i).ToString())
                {
                    Width = pageButtonWidth,
                    Margin = new Padding(1, 0, 0, 0),
                };
                if (PageIndex == i)
                {
                    wenButton.BackColor = SelectedPageColor;
                }
                wenButton.Click += (s, e) =>
                {
                    OnPagingButtonClick(new PagingButtonClickEventArgs(index));
                };
                flow.Controls.Add(wenButton);
            }
            //绑定最后一页
            WenButton wenButtonLast = new WenButton(maxPageCout.ToString())
            {
                Width = pageButtonWidth,
                Margin = new Padding(1, 0, 0, 0),
            };
            if (PageIndex == maxPageCout)
            {
                wenButtonLast.BackColor = SelectedPageColor;
            }
            wenButtonLast.Click += (s, e) =>
            {
                OnPagingButtonClick(new PagingButtonClickEventArgs(maxPageCout));
            };
            flow.Controls.Add(wenButtonLast);
 
 
            if (AutoSizeWidth)
            {
                this.Width = 70 * 2 + flow.Controls.Count * (pageButtonWidth + 1) + 1;
            }
        }
 
        #endregion
 
        #region 重绘
        protected override void WenOnPaint(Graphics g, Rectangle rec, PaintEventArgs e)
        {
            base.WenOnPaint(g, rec, e);
            g.DrawRectangle(Pens.Gray, 0, 0, Width - 1, Height - 1);
        }
        #endregion
        public void BeginInit()
        {
 
        }
 
        public void EndInit()
        {
            RefreshButton();
        }
 
        #region 委托
 
        public delegate void PagingButtonClickEventHandler(object sender, PagingButtonClickEventArgs e);
        [Category("Wen"), Description("按钮点击事件")]
        public event PagingButtonClickEventHandler PagingButtonClick;
        public class PagingButtonClickEventArgs : EventArgs
        {
            private readonly int pageIndex;
 
            public PagingButtonClickEventArgs(int pageIndex)
            {
                this.pageIndex = pageIndex;
            }
 
            public int PageIndex => pageIndex;
        }
 
        protected virtual void OnPagingButtonClick(PagingButtonClickEventArgs e)
        {
            PageIndex = e.PageIndex;
            PagingButtonClick?.Invoke(this, e);
        }
 
        #endregion     
    }
}