c# winform 类似android toast消息功能

先看下效果:

支持动画,支持声音,支持定时自动关闭

使用方法:

var notifycation = new Notification(“My Notification”, "My notification message goes here", 2, "Slide", "Right");

notifycation.show();

用起来就这么简单,第一个参数是title,第二个是内容,背景红色是可以换的,第三个参数是自动关闭时间,第四个是窗口动作,淡入淡出,或者滑翔,第五个参数是从左边还是右边,上边还是下边出来

支持多个toast,消失之前会叠加不会盖住,

核心源码:

if (duration < 0)
                duration = int.MaxValue;
            else
                duration = duration * 1000;

            lifeTimer.Interval = duration;
            labelTitle.Text = title;
            labelBody.Text = body;

            _animator = new FormAnimator(this, animation, direction, 500);

            Region = Region.FromHrgn(NativeMethods.CreateRoundRectRgn(0, 0, Width - 5, Height - 5, 20, 20));

  

[DllImport("user32")]
        internal extern static bool AnimateWindow(IntPtr hWnd, int dwTime, int dwFlags);

        [DllImport("Gdi32.dll", EntryPoint = "CreateRoundRectRgn")]
        internal 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
        );

  

源码下载

原文地址:https://www.cnblogs.com/y114113/p/6971937.html