using JJMediSys.Properties;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace JJMediSys
{
public partial class AlertForm : Form
{
public enum AlertType
{
///
/// 成功
///
Success,
///
/// 提示
///
Info,
///
/// 错误
///
Error,
///
/// 警告
///
Warning
}
public enum ActionType
{
///
/// 等待
///
wait,
///
/// 开启
///
start,
///
/// 关闭
///
close
}
public enum ShowDirection
{
///
/// 头部中心
///
TopCenter,
///
/// 右下角
///
BottomRight,
///
/// 右上角
///
TopRight,
}
///
/// Alert类型
///
private ActionType action;
private int x, y;
///
/// 动画持续的时间
///
private int Duration;
///
/// 弹出的位置
///
private ShowDirection Direction;
///
/// 暴露的方法
///
/// 内容
/// 弹出类型
/// 展示时间 秒
/// 位置
public static void ShowAlert(string msg, AlertType type,
int duration = 3, ShowDirection direction = ShowDirection.TopRight)
{
AlertForm alert = new AlertForm();
if (msg.Length > 30)
{
if (msg.Length > 90)
msg = msg.Substring(0, 90);
alert.Size = new Size(465, 50 * (msg.Length / 15));
alert.lbMsg.Location = new Point(100, 10);
alert.lbMsg.Size = new Size(300, 50 * (msg.Length / 15) - 20);
}
switch (type)
{
case AlertType.Success:
alert.picAlertType.Image = Resources.Success;
alert.BackColor = Color.FromArgb(103, 194, 58);
break;
case AlertType.Info:
alert.picAlertType.Image = Resources.Info;
alert.BackColor = Color.FromArgb(64, 158, 255);
break;
case AlertType.Error:
alert.picAlertType.Image = Resources.Error;
alert.BackColor = Color.FromArgb(245, 108, 108);
break;
case AlertType.Warning:
alert.picAlertType.Image = Resources.Warning;
alert.BackColor = Color.FromArgb(230, 162, 60);
break;
}
alert.Duration = duration * 1000;
alert.Direction = direction;
alert.ShowAlert(msg);
}
public AlertForm()
{
InitializeComponent();
}
///
/// 内部调用显示窗体
///
///
protected void ShowAlert(string msg)
{
this.Opacity = 0.0;
this.StartPosition = FormStartPosition.Manual;
string fname;
for (int i = 1; i < 10; i++)
{
fname = "alert" + i.ToString() + Direction.ToString();
AlertForm frm = (AlertForm)Application.OpenForms[fname];
if (frm == null)
{
this.Name = fname;
// 初始位置
switch (Direction)
{
case ShowDirection.TopCenter:
this.x = (Screen.PrimaryScreen.WorkingArea.Width - this.Width) / 2;
this.y = Screen.PrimaryScreen.WorkingArea.Height / 2 - (this.Height * i + 5 * i);
break;
case ShowDirection.BottomRight:
this.x = Screen.PrimaryScreen.WorkingArea.Width - this.Width + 15;
this.y = Screen.PrimaryScreen.WorkingArea.Height - this.Height * i - 5 * i;
break;
case ShowDirection.TopRight:
this.x = Screen.PrimaryScreen.WorkingArea.Width - this.Width + 15;
this.y = this.Height * i + 5 * i;
break;
}
this.Location = new Point(this.x, this.y);
break;
}
}
// 横向最后的显示位置 形成滑动距离
switch (Direction)
{
case ShowDirection.BottomRight:
case ShowDirection.TopRight:
this.x = Screen.PrimaryScreen.WorkingArea.Width - base.Width - 5;
break;
}
this.lbMsg.Text = msg;
//字体大小自适应
LabelAutoSize();
this.Show();
this.action = ActionType.start;
this.hideTimer.Interval = 1;
this.hideTimer.Start();
}
///
/// msg文字大小自适应
///
private void LabelAutoSize()
{
Font font;
while (true)
{
var lbFont = this.lbMsg.Font;
if (lbMsg.Right >= this.btnPicClose.Left)
{
font = new Font(lbFont.Name, lbFont.Size - 1, lbFont.Style);
this.lbMsg.Font = font;
}
else
{
var top = (this.Height - this.lbMsg.Height) / 2;
lbMsg.Top = top;
break;
}
}
}
private void btnPicClose_Click_1(object sender, EventArgs e)
{
this.hideTimer.Interval = 1;
this.action = ActionType.close;
}
private void btnPicClose_Click(object sender, EventArgs e)
{
//this.hideTimer.Interval = 1;
// this.action = ActionType.close;
this.Close();
}
private void AlertForm_Load(object sender, EventArgs e)
{
}
private void lbMsg_Click(object sender, EventArgs e)
{
}
private void hideTimer_Tick(object sender, EventArgs e)
{
switch (this.action)
{
case ActionType.wait:
this.hideTimer.Interval = Duration;
this.action = ActionType.close;
break;
case ActionType.start:
this.hideTimer.Interval = 1;
this.Opacity += 0.1;
switch (Direction)
{
case ShowDirection.TopCenter:
if (this.Opacity.Equals(1.0))
{
this.action = ActionType.wait;
}
break;
case ShowDirection.BottomRight:
case ShowDirection.TopRight:
if (this.x < this.Location.X)
{
this.Left--;
}
else
{
if (this.Opacity.Equals(1.0))
{
this.action = ActionType.wait;
}
}
break;
}
break;
case ActionType.close:
this.hideTimer.Interval = 1;
this.Opacity -= 0.1;
if (Direction == ShowDirection.TopCenter)
this.Top -= 10;
else
this.Left -= 3;
if (base.Opacity == 0.0)
base.Close();
break;
}
}
}
}