一 窗口背景
实现方法:
① 自己绘制一个圆角矩形背景;
② 将外围区域透明化:
this.BackColor=Color.White;
this.TransparencyKey=Color.White;
二 窗口自动关闭
实现:
① 显示窗口时,同时开启定时器;
② 约1.5秒,关闭窗口;
③ 同时,在窗口显示的最后0.5秒控制窗口谈出;
子窗体代码:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Drawing.Text;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace 窗口自动关闭
{
public partial class Form2 : Form
{
private string message;
//定时器:自动关闭窗口
private System.Windows.Forms.Timer timer;
private int count;//计数,窗口弹出计算
public Form2()
{
this.FormBorderStyle = FormBorderStyle.None;
this.BackColor = Color.White;
this.TransparencyKey = Color.White;//指定透明区域的颜色
this.ShowInTaskbar = false;
}
//owner可以是Form窗体也可以是控件
public void ShowMessage(Control owner, string message)
{
this.message = message;
this.StartPosition = FormStartPosition.Manual;
this.Size = MeasureSize(message);
//找到owner所在的顶级窗口
Form form = owner.FindForm();
this.Owner = form;
//form2窗口相对主窗口居中
Rectangle fr = new Rectangle(form.Location, form.Size);
int x = fr.X + (fr.Width - this.Width) / 2;
int y = fr.Y + (fr.Height - this.Height) / 2;
this.Location = new Point(x, y);
this.Show();
//启动定时器
if (timer == null)
{
timer = new System.Windows.Forms.Timer();
timer.Interval = 200;
timer.Tick += this.OnTimer;
this.count = 15;
timer.Start();
}
}
private void OnTimer(object sender, EventArgs e)
{
count -= 2;
if (count <= 0)
{
//计时结束,关闭窗口
timer.Dispose();
this.Dispose();
return;
}
//Opacity 窗口整体的透明度
this.Opacity = count / 5.0;
}
protected override bool ShowWithoutActivation
{
get { return true; }
}
public Size MeasureSize(string str)
{
StringFormat sf = new StringFormat();
sf.Alignment = StringAlignment.Center;
sf.LineAlignment = StringAlignment.Center;
//按最大宽度300进行测试,计算文本显示所需的尺寸
Graphics g = this.CreateGraphics();
SizeF size = g.MeasureString(str, this.Font, 300);
g.Dispose();
//最小宽度120,最小高度50
if (size.Width < 100) size.Width = 100;
if (size.Height < 50) size.Height = 50;
//适当放大一点,留出padding空间
return new Size((int)size.Width + 10, (int)size.Height + 4);
}
protected override void OnPaint(PaintEventArgs e)
{
base.OnPaint(e);
Graphics g = e.Graphics;
int w = this.Width, h = this.Height;
Rectangle rect = new Rectangle(0, 0, w, h);
rect.Inflate(-4, -4);
//平滑绘制,反锯齿
g.SmoothingMode = SmoothingMode.HighQuality;
g.TextRenderingHint = TextRenderingHint.AntiAliasGridFit;
Color bgColor = Color.Orange;
Color textColor = Color.FromArgb(0x30, 0x30, 0x30);
using (Brush brush = new SolidBrush(bgColor))
{
GraphicsPath path = RoundRectangle(rect, 10);
g.FillPath(brush, path);
}
if (message != null)
{
StringFormat sf = new StringFormat();
sf.Alignment = StringAlignment.Center;
sf.LineAlignment = StringAlignment.Center;
Brush brush = new SolidBrush(Color.Black);
g.DrawString(message, this.Font, brush, rect, sf);
brush.Dispose();
}
}
public static GraphicsPath RoundRectangle(Rectangle rect, int r)
{
int x = rect.X, y = rect.Y;
int w = rect.Width;
int h = rect.Height;
if (r > w / 2) r = w / 2;
if (r > h / 2) r = h / 2;
GraphicsPath path = new GraphicsPath();
path.AddArc(new Rectangle(x, y, r, r), 180, 90);
path.AddArc(new Rectangle(x + w - r, y, r, r), 270, 90);
path.AddArc(new Rectangle(x + w - r, y + h - r, r, r), 0, 90);
path.AddArc(new Rectangle(x, y + h - r, r, r), 90, 90);
path.CloseFigure();
return path;
}
}
}
主窗体
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 窗口自动关闭
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
Form2 form2 = new Form2();
form2.ShowMessage(button1, "中国人民解放军");
}
}
}
三 AfToast
Af.Winform.AfToast短消息提示
短消息提示是一个常用功能,可以直接加到项目中使用。
工具方法:以static形式封装一些快捷方法。
AfToast.show(owner,message)
AfToast.show(owner,bgColor,message)
子窗体
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Drawing.Text;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace 短消息提示
{
public partial class Af : Form
{
private string message;
//定时器:自动关闭窗口
private System.Windows.Forms.Timer timer;
private int count;//计数,窗口谈出计算
Color bgColor = Color.FromArgb(0xFF, 0xFF, 0XE0);
Color textColor = Color.FromArgb(0x30, 0x30, 0x30);
public Af()
{
this.FormBorderStyle = FormBorderStyle.None;
this.BackColor = Color.White;
this.TransparencyKey = Color.White;
this.ShowInTaskbar = false;
}
public void ShowMessage(Control owner,string message)
{
this.message = message;
this.StartPosition = FormStartPosition.Manual;
this.Size = MeasureSize(message);
//找到owner所在的顶级窗口
Form form = owner.FindForm();
this.Owner = form;
//使Toast窗口相对主窗口居中
Rectangle fr = new Rectangle(form.Location, form.Size);
int x = fr.X + (fr.Width - this.Width) / 2;
int y = fr.Y + (fr.Height - this.Height) / 2;
this.Location = new Point(x, y);
this.Show();
//启动定时器
if(timer==null)
{
timer = new System.Windows.Forms.Timer();
timer.Interval = 200;
timer.Tick += this.OnTimer;
this.count = 15;
timer.Start();
}
}
//定时器控制窗口的自动关闭
private void OnTimer(object sender ,EventArgs e)
{
count -= 2;
if(count<=0)
{
//计时结束,关闭窗口
timer.Dispose();
this.Dispose();
return;
}
//Opacity窗口整体的透明度
this.Opacity = count / 5 ;
}
//焦点控制:本窗口不剥夺主窗口的焦点
//否则,当Toast窗口激活时,主窗口焦点被剥夺
protected override bool ShowWithoutActivation
{
get { return true; }
}
public Size MeasureSize(string str)
{
StringFormat sf = new StringFormat();
sf.Alignment = StringAlignment.Center;
sf.LineAlignment = StringAlignment.Center;
//按最大宽度300进行测算,计算文本显示所需的尺寸
Graphics g = this.CreateGraphics();
SizeF size = g.MeasureString(str, this.Font, 300);
g.Dispose();
//最小宽度120,最小高度50
if (size.Width < 100) size.Width = 100;
if (size.Height < 50) size.Height = 50;
return new Size((int)size.Width + 10, (int)size.Height + 4);
}
protected override void OnPaint(PaintEventArgs e)
{
base.OnPaint(e);
Graphics g = e.Graphics;
int w = this.Width, h = this.Height;
Rectangle rect = new Rectangle(0, 0, w, h);
rect.Inflate(-4, -4);
//平滑绘制,反锯齿
g.SmoothingMode = SmoothingMode.HighQuality;
g.TextRenderingHint = TextRenderingHint.AntiAliasGridFit;
using(Brush brush=new SolidBrush(bgColor))
{
GraphicsPath path = RoundRectangle(rect, 10);
g.FillPath(brush, path);
}
if(message!=null)
{
StringFormat sf = new StringFormat();
sf.Alignment = StringAlignment.Center;
sf.LineAlignment = StringAlignment.Center;
Brush brush = new SolidBrush(textColor);
g.DrawString(message, this.Font, brush, rect, sf);
brush.Dispose();
}
}
public static GraphicsPath RoundRectangle(Rectangle rect,int r)
{
int x = rect.X, y = rect.Y;
int w = rect.Width;
int h = rect.Height;
if (r > w / 2) r = w / 2;
if (r > h / 2) r = h / 2;
GraphicsPath path = new GraphicsPath();
path.AddArc(new Rectangle(x, y, r, r), 180, 90);
path.AddArc(new Rectangle(x + w - r, y, r, r), 270, 90);
path.AddArc(new Rectangle(x + w - r, y + h - r, r, r), 0, 90);
path.AddArc(new Rectangle(x, y + h - r, r, r), 90, 90);
path.CloseFigure();
return path;
}
//添加一些更容易使用的工具方法
public static void Show(Control owner ,string message)
{
Af t = new Af();
t.ShowMessage(owner, message);
}
public static void Show(Control owner,Color bgColor,string message)
{
Af t = new Af();
t.bgColor = bgColor;
t.ShowMessage(owner, message);
}
}
}
父窗体
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 短消息提示
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
Af.Show(this, "中国人民解放军");
}
}
}