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

    }
}
相关推荐
wearegogog1231 分钟前
基于MATLAB的IEEE 9节点系统潮流计算
开发语言·matlab
分布式存储与RustFS10 分钟前
RustFS在AI场景下的实测:从GPU到存储的完整加速方案
开发语言·人工智能·rust·对象存储·企业存储·rustfs·minio国产化替代
揽昕33 分钟前
判断对象是否含有某个属性
开发语言·前端·javascript
phltxy1 小时前
解锁JavaScript WebAPI:从基础到实战,打造交互式网页
开发语言·javascript
资生算法程序员_畅想家_剑魔1 小时前
Java常见技术分享-分布式篇-分布式系统基础理论
java·开发语言·分布式
FL16238631291 小时前
C# winform部署yolo26-obb旋转框检测的onnx模型演示源码+模型+说明
开发语言·c#
大猫和小黄2 小时前
Java异常处理:从基础到SpringBoot实战解析
java·开发语言·spring boot·异常
半夏知半秋2 小时前
kcp学习-通用的kcp lua绑定
服务器·开发语言·笔记·后端·学习
csbysj20202 小时前
并查集路径压缩
开发语言
JavaEdge.2 小时前
java.io.IOException: Previous writer likely failed to write hdfs报错解决方案
java·开发语言·hdfs