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

    }
}
相关推荐
玩泥巴的1 小时前
MAF + 飞书:让大模型干活,但别让它乱干
ai·c#·.net·飞书·agent
WJ.Polar4 小时前
Java封装、继承、多态
java·开发语言
摇滚侠4 小时前
Java 中如何表示负数,有符号和无符号的区别,补码的计算规则,char byte short int long 的范围
java·开发语言
weixin_307779134 小时前
C++代码实现MATLAB中的fitrtree函数功能
开发语言·c++·算法·matlab
爱读书的小胖4 小时前
Codewars:Rot13(C)解题分析
c语言·开发语言
鸽鸽程序猿4 小时前
【项目】【在线判题系统】b端题目增删改
开发语言·python
Lhappy嘻嘻5 小时前
读懂 JVM:内存模型、类加载机制、分代回收
java·开发语言·jvm
cpp_w6 小时前
C#基础3- 面向对象中级应用3
后端·c#
weixin_307779136 小时前
C++代码实现MATLAB中的spectralcluster函数功能
开发语言·c++·算法·matlab
鸽鸽程序猿6 小时前
【项目】【在线判题系统】c端:竞赛报名
c语言·开发语言