首页 > 编程语言 >C# Winfom实现Toast功能

C# Winfom实现Toast功能

时间:2023-05-05 13:55:05浏览次数:42  
标签:Toast toast C# text toastInfo int Owner public Winfom

    public class Toast
    {
        public class ToastInfo
        {
            public Form Owner { get; set; }
            public string Text { get; set; } = "";
            public bool Increase { get; set; } = true;
            public int StartTop { get; set; } = 20;
            public int Timeout { get; set; } = 2000;
            public Color BackColor { get; set; } = default;
            public Color TextColor { get; set; } = default;
        }
        [DllImport("Gdi32.dll", EntryPoint = "CreateRoundRectRgn")]
        public static extern IntPtr CreateRoundRectRgn
        (
            int nLeftRect, // x-coordinate of upper-left corner
            int nTopRect, // y-coordinate of upper-left corner
            int nRightRect, // x-coordinate of lower-right corner
            int nBottomRect, // y-coordinate of lower-right corner
            int nWidthEllipse, // width of ellipse
            int nHeightEllipse // height of ellipse
        );
        static object LockObject = new object();

        public static void Show(ToastInfo toastInfo)
        {
            if (toastInfo.Owner == null)
                return;

            var toasts = toastInfo.Owner.Controls.Cast<Control>().Where(v => v?.GetType() == typeof(ToastControl)).ToList();
            if (!toastInfo.Increase)
            {
                if (toasts.Count > 0)
                {
                    var firstToast = toasts[0] as ToastControl;
                    firstToast.timer1.Stop();
                    firstToast.ToastText = toastInfo.Text;
                    firstToast.timer1.Start();
                    return;
                }
            }
            var toast = new ToastControl();

            toast.Name = $"toast{toasts.Count}";
            toast.ToastText = toastInfo.Text;

            toast.Top =  (toasts.Count * (toast.Height + 5)) + toastInfo.StartTop;
            toast.Region = System.Drawing.Region.FromHrgn(CreateRoundRectRgn(0, 0, toast.Width, toast.Height, 5, 5));
            if (toastInfo.BackColor != default)
            {
                toast.BackColor = toastInfo.BackColor;
            }
            if (toastInfo.TextColor != default)
            {
                toast.lblMsg.ForeColor = toastInfo.TextColor;
            }
            toastInfo.Owner.Controls.Add(toast);

            toast.timer1.Interval = toastInfo.Timeout;
            toast.timer1.Tag = toast;
            toast.timer1.Tick += new EventHandler((s, e) =>
            {
                lock (LockObject)
                {
                    var timer = (s as Timer);
                    timer?.Stop();
                    var controlToast = timer.Tag as ToastControl;
                    toastInfo.Owner?.Controls.Remove(controlToast);
                    controlToast.Dispose();
                    var otherToasts = toastInfo.Owner.Controls.Cast<Control>().Where(v => v?.GetType() == typeof(ToastControl)).ToList();
                    for (int i = 0; i < otherToasts.Count; i++)
                    {
                        var toast2 = otherToasts[i];
                        toast2.Top -= (toast2.Height + 5);
                    }
                }
            });
            toast.timer1.Start();

            toast.Left = (int)(toastInfo.Owner.Width / 2 - toast.Width / 2);
            toast.Visible = true;
            toast.BringToFront();
        }


        public static void Info(string text, int timeOut = 2000, bool increase = true, int startTop = 20)
        {
            Show(new ToastInfo { Owner = Common.FindOwner(), Increase = increase, StartTop = startTop, Timeout = timeOut, Text = text, BackColor = ColorTranslator.FromHtml("#3C486B"), TextColor = Color.White });
        }
        public static void Error(string text = "發生錯誤!", int timeOut = 2000, bool increase = true, int startTop = 20)
        {
            Logs.Write(text);
            Show(new ToastInfo { Owner = Common.FindOwner(), Increase = increase, StartTop = startTop, Timeout = timeOut, Text = text, BackColor = ColorTranslator.FromHtml("#FF6D60"), TextColor = Color.White });
        }
        public static void Success(string text= "操作成功!", int timeOut = 2000, bool increase = true, int startTop = 20)
        {
            Show(new ToastInfo { Owner = Common.FindOwner(), Increase = increase, StartTop = startTop, Timeout = timeOut, Text = text, BackColor = ColorTranslator.FromHtml("#68B984"), TextColor = Color.White });
        }

    }

  使用方式:

  Toast.Error("一个错误的Toast!");

