using System;
using System.Configuration;
using System.Drawing;
using System.Windows.Forms;
namespace Gs.DevApp
{
public partial class Toast : DevExpress.XtraEditors.XtraForm
{
private System.Windows.Forms.Timer tmr;
private System.Windows.Forms.Timer tmr2;
///
///
///
/// 1成功,-1失败
///
/// 动画延长时间
public Toast(int it, string messageText, int vanishingSeconds = 5)
{
InitializeComponent();
this.StartPosition = FormStartPosition.CenterScreen; // 设置窗体居中显示
this.AutoScaleMode = AutoScaleMode.Font; // 根据系统字体大小
int clientWidth=0;
int clientHeight = 0;
this.lbMsg.BackColor = System.Drawing.Color.Firebrick;
this.lbMsg.Font = new System.Drawing.Font("Tahoma", 38F);
this.lbMsg.ForeColor = System.Drawing.Color.White;
messageText = messageText.Replace("@n", "\r\n");
using (Graphics graphics = this.CreateGraphics())
{
Size textSize = TextRenderer.MeasureText(messageText, lbMsg.Font);
clientWidth = (int)textSize.Width + 10;
clientHeight= (int)textSize.Height+10;
}
this.ClientSize = new System.Drawing.Size(clientWidth, clientHeight);
this.StartPosition = FormStartPosition.CenterScreen; // 设置窗体居中显示
this.AutoScaleMode = AutoScaleMode.Font; // 根据系统字体大小
this.CenterToScreen();
try
{
string _vanishingSeconds = ConfigurationManager.AppSettings["ToastSeconds"];
if (!string.IsNullOrEmpty(_vanishingSeconds))
vanishingSeconds = int.Parse(_vanishingSeconds);
}
catch (Exception)
{
}
System.ComponentModel.ComponentResourceManager resources = new System.ComponentModel.ComponentResourceManager(typeof(Toast));
if (it > 0)
{
this.lbMsg.BackColor = System.Drawing.Color.SeaGreen;
}
else
{
this.lbMsg.BackColor = System.Drawing.Color.Firebrick;
}
double elapsedTime = 0;
lbMsg.Text = messageText;
double vanishingMilliSeconds = vanishingSeconds * 1000;
tmr = new System.Windows.Forms.Timer();
tmr.Tick += delegate
{
tmr.Stop();
tmr2.Stop();
tmr.Dispose();
tmr2.Dispose();
this.Close();
};
tmr.Interval = (int)TimeSpan.FromSeconds(vanishingSeconds).TotalMilliseconds;
tmr.Start();
//second timer
tmr2 = new System.Windows.Forms.Timer();
tmr2.Tick += delegate
{
elapsedTime += 50;
if (elapsedTime >= (vanishingMilliSeconds * 65) / 100)
this.Opacity -= 0.05f;
};
tmr2.Interval = (int)TimeSpan.FromMilliseconds(50).TotalMilliseconds;
tmr2.Start();
}
}
}