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 } }