标签:Toast,toast,C#,text,toastInfo,int,Owner,public,Winfom
From: https://www.cnblogs.com/Adin/p/17373909.html

相关文章

  • mysql_clone
    HowtoInstall安装UsingtheINSTALLPLUGINstatement:Unix/Linux:mysql>INSTALLPLUGINcloneSONAME'mysql_clone.so';Windows:mysql>INSTALLPLUGINcloneSONAME'mysql_clone.dll';Usingtheplugin-loadoptionintheMySQLco......
  • 转载:PageOffice在线打开office文件添加盖章没反应
    1、在控制面板的程序功能里面卸载印章客户端,然后重新打开文件,根据提示安装印章客户端sealsetup.exe,重新盖章试试。(**注意:**安装卸载的时候,先关闭所有的浏览器和所有的office,关闭以后再打开任务管理器,点击详信息,看看有没有WINWORD.EXE和EXCEL.EXE,有的话结束了。)如果还不能解......
  • CrackRTF 1
    查壳先运行看看是个啥:要我们输入密码(1),估计还有密码(2),随便输了就直接给你退出,先不管密码(2)是否存在遇到了再说。32位,进IDA找主函数:int__cdeclmain_0(intargc,constchar**argv,constchar**envp){DWORDv3;//eaxDWORDv4;//eaxcharStr[260];//[esp+4Ch......
  • AWS Serverless Lambda Function架构设计的几种方式
    如何构建大型Serverless应用从路由方式上,Lambda大致可以分为三种架构方式1:单体应用式这种方式和传统RESTAPI很相似,以nodejs为例,在service内部使用express框架作路由,如下图所示代码示例如下constexpress=require('express')constapp=express()app.listen(80,()=>......
  • ETF2100/5900 Introductory Econometrics
    ETF2100/5900IntroductoryEconometricsAssignment2—ACaseStudyontheBirth-weightofBabyImportantnotes:1.Thisisanindividualassignment.Thisassignmentisworth20%ofthisunit’stotalmark.Markswillbedeductedforlatesubmissiononthefoll......
  • COMP282面向C#编程UGI
    COMP282–AdvancedObject-OrientedCLanguagesCoursework2–C#Deadline:Monday15thofMayat17:00Lastpossibletimetohand-in:Monday29thofMayat17:00Weighting:50%Feedback:Generalfeedbackwillbereleasedshortlyafterthelastpossibletimeyo......
  • stm32 cubeide ST7920 12864点阵屏 U8G2移植
    准备工作【通用-移植u8g2准备工作】在cubeide中移植u8g2到STM32的准备工作源码获取和文件处理-不打鱼光晒网-博客园(cnblogs.com) 7920很老了,spi只能接受2.5M的时钟,实际上2M就大概率花屏了,使用硬件spi的话,由于分频系数选择的问题,配置为1M就行了,不然花屏7920的穿行模式仅......
  • Linux Centos7内核升级
    LinuxCentos7内核升级现在主流的centos应该都是centos7了,从centos7.2开始,内核版本为3.10,越往后内核版本越高。高版本的内核修复了许多的低版本内核的bug,因此,系统是需要提高内核版本的,从而提高安全性,稳定性,并增加更多的功能。Linux是支持多版本内核共存的,无非是系统启动的时候应......
  • 注册表RootKey简写:HKCR, HKCU, HKLM, HKU, and HKCC
    WhatDoHKCR,HKCU,HKLM,HKU,andHKCCMean?(RegistryRootKeys)By Rich Note: TogetabetterunderstandingofWindowsRegistrybasics,read thisguide.Ifyou’resomewhatfamiliarwiththeWindowsRegistry,you’venodoubtseenreferencestoHKCR,HKCU......
  • 在 linux-4.9/drivers/usb/serial/ch341.c 上串口收发数据异常
    有天做USBhost串口驱动的时候发下FT\CP都没有问题,就CH341有问题,读写正常,但数据不正常。有一点稀奇,可能是ch中间有版本变更了吧。解法就更新到https://github.com/torvalds/linux/blob/master/drivers/usb/serial/ch341.c最新的代码就行,有一些函数有出入,但大体影响......