前言
消息通知在應用程序中,是一種非常有用的功能,實現對一些重要信息、提醒或警告及時向用戶展示。我們在使用軟件時,通常會收到一種從桌面右下角彈出的(提示信息或廣告)信息框。本文將介紹使用 C# 實現此種方式的信息通知窗口。
實現
1、使用 API 的 AnimateWindow 函數
using System;
using System.Runtime.InteropServices;
namespace Fountain.WinForm.MessageBoxDemo
{
public class Win32
{
/// <summary>
/// 自左向右顯示窗口,該標記可以在遷移轉變動畫和滑動動畫中應用。應用AW_CENTER標記時忽視該標記
/// </summary>
public const int AW_HOR_POSITIVE = 0x0001;
/// <summary>
/// 自右向左顯示窗口,該標記可以在遷移轉變動畫和滑動動畫中應用。應用AW_CENTER標記時忽視該標記
/// </summary>
public const int AW_HOR_NEGATIVE = 0x0002;
/// <summary>
/// 自頂向下顯示窗口,該標記可以在遷移轉變動畫和滑動動畫中應用。應用AW_CENTER標記時忽視該標記
/// </summary>
public const int AW_VER_POSITIVE = 0x0004;
/// <summary>
/// 自下向上顯示窗口,該標記可以在遷移轉變動畫和滑動動畫中應用。應用AW_CENTER標記時忽視該標記該標記
/// </summary>
public const int AW_VER_NEGATIVE = 0x0008;
/// <summary>
/// 若應用了AW_HIDE標記,則使窗口向內重疊;不然向外擴大
/// </summary>
public const int AW_CENTER = 0x0010;
/// <summary>
/// 隱蔽窗口
/// </summary>
public const int AW_HIDE = 0x10000;
/// <summary>
/// 激活窗口,在應用了AW_HIDE標記后不要應用這個標記
/// </summary>
public const int AW_ACTIVE = 0x20000;
/// <summary>
/// 滑動類型動畫結果,默認為遷移轉變動畫類型,當應用AW_CENTER標記時,這個標記就被忽視
/// </summary>
public const int AW_SLIDE = 0x40000;
/// <summary>
/// 淡入淡出結果
/// </summary>
public const int AW_BLEND = 0x80000;
/// <summary>
/// 窗體動畫函數
/// </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>
/// 關閉窗口的定時器
/// </summary>
private Timer formCloseTime = new Timer();
/// <summary>
/// 構造方法
/// </summary>
public FormMessageBox()
{
InitializeComponent();
}
/// <summary>
/// 窗體加載
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void FormMessageBox_Load(object sender, EventArgs e)
{
// 手動設置起始位置
this.StartPosition = FormStartPosition.Manual;
// 計算屏幕尺寸并將窗體放置在右下角
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>
/// 關閉窗口的定時器響應事件
/// </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>
/// 關閉窗口的定時器
/// </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);
// 設置窗體在屏幕右下角顯示
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);
// 消息框彈出速度,數值越大越慢
Thread.Sleep(1);
}
}
catch (Exception exception)
{
}
}
/// <summary>
/// 關閉窗口的定時器響應事件
/// </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++)
{
//彈出框向下移動消失
Point point = new Point(this.Location.X, this.Location.Y + i);
this.PointToScreen(point);
//即時轉換成屏幕坐標
this.Location = point;
//下降速度調節,數字越小消失的速度越快,建議不大于10
Thread.Sleep(8);
}
this.Close();
this.Dispose();
}
}
}
FormNotifyBox notifyForm = new FormNotifyBox();
notifyForm.Show();
小結
以上通過二個 WinForm 應用示例,對 C# 如何實現消息提示進行了介紹。希望對有需要的伙伴能提供一些幫助,如需在WPF應用程序中實現,也可參考。
該文章在 2024/7/3 9:23:27 編輯過