一 窗口背景

实现方法:
① 自己绘制一个圆角矩形背景;
② 将外围区域透明化:

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,最小高度50if (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,最小高度50if (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, "中国人民解放军");}}
}

C# 窗口背景 短消息提示相关推荐

  1. window.createPopup()用法以及短消息提示框代码

    一.在做一个portal项目时,用户要求在门户首页上的待办信息要有明确的提示,且在浏览器最小化的情况下,当有新的待办信息时,也要做提示.用了alert方法.或者用div的方法都很难实现"在浏 ...

  2. 虎翼主机 mysql_Discuz!发论坛短消息提示mysql数据库错误

    pop:之前在其他的主机转到虎翼门户通的空间,只记得修改主论坛数据库的配置,忘记了这个ucenter的配置了. 症状: 登陆.发帖等均正常的,但是不能发论坛短消息,提示如下错误信息 Can not c ...

  3. MSN消息提示类(II)

    纯js编写 跨框架 无图片 支持调速度 任意位置弹出 需要ie5.5以上 <HTML><HEAD>  <SCRIPT language=JavaScript>   ...

  4. asp.net 页面右下角弹出类似QQ或MSN的消息提示

    <HTML><HEAD> <TITLE>网页特效观止|JsCode.CN|---页面右下角弹出类似QQ或MSN的消息提示</TITLE> <SCR ...

  5. MSN样式的消息提示

    纯JavaScript编写 跨框架 无图片 支持调速度 任意位置弹出 需要ie5.5以上 < HTML >< HEAD >   < SCRIPT language = J ...

  6. flutter Toast消息提示框

    题记 -- 执剑天涯,从你的点滴积累开始,所及之处,必精益求精,即是折腾每一天. 本文章将讲述: 1.在 flutter 跨平台开发中,使用 Dart 实现 Toast 消息提示框效果 2.Overl ...

  7. android 消息提示机制

    消息提示机制 一 对话框 Dialog AlertDialog 对话框有:标准对话框, 菜单式对话框, 菜单式复选框对话框,自定义对话框 (注意:这里的上下文公用了,代码的点击方法我已经在点击监听事件 ...

  8. C#MessageBox(消息提示框)使用详解

    摘要:以自身经历,自己开发的MessageBox方法测试器详解MessageBox消息提示框的各种不同样式和用法. 编程语言:C# 编程环境:Visual Studio 2019 目录 Message ...

  9. 手机测试用例-短消息测试用例

    ID 功能描述 操作步骤 预期结果 test time P/F comment tester test time P/F comment tester 1 短信息 1.1创建.编辑短消息并发送 书写短 ...

最新文章

  1. 8、设计模式-结构型模式-适配器模式
  2. 防患高通效仿华为,苹果10亿美元收购英特尔手机基带业务!打造5G备胎,加强自主可控...
  3. webstorm2018破解方法
  4. 【专题介绍】视频内容生产与消费创新(Part1)
  5. 基于React的表单开发的分析(上)
  6. 重读经典:《ImageNet Classification with Deep Convolutional Neural Networks》
  7. php怎么给span赋值,php给一组指定关键词添加span标签的方法
  8. HTML5基本知识小测验
  9. 【linux高级程序设计】(第九章)进程间通信-管道 3
  10. lodopa5预览时默认横向_微软Windows 10 20H1预览版18922开始推送,附更新内容
  11. robotframework-接口测试详解(上传文件)
  12. java servlet容器有哪些_servlet容器是什么
  13. 求职面试-HR会问你什么问题?
  14. 南方电网两栖机器人_南方电网首个作业级水下机器人落户海南 为海底电缆“护驾”...
  15. speedoffice(Excel)表格怎么一次插入多行?
  16. Windows 10 安装使用TensorFlow-GPU
  17. Nginx反向代理服务器解决负责均衡问题
  18. ifft 快速傅里叶逆变换(Matlab)
  19. Windows获取模块基地址
  20. OpenGL画自行车+菜单设置(附源码)

热门文章

  1. CSS3新特性总结及CSS组件、特效汇总
  2. 2022最新软件测试工具大全
  3. 群晖 建立文件禁止访问_在设计系统中建立可访问性
  4. 如何删除 Windows 托盘区指定图标?
  5. 基于乐鑫 ESP32 的智能手表
  6. 友盟+尚直虎:大数据重塑数字营销方式
  7. ipad2019编写html,iPad 2019款值得入手吗 iPad的极致性价比2019款介绍
  8. Python快速入门视频
  9. zookeeper简要梳理
  10. WIN10 修改Edge浏览器图标