winform--分页控件
新建一个用户组件,直接上代码:
/* * 作者:pengyan zhang * 邮箱:3073507793@qq.com * 博客:https://www.cnblogs.com/zpy1993-09 * 时间:2024-04-12 18:36 */ public partial class CPPageComponent : FlowLayoutPanel { #region 事件 /// <summary> /// 委托 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> public delegate void CPPageEventHandler(object sender, PageEventArgs e); /// <summary> /// 开关状态发生改变时引发的事件 /// </summary> public event CPPageEventHandler PageEvent; #endregion /// <summary> /// 当点击某一页被选中时 /// </summary> [Category("SimpleUI"), Description("被选中时的背景颜色"), DefaultValue(typeof(Color), "0, 159, 149")] public Color CP_PageCheckBackColor { get { return PageCheckBackColor; } set { PageCheckBackColor = value; } } private Color PageCheckBackColor = Color.FromArgb(0, 159, 149); /// <summary> /// 当某一页未被选中时的背景颜色 /// </summary> [Category("SimpleUI"), Description("未被选中时的背景颜色"), DefaultValue(typeof(Color), "255, 255, 255")] public Color CP_PageUnCheckBackColor { get { return PageUnCheckBackColor; } set { PageUnCheckBackColor = value; } } private Color PageUnCheckBackColor = Color.FromArgb(255, 255, 255); /// <summary> /// 当某一页被选中时字体颜色 /// </summary> [Category("SimpleUI"), Description("被选中时的字体颜色"), DefaultValue(typeof(Color), "255, 255, 255")] public Color CP_PageCheckFontColor { get { return PageCheckBackColor; } set { PageCheckBackColor = value; } } private Color PageCheckFontColor= Color.FromArgb(255, 255, 255); /// <summary> /// 当某一页未被选中的字体颜色 /// </summary> [Category("SimpleUI"), Description("未被选中时的字体颜色"), DefaultValue(typeof(Color), "0, 0,0")] public Color CP_PageUnCheckFontColor { get { return PageUnCheckBackColor; } set { PageUnCheckBackColor = value; } } private Color PageUnCheckFontColor = Color.FromArgb(0, 0, 0); /// <summary> /// 当鼠标进入某一页时 /// </summary> [Category("SimpleUI"), Description("鼠标进入时的背景颜色"), DefaultValue(typeof(Color), "224, 224, 224")] public Color CP_ageEneterColor { get { return PageEneterColor; } set { PageEneterColor = value; } } private Color PageEneterColor= Color.FromArgb(224, 224, 224); private Label labelLastPage { get; set; } private Label labelNextPage { get; set; } private TextBox PageBox { get; set; } private Label CurrentPageControl { get; set; } private Label labelLeft { get; set; } private Label labelRight { get; set; } private List<Label> pages { get; set; } private int page { get; set; } = 0; private int PageSize { get; set; } = 10; private int PageIndex { get; set; } = 1; private int SumPage { get; set; } private Label label_StartPage { get; set; } private Label label_EndPage { get; set; } private int[] PageArr { get; set; } = new[] { 10, 20, 50, 100 }; private FlowLayoutPanel flowLayoutPanel { get; set; } private Label Drop { get;set; } private Popup pop { get; set; } public CPPageComponent() { InitializeComponent(); } public CPPageComponent(IContainer container) { container.Add(this); InitializeComponent(); } private void page_Painit(object? sender, PaintEventArgs e) { Control control = sender as Control; ControlPaint.DrawBorder(e.Graphics,//获取进行绘制的图形 new Rectangle(0, 0, control.Width, control.Height),//绘制控件所在工作区域 Color.FromArgb(224, 224, 224), 1, ButtonBorderStyle.Solid,//边框类型,选择实线边框,也有虚线等种类;下面这四个重复的传参是设置工作区上下左右边框的功能 Color.FromArgb(224, 224, 224), 1, ButtonBorderStyle.Solid, Color.FromArgb(224, 224, 224), 1, ButtonBorderStyle.Solid, Color.FromArgb(224, 224, 224), 1, ButtonBorderStyle.Solid); } protected override void OnResize(EventArgs ea) { this.Height = 31; base.OnResize(ea); } private void Label_Click(object? sender, EventArgs e) { var label = sender as Label; if ((int)label.Tag == (int)CurrentPageControl.Tag) return; int currentpage = (int)label.Tag; this.PageIndex= currentpage; PrePage(currentpage, label); } private void label_MouseEnter_Click(object? sender, EventArgs e) { var label = sender as Label; if (CurrentPageControl != label) { label.Cursor = Cursors.Hand; label.BackColor = PageEneterColor; } } private void label_MouseLeave_Click(object? sender, EventArgs e) { var label = sender as Label; if (CurrentPageControl != label) label.BackColor = PageUnCheckBackColor; } private void Pagebox_TextChanged(object? sender, EventArgs e) { PageBox.Text = Regex.Replace(PageBox.Text, "[^0-9]", ""); if (PageBox.Text == "0") PageBox.Text = ""; } private void label_Sure_MouseEnter_Click(object? sender, EventArgs e) { var label = sender as Label; label.Cursor = Cursors.Hand; label.BackColor = PageEneterColor; label.ForeColor = PageUnCheckFontColor; } private void label_Sure_MouseLeave_Click(object? sender, EventArgs e) { var label = sender as Label; label.BackColor = PageUnCheckBackColor; } private void label_Sure_Click(object? sender, EventArgs e) { if (string.IsNullOrEmpty(PageBox.Text)) return; int pageCount = int.Parse(PageBox.Text); PageSure(pageCount); } private void labelNextPage_MouseEnter_Click(object? sender, EventArgs e) { var label = sender as Label; if ((int)CurrentPageControl.Tag == page) label.Cursor = Cursors.No; else { label.Cursor = Cursors.Hand; label.BackColor = PageEneterColor; } } private void labelNextPage_MouseLeave_Click(object? sender, EventArgs e) { var label = sender as Label; label.BackColor = PageUnCheckBackColor ; } private void labelNextPage_Click(object? sender, EventArgs e) { if ((int)CurrentPageControl.Tag == page) return; if ((int)CurrentPageControl.Tag == page-1 && page > 6) { CurrentPageControl.BackColor = PageUnCheckBackColor; CurrentPageControl.ForeColor = PageUnCheckFontColor; CurrentPageControl = label_EndPage; CurrentPageControl.BackColor = PageCheckBackColor; CurrentPageControl.ForeColor = PageUnCheckFontColor; return; } for (int i = 0; i < pages.Count; i++) { if ((int)CurrentPageControl.Tag + 1 == (int)pages[i].Tag){ PrePage((int)CurrentPageControl.Tag+1, pages[i]); break;} } } private void LastPage_MouseEnter_Click(object? sender, EventArgs e) { var label = sender as Label; if ((int)CurrentPageControl.Tag == 1) label.Cursor = Cursors.No; else {label.Cursor = Cursors.Hand;label.BackColor = PageEneterColor;} } private void LastPage_MouseLeave_Click(object? sender, EventArgs e) { var label = sender as Label; label.BackColor = PageUnCheckBackColor; } private void LastPage_Click(object? sender, EventArgs e) { if ((int)CurrentPageControl.Tag == 1) return; if ((int)CurrentPageControl.Tag == 2&&page>6) { CurrentPageControl.BackColor = PageUnCheckBackColor; CurrentPageControl.ForeColor = PageUnCheckFontColor; CurrentPageControl = label_StartPage; CurrentPageControl.BackColor = PageCheckBackColor; CurrentPageControl.ForeColor = PageCheckFontColor; return; } for (int i = 0; i < pages.Count; i++) { if ((int)CurrentPageControl.Tag - 1 == (int)pages[i].Tag) { PrePage((int)CurrentPageControl.Tag -1, pages[i]);break; } } } /// <summary> /// 初始化分页 /// </summary> /// <param name="page"></param> public void SetPage(int page) { this.Controls.Clear(); if (page == 0) return; labelLastPage = new Label(); labelLastPage.AutoSize = false; labelLastPage.Size = new Size(60, 30); labelLastPage.Text = "上一页"; labelLastPage.TextAlign = ContentAlignment.MiddleCenter; labelLastPage.BackColor = PageUnCheckBackColor; labelLastPage.Margin = new Padding(0, 0, 2, 0); labelLastPage.Paint += page_Painit; labelLastPage.Click += LastPage_Click; labelLastPage.MouseLeave += LastPage_MouseLeave_Click; labelLastPage.MouseEnter += LastPage_MouseEnter_Click; this.Controls.Add(labelLastPage); DealPage(page); labelNextPage = new Label(); labelNextPage.AutoSize = false; labelNextPage.Size = new Size(60, 30); labelNextPage.Text = "下一页"; labelNextPage.Margin = new Padding(0, 0, 10, 0); labelNextPage.TextAlign = ContentAlignment.MiddleCenter; labelNextPage.BackColor = PageUnCheckBackColor; labelNextPage.Paint += page_Painit; labelNextPage.Click += labelNextPage_Click; labelNextPage.MouseLeave += labelNextPage_MouseLeave_Click; labelNextPage.MouseEnter += labelNextPage_MouseEnter_Click; this.Controls.Add(labelNextPage); Label labelLastRange = new Label(); labelLastRange.AutoSize = false; labelLastRange.Size = new Size(40, 30); labelLastRange.Text = "到第"; labelLastRange.Margin = new Padding(0, 0, 0, 0); labelLastRange.TextAlign = ContentAlignment.MiddleCenter; labelLastRange.BackColor = PageUnCheckBackColor; this.Controls.Add(labelLastRange); PageBox = new TextBox(); PageBox.Size = new Size(50, 30); PageBox.Margin = new Padding(0, 2, 0, 0); PageBox.Paint += page_Painit; PageBox.TextChanged += Pagebox_TextChanged; this.Controls.Add(PageBox); Label labelRangePage = new Label(); labelRangePage.AutoSize = false; labelRangePage.Size = new Size(30, 30); labelRangePage.Text = "页"; labelRangePage.Margin = new Padding(0, 0, 2, 0); labelRangePage.TextAlign = ContentAlignment.MiddleCenter; labelRangePage.BackColor = PageUnCheckBackColor; this.Controls.Add(labelRangePage); Label label_Sure = new Label(); label_Sure.AutoSize = false; label_Sure.Size = new Size(50, 30); label_Sure.Text = "确定"; label_Sure.Margin = new Padding(0, 0, 2, 0); label_Sure.TextAlign = ContentAlignment.MiddleCenter; label_Sure.BackColor = PageUnCheckBackColor; label_Sure.Paint += page_Painit; label_Sure.Click += label_Sure_Click; label_Sure.MouseLeave += label_Sure_MouseLeave_Click; label_Sure.MouseEnter += label_Sure_MouseEnter_Click; this.Controls.Add(label_Sure); Label labelSum = new Label(); labelSum.AutoSize = false; labelSum.Size = new Size(80, 30); labelSum.Text = "共 " + page + " 条"; labelSum.Margin = new Padding(0, 0, 2, 0); labelSum.TextAlign = ContentAlignment.MiddleCenter; labelSum.BackColor = PageUnCheckBackColor; this.Controls.Add(labelSum); DropPage(); } private void DealPage(int SumPage) { this.SumPage = SumPage; page = SumPage % PageSize > 0 ? SumPage / PageSize + 1 : SumPage / PageSize; pages = new List<Label>(); if (page < 7) { for (int i = 1; i <= page; i++) { Label label = new Label(); label.Parent = this; label.AutoSize = false; label.Size = new Size(30, 30); label.Text = i.ToString(); label.Tag = i; label.Margin = new Padding(0, 0, 2, 0); label.TextAlign = ContentAlignment.MiddleCenter; label.BackColor = PageUnCheckBackColor; label.Paint += page_Painit; label.Click += Label_Click; label.MouseLeave += label_MouseLeave_Click; label.MouseEnter += label_MouseEnter_Click; this.Controls.Add(label); pages.Add(label); } pages[0].BackColor = PageCheckBackColor; pages[0].ForeColor = PageCheckFontColor; CurrentPageControl = pages[0]; } else { string[] PageArr = new string[] { "1","...","2","3","4","5","6","...",page.ToString() }; for (int i = 0; i < PageArr.Length; i++) { Label label = new Label(); label.Parent = this; label.AutoSize = false; label.Text = PageArr[i]; if (i == 1) { label.Size = new Size(50, 30); label.Tag = -1; label.BackColor = PageUnCheckBackColor; label.Visible = false; labelLeft = label; } else if (i == 7) { label.BackColor = PageUnCheckBackColor; label.Size = new Size(50, 30); label.Tag = 0; labelRight = label; } else { label.Size = new Size(30, 30); label.Tag = int.Parse(PageArr[i]); label.Click += Label_Click; if (i == 6) label.Visible = false; label.BackColor = PageUnCheckBackColor; label.MouseLeave += label_MouseLeave_Click; label.MouseEnter += label_MouseEnter_Click; if (i > 1 && i < 7) pages.Add(label); if (i == 0) { label.BackColor = PageCheckBackColor; label.ForeColor = PageCheckFontColor; CurrentPageControl = label; label_StartPage = label; } if (i == 8) label_EndPage = label; } label.Margin = new Padding(0, 0, 2, 0); label.TextAlign = ContentAlignment.MiddleCenter; label.Paint += page_Painit; this.Controls.Add(label); } } // if (PageEvent != null) PageEvent.Invoke(CurrentPageControl, new PageEventArgs(1, this.PageSize)); } private void DropPage() { Drop = new Label(); Drop.AutoSize = false; Drop.Size = new Size(60, 30); Drop.Text = this.PageSize.ToString(); Drop.Tag = this.PageSize; Drop.Margin = new Padding(0, 0, 10, 0); Drop.TextAlign = ContentAlignment.MiddleLeft; Drop.ImageAlign = ContentAlignment.MiddleRight; Drop.Image = Properties.Resources.bottom_pull; Drop.BackColor = PageUnCheckBackColor; Drop.Paint += page_Painit; Drop.Click += Drop_Click; Drop.MouseLeave += Drop_MouseLeave_Click; Drop.MouseEnter += Drop_MouseEnter_Click; this.Controls.Add(Drop); flowLayoutPanel=new FlowLayoutPanel(); flowLayoutPanel.Width = 60; int height = 0; for (int i = 0; i < PageArr.Length; i++) { Label label = new Label(); label.Tag = PageArr[i]; label.Text = PageArr[i].ToString() ; label.AutoSize = false; label.Height = 30; label.Width = 60; label.TextAlign = ContentAlignment.MiddleCenter; label.BackColor = PageUnCheckBackColor; label.Click += DropItem_Click; label.MouseEnter += Dropitem_MouseEnterClick; label.MouseLeave += Dropitem_MouseLeaveClick; label.Paint += Drop_Paint; height +=30; flowLayoutPanel.Controls.Add(label); } //如果子项的高大于下拉框的4倍高度,某人为4倍高度 if (height > 30 * 4) { flowLayoutPanel.Height = 30 * 4; } else//如果小于4倍高度,那么就等于累计高度 { flowLayoutPanel.Height = height; } } private void Drop_Paint(object? sender, PaintEventArgs e) { Control control = sender as Control; ControlPaint.DrawBorder(e.Graphics,//获取进行绘制的图形 new Rectangle(0, 0, control.Width, control.Height),//绘制控件所在工作区域 Color.FromArgb(224, 224, 224), 1, ButtonBorderStyle.Solid,//边框类型,选择实线边框,也有虚线等种类;下面这四个重复的传参是设置工作区上下左右边框的功能 Color.FromArgb(224, 224, 224), 1, ButtonBorderStyle.Solid, Color.FromArgb(224, 224, 224), 1, ButtonBorderStyle.Solid, Color.FromArgb(224, 224, 224), 1, ButtonBorderStyle.Solid); } private void Dropitem_MouseLeaveClick(object? sender, EventArgs e) { var label = sender as Label; label.Cursor = Cursors.Hand; label.BackColor = PageUnCheckBackColor; label.ForeColor = PageUnCheckFontColor; } private void Dropitem_MouseEnterClick(object? sender, EventArgs e) { var label = sender as Label; label.BackColor = PageCheckBackColor; label.ForeColor =PageCheckFontColor; } private void DropItem_Click(object? sender, EventArgs e) { var label = sender as Label; this.Controls.Clear(); this.PageSize = (int)label.Tag; this.Drop.Text = label.Tag.ToString(); SetPage(this.SumPage); if (PageEvent != null) PageEvent.Invoke(CurrentPageControl, new PageEventArgs(1, this.PageSize)); pop.Close(); } private void Drop_MouseEnter_Click(object? sender, EventArgs e) { var label = sender as Label; label.BackColor = PageEneterColor; } private void Drop_MouseLeave_Click(object? sender, EventArgs e) { var label = sender as Label; label.Cursor = Cursors.Hand; label.BackColor = PageUnCheckBackColor; label.ForeColor = PageUnCheckFontColor; } private void Drop_Click(object? sender, EventArgs e) { Label label = sender as Label; pop = new Popup(flowLayoutPanel); pop.Show(label); } /// <summary> /// 点击页码或者点击上一页,写一页按钮 /// </summary> /// <param name="currentpage"></param> /// <param name="label"></param> private void PrePage(int currentpage, Label label) { CurrentPageControl.BackColor = PageUnCheckBackColor; CurrentPageControl.ForeColor = PageUnCheckFontColor; if (page < 7) CurrentPageControl = label; else { int count = 0; if (currentpage - 1 < 4) { CurrentPageControl = label; if (currentpage == 4 && pages[0].Visible == true) { for (int i = 2; i < 6; i++) { pages[count].Tag = i; pages[count].Text = i.ToString(); if (i == currentpage) CurrentPageControl = pages[count]; count++; } } if (currentpage <= 3 && labelLeft.Visible == true) { for (int i = 2; i < 6; i++) { pages[count].Tag = i; pages[count].Text = i.ToString(); if (i == currentpage) CurrentPageControl = pages[1]; count++; } } if (labelLeft.Visible == true) labelLeft.Visible = false; if (pages[4].Visible == true) pages[4].Visible = false; if (pages[0].Visible == false) pages[0].Visible = true; if (labelRight.Visible == false) labelRight.Visible = true; } else { if (page - currentpage < 4) { CurrentPageControl = label; if (page - currentpage == 3 && labelRight.Visible == true) { if (pages[4].Visible == false) pages[4].Visible = true; for (int i = currentpage - 2; i < currentpage + 3; i++) { pages[count].Tag = i; pages[count].Text = i.ToString(); if (i == currentpage) CurrentPageControl = pages[count]; count++; } } if (pages[0].Visible == false) pages[0].Visible = true; if (page - currentpage == 3 && labelRight.Visible == false) { for (int i = currentpage - 2; i < currentpage + 3; i++) { pages[count].Tag = i; pages[count].Text = i.ToString(); if (count == 2) CurrentPageControl = pages[2]; count++; } } if (page - currentpage < 3) { if (pages[0].Visible == true) pages[0].Visible = false; if (pages[4].Visible == false) pages[4].Visible = true; for (int i = page - 5; i < page; i++) { pages[count].Tag = i; pages[count].Text = i.ToString(); if (i == currentpage) CurrentPageControl = pages[count]; count++; } } if (labelLeft.Visible == false) labelLeft.Visible = true; if (labelRight.Visible == true) labelRight.Visible = false; } else { if (pages[0].Visible == false) pages[0].Visible = true; if (pages[4].Visible == false) pages[4].Visible = true; if (labelLeft.Visible == false) labelLeft.Visible = true; if (labelRight.Visible == false) labelRight.Visible = true; for (int i = currentpage - 2; i < currentpage + 3; i++) { pages[count].Tag = i; pages[count].Text = i.ToString(); if (i == currentpage) CurrentPageControl = pages[count]; count++; } } } } CurrentPageControl.BackColor = PageCheckBackColor; CurrentPageControl.ForeColor = PageCheckFontColor; if (PageEvent != null) PageEvent.Invoke(currentpage,new PageEventArgs((int)label.Tag,this.PageSize)); } /// <summary> /// 跳转页,点击跳转到某一页 /// </summary> /// <param name="pageCount"></param> private void PageSure(int pageCount) { if (pageCount > 0 && pageCount <= page) { CurrentPageControl.BackColor = PageUnCheckBackColor; CurrentPageControl.ForeColor = PageUnCheckFontColor; if (page < 7) { pages.ForEach(m => { if ((int)m.Tag == pageCount) CurrentPageControl = m; }); } else { int count = 0; if (pageCount < 5) { CurrentPageControl = label_StartPage; for (int i = 2; i < 6; i++) { pages[count].Tag = i; pages[count].Text = i.ToString(); if (i == pageCount) CurrentPageControl = pages[count]; count++; } if (labelLeft.Visible == true) labelLeft.Visible = false; if (labelRight.Visible == false) labelRight.Visible = true; if (pages[0].Visible == false) pages[0].Visible = true; if (pages[4].Visible == true) pages[4].Visible = false; } else { if (labelRight.Visible == true) labelRight.Visible = false; if (pages[0].Visible == true) pages[0].Visible = false; if (pages[4].Visible == false) pages[4].Visible = true; //如果选中的是最后一页 CurrentPageControl = label_EndPage; if (page - pageCount <= 3) { if (page - pageCount == 3) if (pages[0].Visible == false) pages[0].Visible = true; for (int i = page - 5; i < page; i++) { pages[count].Tag = i; pages[count].Text = i.ToString(); if (i == pageCount) CurrentPageControl = pages[count]; count++; } } else { for (int i = pageCount - 2; i < pageCount + 3; i++) { pages[count].Tag = i; pages[count].Text = i.ToString(); if (i == pageCount) CurrentPageControl = pages[count]; count++; } if (labelRight.Visible == false) labelRight.Visible = true; if (pages[0].Visible == false) pages[0].Visible = true; if (pages[4].Visible == false) pages[4].Visible = true; } if (labelLeft.Visible == false) labelLeft.Visible = true; } } } else PageBox.Text = ""; CurrentPageControl.BackColor = PageCheckBackColor; CurrentPageControl.ForeColor = PageCheckFontColor; if (PageEvent != null) PageEvent.Invoke(CurrentPageControl, new PageEventArgs(pageCount, this.PageSize)); } }
cpPageComponent1.SetPage(200);
效果:
.Net Core