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 分钟前
SAP-ABAP:MM 模块供应商主数据开发:字段扩展、分级管控与同步接口开发
开发语言·性能优化·接口·sap·abap·增强·经验交流
小小数媒成员7 分钟前
UGUI 性能优化(详细,全,深入)
开发语言·unity·游戏引擎
大黄说说9 分钟前
告别卡顿:Android UI 性能优化,布局层级过度绘制实战排查
开发语言
千谦阙听13 分钟前
【 C++篇】:模板初阶——泛型编程、函数模板与类模板
开发语言·c++·学习·visual studio
SEO_juper15 分钟前
2026年用Python检测网站薄内容与重复页:用向量相似度找出“搜索引擎眼中一样“的页面(附完整代码)
开发语言·前端·seo·独立站·谷歌优化
老王爱玩车22 分钟前
第6讲:数组和函数实践-----控制台扫雷
c语言·开发语言·学习
m0_7345717640 分钟前
深入理解C++ 多态<三>(Polymorphism)模板方法模式
开发语言·c++
君顾141 分钟前
24小时自助健身房系统开发实战:从0到1完整技术架构指南
java·开发语言·健身房
江屿风1 小时前
【Linux系统】【Linux 进程程序替换机制解析及自定义 Shell 核心逻辑实现 】流食般投喂
linux·运维·服务器·开发语言·笔记
SEO_juper1 小时前
2026年用Python检查多语言站点hreflang:自动揪出“回链缺失/标错语言“等致命错误(附完整代码)
开发语言·前端·seo·独立站·谷歌优化