前言
消息通知在應(yīng)用程序中,是一種非常有用的功能,實(shí)現(xiàn)對(duì)一些重要信息、提醒或警告及時(shí)向用戶展示。我們?cè)谑褂密浖r(shí),通常會(huì)收到一種從桌面右下角彈出的(提示信息或廣告)信息框。本文將介紹使用 C# 實(shí)現(xiàn)此種方式的信息通知窗口。
實(shí)現(xiàn)
1、使用 API 的 AnimateWindow 函數(shù)
using System;
using System.Runtime.InteropServices;
namespace Fountain.WinForm.MessageBoxDemo
{
public class Win32
{
/// <summary>
/// 自左向右顯示窗口,該標(biāo)記可以在遷移轉(zhuǎn)變動(dòng)畫和滑動(dòng)動(dòng)畫中應(yīng)用。應(yīng)用AW_CENTER標(biāo)記時(shí)忽視該標(biāo)記
/// </summary>
public const int AW_HOR_POSITIVE = 0x0001;
/// <summary>
/// 自右向左顯示窗口,該標(biāo)記可以在遷移轉(zhuǎn)變動(dòng)畫和滑動(dòng)動(dòng)畫中應(yīng)用。應(yīng)用AW_CENTER標(biāo)記時(shí)忽視該標(biāo)記
/// </summary>
public const int AW_HOR_NEGATIVE = 0x0002;
/// <summary>
/// 自頂向下顯示窗口,該標(biāo)記可以在遷移轉(zhuǎn)變動(dòng)畫和滑動(dòng)動(dòng)畫中應(yīng)用。應(yīng)用AW_CENTER標(biāo)記時(shí)忽視該標(biāo)記
/// </summary>
public const int AW_VER_POSITIVE = 0x0004;
/// <summary>
/// 自下向上顯示窗口,該標(biāo)記可以在遷移轉(zhuǎn)變動(dòng)畫和滑動(dòng)動(dòng)畫中應(yīng)用。應(yīng)用AW_CENTER標(biāo)記時(shí)忽視該標(biāo)記該標(biāo)記
/// </summary>
public const int AW_VER_NEGATIVE = 0x0008;
/// <summary>
/// 若應(yīng)用了AW_HIDE標(biāo)記,則使窗口向內(nèi)重疊;不然向外擴(kuò)大
/// </summary>
public const int AW_CENTER = 0x0010;
/// <summary>
/// 隱蔽窗口
/// </summary>
public const int AW_HIDE = 0x10000;
/// <summary>
/// 激活窗口,在應(yīng)用了AW_HIDE標(biāo)記后不要應(yīng)用這個(gè)標(biāo)記
/// </summary>
public const int AW_ACTIVE = 0x20000;
/// <summary>
/// 滑動(dòng)類型動(dòng)畫結(jié)果,默認(rèn)為遷移轉(zhuǎn)變動(dòng)畫類型,當(dāng)應(yīng)用AW_CENTER標(biāo)記時(shí),這個(gè)標(biāo)記就被忽視
/// </summary>
public const int AW_SLIDE = 0x40000;
/// <summary>
/// 淡入淡出結(jié)果
/// </summary>
public const int AW_BLEND = 0x80000;
/// <summary>
/// 窗體動(dòng)畫函數(shù)
/// </summary>
/// <param name="hwnd"></param>
/// <param name="dwTime"></param>
/// <param name="dwFlags"></param>
/// <returns></returns>
[DllImport("user32")]
public static extern bool AnimateWindow(IntPtr hwnd, int dwTime, int dwFlags);
}
}
using System;
using System.Drawing;
using System.Windows.Forms;
namespace Fountain.WinForm.MessageBoxDemo
{
public partial class FormMessageBox : Form
{
/// <summary>
/// 關(guān)閉窗口的定時(shí)器
/// </summary>
private Timer formCloseTime = new Timer();
/// <summary>
/// 構(gòu)造方法
/// </summary>
public FormMessageBox()
{
InitializeComponent();
}
/// <summary>
/// 窗體加載
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void FormMessageBox_Load(object sender, EventArgs e)
{
// 手動(dòng)設(shè)置起始位置
this.StartPosition = FormStartPosition.Manual;
// 計(jì)算屏幕尺寸并將窗體放置在右下角
Rectangle screenRectangle = Screen.PrimaryScreen.WorkingArea;
int x = screenRectangle.Width - this.Width;
int y = screenRectangle.Height - this.Height;
this.Location = new Point(x, y);
this.TopMost = true;
Win32.AnimateWindow(this.Handle, 1000, Win32.AW_SLIDE + Win32.AW_VER_NEGATIVE);
this.ShowInTaskbar = false;
formCloseTime.Interval = 5000;
formCloseTime.Tick += new EventHandler(formCloseTime_Tick);
formCloseTime.Start();
}
/// <summary>
/// 關(guān)閉窗口的定時(shí)器響應(yīng)事件
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void formCloseTime_Tick(object sender, EventArgs e)
{
formCloseTime.Stop();
this.Close();
}
/// <summary>
///
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void FormMessageBox_FormClosed(object sender, FormClosedEventArgs e)
{
formCloseTime.Stop();
formCloseTime.Dispose();
Win32.AnimateWindow(this.Handle, 1000, Win32.AW_SLIDE + Win32.AW_VER_POSITIVE + Win32.AW_HIDE);
}
}
}
FormMessageBox formMessageBox = new FormMessageBox();
formMessageBox.Show();
2、控制窗體顯示
using System;
using System.Drawing;
using System.Threading;
using System.Windows.Forms;
namespace Fountain.WinForm.MessageBoxDemo
{
public partial class FormNotifyBox : Form
{
/// <summary>
/// 關(guān)閉窗口的定時(shí)器
/// </summary>
private System.Windows.Forms.Timer formCloseTime = new System.Windows.Forms.Timer();
/// <summary>
///
/// </summary>
private Point formPoint;
/// <summary>
///
/// </summary>
public FormNotifyBox()
{
InitializeComponent();
this.formPoint = new Point(Screen.PrimaryScreen.WorkingArea.Width - this.Width, Screen.PrimaryScreen.WorkingArea.Height);
// 設(shè)置窗體在屏幕右下角顯示
this.Location = formPoint;
}
/// <summary>
///
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void FormNotifyBox_Load(object sender, EventArgs e)
{
try
{
formCloseTime.Interval = 5000;
formCloseTime.Tick += new EventHandler(formCloseTime_Tick);
formCloseTime.Start();
this.TopMost = false;
this.BringToFront();
this.TopMost = true;
this.PointToClient(this.formPoint);
this.Location = this.formPoint;
this.Show();
for (int i = 0; i < this.Height; i++)
{
this.Location = new Point(formPoint.X, formPoint.Y - i);
// 消息框彈出速度,數(shù)值越大越慢
Thread.Sleep(1);
}
}
catch (Exception exception)
{
}
}
/// <summary>
/// 關(guān)閉窗口的定時(shí)器響應(yīng)事件
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void formCloseTime_Tick(object sender, EventArgs e)
{
formCloseTime.Enabled = false;
for (int i = 0; i <= this.Height; i++)
{
//彈出框向下移動(dòng)消失
Point point = new Point(this.Location.X, this.Location.Y + i);
this.PointToScreen(point);
//即時(shí)轉(zhuǎn)換成屏幕坐標(biāo)
this.Location = point;
//下降速度調(diào)節(jié),數(shù)字越小消失的速度越快,建議不大于10
Thread.Sleep(8);
}
this.Close();
this.Dispose();
}
}
}
FormNotifyBox notifyForm = new FormNotifyBox();
notifyForm.Show();
小結(jié)
以上通過二個(gè) WinForm 應(yīng)用示例,對(duì) C# 如何實(shí)現(xiàn)消息提示進(jìn)行了介紹。希望對(duì)有需要的伙伴能提供一些幫助,如需在WPF應(yīng)用程序中實(shí)現(xiàn),也可參考。
該文章在 2024/7/3 9:23:27 編輯過