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();
            }
        }

    }
}
相关推荐
The Chosen One9853 分钟前
【Linux】深入理解Linux进程(二):进程的状态
linux·运维·服务器·开发语言·git
嵌入式小杰19 分钟前
一阶低通滤波入门教程:从原理到单片机 C 代码实现
c语言·开发语言·stm32·单片机·算法
叼烟扛炮28 分钟前
C++ 知识点02 输入输出
开发语言·c++·算法·输入输出
qcx2329 分钟前
深度解析Deepseek V4:1M 上下文不是军备竞赛,是养 Agent 的人才知道的痛
java·开发语言
小此方33 分钟前
Re:思考·重建·记录 现代C++ C++11篇(六) 从 shared_ptr 到 weak_ptr:起底智能指针的引用计数与循环引用之痛
开发语言·c++·c++11·现代c++
晨非辰35 分钟前
吃透C++两大默认成员函数:const成员函数、 & 取地址运算符重载
java·大数据·开发语言·c++·人工智能·后端·面试
我滴老baby38 分钟前
多智能体协作系统设计当AI学会团队合作效率翻十倍
android·开发语言·人工智能
落雪寒窗-39 分钟前
Python进阶核心路线(工程向)
开发语言·python
humcomm40 分钟前
全栈开发技术栈的最新进展(2026年视角)
开发语言·架构
聆风吟º1 小时前
【C标准库】深入理解C语言pow函数:从入门到精通,一文搞定幂运算
c语言·开发语言·库函数·pow·幂运算