C#Winform自定义信息提示框控件

1、创建信息提示框控件

在控件库添加用户控件,命名为InfoTip;

在属性/布局栏将Size设置为148,128。

2、修改InfoTip.cs

csharp 复制代码
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Timers;
using System.Windows.Forms;

namespace UserControlLib
{
    public partial class InfoTip : UserControl
    {
        private string[] segment;
        private System.Timers.Timer CommandTimer = new System.Timers.Timer();
        int counter = 3;

        public InfoTip()
        {
            InitializeComponent();
            CommandTimer.Interval += 1000;
            CommandTimer.Elapsed += CommandTimer_Elapsed;
            CommandTimer.Start();
        }

        private void CommandTimer_Elapsed(object sender, ElapsedEventArgs e)
        {
            if (this.InvokeRequired)
            {
                MethodInvoker mi = delegate ()
                {
                    if (this.Visible)
                    {
                        if (counter != 0)
                        {
                            counter--;
                        }
                        if (counter == 0)
                        {
                            this.Visible = false;
                            counter = 3;
                        }
                    }
                };
                this.Invoke(mi);
            }
            else
            {
                if (this.Visible)
                {
                    if (counter != 0)
                    {
                        counter--;
                    }
                    if (counter == 0)
                    {
                        this.Visible = false;
                    }
                }
            }
        }

        protected override void OnPaint(PaintEventArgs e)
        {
            base.OnPaint(e);
            var g = e.Graphics;
            g.SmoothingMode = SmoothingMode.AntiAlias;
            for (int i = 0; i < segment.Length; i++)
            {
                g.DrawString(segment[i], new Font("Yahei", 9f), new SolidBrush(Color.Black), 5, 15 * i + 5);
            }
        }

        private string content;
        [Description("闩锁状态"), Category("自定义")]
        public string Content
        {
            get { return content; }
            set
            {
                content = value;
                string[] arr = content.Split(',');
                segment = arr;
                Invalidate();
            }
        }

    }
}
相关推荐
学步_技术4 分钟前
Python编码系列—Python抽象工厂模式:构建复杂对象家族的蓝图
开发语言·python·抽象工厂模式
IT规划师14 分钟前
C#|.net core 基础 - 扩展数组添加删除性能最好的方法
c#·.netcore·数组
wn53128 分钟前
【Go - 类型断言】
服务器·开发语言·后端·golang
Hello-Mr.Wang40 分钟前
vue3中开发引导页的方法
开发语言·前端·javascript
救救孩子把43 分钟前
Java基础之IO流
java·开发语言
WG_1744 分钟前
C++多态
开发语言·c++·面试
时光追逐者1 小时前
分享6个.NET开源的AI和LLM相关项目框架
人工智能·microsoft·ai·c#·.net·.netcore
宇卿.1 小时前
Java键盘输入语句
java·开发语言
Amo Xiang1 小时前
2024 Python3.10 系统入门+进阶(十五):文件及目录操作
开发语言·python
friklogff1 小时前
【C#生态园】提升C#开发效率:深入了解自然语言处理库与工具
开发语言·c#·区块链