• winform 自定义 标签


    winform 标签

    看一下效果
    在这里插入图片描述
    主要原因是winform 没有这样的标签控件,想要稍稍美化下程序,别的Express这些又要付费,用到的地方,主要是查询标签,显示类型等等。一通折腾自己搞一个。

    首先分析一下

    标签特点和组成:
    1,带弧度
    2,一个内容项
    3,一个小叉叉 X

    基本控件用啥,我用的是lable。
    当然要是用Button 别的什么也可以,条条大路通罗马嘛。

    那就是俩个带弧度的lable组合一下,搞成一个控件,一个显示内容,一个关闭事件。
    在这里插入图片描述

    带弧度的lable

    新增一个组件(就这玩意)
    在这里插入图片描述
    继承Lable
    看代码:

     public partial class RadiusLable : System.Windows.Forms.Label
     {
     	public RadiusLable()
            {
                InitializeComponent();
           }
            protected override void OnPaint(PaintEventArgs e)
            {
                e.Graphics.SmoothingMode = SmoothingMode.AntiAlias;
                AddArcForm(this);
                base.OnPaint(e);
            }
            private void AddArcForm(System.Windows.Forms.Control form)
            {
                int basicWidth = 10;
                GraphicsPath p = new GraphicsPath();
                p.StartFigure();
                p.AddArc(new Rectangle(0, 0, basicWidth , basicWidth ), 180, 90);
                p.AddArc(new Rectangle(form.Width - basicWidth , 0, basicWidth , basicWidth ), 270, 90);
                p.AddArc(new Rectangle(form.Width - basicWidth , form.Height - basicWidth , basicWidth , basicWidth ), 0, 90);
                p.AddArc(new Rectangle(0, form.Height - basicWidth , basicWidth , basicWidth ), 90, 90);
                p.CloseFigure();
                form.Region = new Region(p);
            }
    }
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24

    主要就是画那个弧度。别的功能根据需求重写。

    组合控件

    新建一个控件(就这玩意)
    在这里插入图片描述
    首先设计图,用到的是 一个panel 两个 字节重写的带弧度lable,按自己需求控制其大小,字体,颜色等。这儿就是弄一个简单的。
    在这里插入图片描述
    在这里插入图片描述
    就搞这么大,根据需求调整。
    lable 大小由自定义和自适用两种AutoSize 来控制,自使用太丑,所以就自定义了。给内容标签页赋值时自定义其大小,记得同时设置控件整体大小和panel大小。
    再加一个关闭x的点击事件。搞一个对外事件,就完事了。
    看代码

    public partial class TagControl : UserControl
    {
    	public TagControl()
            {
                InitializeComponent();
            }
             [Description("关闭事件"), Category("自定义")]
            public event EventHandler CloseClick;
            public void SetText(string text)
            {
                this.Content.Text = text;
                if (text.Length > 2)
                {
                    this.panel1.Width = 81 + (text.Length - 2) * 10;
                    this.Width = 90 + (text.Length - 2) * 10;
                    this.Content.Width = 76 + (text.Length - 2) * 10;            }
            }
            private void close_Click(object sender, EventArgs e)
            {
                CloseClick.Invoke(this, e);
            }
    }
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21

    最终生成一下,其他界面调用就行了。
    其中setText里设置的尺寸根据自己需求来。
    代码很简短,一目了然。

  • 相关阅读:
    java毕业设计开题报告论文基于JavaWeb项目实现的高校学生在线选课系统
    80-基于stm32单片机的图书馆噪音检测量分贝仪(源码+原理图)
    自动化测试测试框架封装改造
    布局与打包
    UE4创建一个左右摇摆的“喷泉”
    【C++初阶】小白入门C++
    Spring核心问题回顾3:spring的事务传播机制、事务失效的情况、对ioc的理解
    cobol结构
    技术分享 | 测试开发工程师必读经典好书清单
    尝试FreeBSD下安装ollama
  • 原文地址:https://blog.csdn.net/weixin_40899924/article/details/127689788