下面是详细讲解“C# 重写Notification提示窗口的示例代码”的完整攻略:
一、什么是Notification提示窗口
Notification提示窗口就是Windows操作系统的一个提示框,一般用于通知用户系统的一些状态变化或提示信息。它一般弹出在屏幕的右下角,显示一定的时间之后会自动消失。
二、怎样重写Notification提示窗口
要重写Notification提示窗口,需要用到Windows Forms中的NotifyIcon控件。这个控件可以在系统托盘中显示一个图标,同时支持弹出BalloonTip提示窗口。我们可以通过自定义BalloonTip窗口的样式和内容来实现重写Notification提示窗口的效果。
下面是一个简单的示例:
示例一
//创建一个NotifyIcon控件
private NotifyIcon notifyIcon1 = new NotifyIcon();
//设置NotifyIcon控件的属性
notifyIcon1.Icon = new Icon("MyIcon.ico");
notifyIcon1.Visible = true;
//注册NotifyIcon控件的BalloonTip显示事件
notifyIcon1.BalloonTipShown += NotifyIcon1_BalloonTipShown;
//注册NotifyIcon控件的BalloonTip关闭事件
notifyIcon1.BalloonTipClosed += NotifyIcon1_BalloonTipClosed;
//弹出一个自定义的BalloonTip提示窗口
private void ShowNotification(string message)
{
notifyIcon1.BalloonTipTitle = "提示信息";
notifyIcon1.BalloonTipText = message;
notifyIcon1.BalloonTipIcon = ToolTipIcon.Info;
notifyIcon1.ShowBalloonTip(3000);
}
//处理BalloonTip关闭事件
private void NotifyIcon1_BalloonTipClosed(object sender, EventArgs e)
{
//TODO: BalloonTip关闭时需要执行的操作
}
//处理BalloonTip显示事件
private void NotifyIcon1_BalloonTipShown(object sender, EventArgs e)
{
//TODO: BalloonTip显示时需要执行的操作
}
上面的代码中,我们首先创建了一个NotifyIcon控件,并设置了它的Icon和Visible属性。然后,我们注册了NotifyIcon控件的BalloonTipShown和BalloonTipClosed事件,用于处理BalloonTip提示窗口的显示和关闭。最后,我们实现了ShowNotification方法,用于弹出一个自定义的BalloonTip提示窗口。
示例二
接下来,我们再看一个稍微复杂一些的示例,该示例实现了一个带有多个按钮和文本框的BalloonTip提示窗口。
//创建一个NotifyIcon控件
private NotifyIcon notifyIcon1 = new NotifyIcon();
//设置NotifyIcon控件的属性
notifyIcon1.Icon = new Icon("MyIcon.ico");
notifyIcon1.Visible = true;
//注册NotifyIcon控件的BalloonTip显示事件
notifyIcon1.BalloonTipShown += NotifyIcon1_BalloonTipShown;
//注册NotifyIcon控件的BalloonTip关闭事件
notifyIcon1.BalloonTipClosed += NotifyIcon1_BalloonTipClosed;
//弹出一个自定义的BalloonTip提示窗口
private void ShowCustomNotification(string message)
{
//创建BalloonTip的容器控件
var container = new Panel();
container.BackColor = Color.White;
//创建BalloonTip的文本框
var textBox = new TextBox();
textBox.Multiline = true;
textBox.ReadOnly = true;
textBox.Text = message;
textBox.Dock = DockStyle.Top;
container.Controls.Add(textBox);
//创建BalloonTip的按钮
var btnOK = new Button();
btnOK.Text = "确定";
btnOK.Dock = DockStyle.Right;
container.Controls.Add(btnOK);
var btnCancel = new Button();
btnCancel.Text = "取消";
btnCancel.Dock = DockStyle.Right;
container.Controls.Add(btnCancel);
//创建BalloonTip
var balloonTip = new CustomBalloonTip(notifyIcon1);
balloonTip.AutoClose = false;
balloonTip.Size = container.PreferredSize;
balloonTip.Controls.Add(container);
//显示BalloonTip
balloonTip.Show(5000);
}
//处理BalloonTip关闭事件
private void NotifyIcon1_BalloonTipClosed(object sender, EventArgs e)
{
//TODO: BalloonTip关闭时需要执行的操作
}
//处理BalloonTip显示事件
private void NotifyIcon1_BalloonTipShown(object sender, EventArgs e)
{
//TODO: BalloonTip显示时需要执行的操作
}
//自定义的BalloonTip控件
public class CustomBalloonTip : ToolTip
{
private const int CS_DROPSHADOW = 0x20000;
private Control _owner;
public CustomBalloonTip(Control owner)
{
_owner = owner;
//设置BalloonTip的样式
this.UseFading = true;
this.UseAnimation = true;
this.IsBalloon = true;
this.ShowAlways = true;
//去掉BalloonTip的阴影
this.SetStyle(ControlStyles.AllPaintingInWmPaint, true);
this.SetStyle(ControlStyles.UserPaint, true);
this.SetStyle(ControlStyles.DoubleBuffer, true);
//将BalloonTip的样式设为WS_EX_TOOLWINDOW
int exStyle = (int)Win32API.GetWindowLong(this.Handle, Win32API.GWL_EXSTYLE);
exStyle |= (int)Win32API_WS_EX.WS_EX_TOOLWINDOW;
Win32API.SetWindowLong(this.Handle, Win32API.GWL_EXSTYLE, (IntPtr)exStyle);
}
protected override void CreateHandle()
{
base.CreateHandle();
Win32API.SetWindowPos(this.Handle, Win32API.HWND_TOPMOST, 0, 0, 0, 0,
Win32API.SWP_NOMOVE | Win32API.SWP_NOSIZE | Win32API.SWP_NOACTIVATE);
}
protected override CreateParams CreateParams
{
get
{
CreateParams cp = base.CreateParams;
cp.ExStyle |= CS_DROPSHADOW;
return cp;
}
}
protected override void OnPopup(System.EventArgs e)
{
base.OnPopup(e);
this.UpdateOwner();
}
protected override void OnDraw(System.Windows.Forms.DrawToolTipEventArgs e)
{
e.DrawBackground();
e.DrawBorder();
e.DrawText(TextFormatFlags.TextBoxControl | TextFormatFlags.LeftAndRightPadding);
}
private void UpdateOwner()
{
if (_owner != null && this.Visible)
{
//获取BalloonTip的位置和大小
Rectangle rect = this.Bounds;
rect.X = _owner.PointToScreen(Point.Empty).X - rect.Width / 2;
rect.Y = _owner.PointToScreen(Point.Empty).Y - rect.Height - 10;
//更新BalloonTip的位置
Win32API.SetWindowPos(this.Handle, Win32API.HWND_TOPMOST, rect.X, rect.Y,
rect.Width, rect.Height, Win32API.SWP_NOACTIVATE);
}
}
}
public class Win32API
{
[DllImport("user32.dll", SetLastError = true)]
public static extern IntPtr GetWindowLong(IntPtr hWnd, int nIndex);
[DllImport("user32.dll")]
public static extern IntPtr SetWindowLong(IntPtr hWnd, int nIndex, IntPtr dwNewLong);
[DllImport("user32.dll", SetLastError = true)]
[return: MarshalAs(UnmanagedType.Bool)]
public static extern bool SetWindowPos(IntPtr hWnd, IntPtr hWndInsertAfter, int X, int Y, int cx, int cy, uint uFlags);
public const int WS_EX_TOOLWINDOW = 0x80;
public const int GWL_EXSTYLE = -20;
public static readonly IntPtr HWND_TOPMOST = new IntPtr(-1);
public const int SWP_NOSIZE = 0x0001;
public const int SWP_NOMOVE = 0x0002;
public const int SWP_NOACTIVATE = 0x0010;
}
public enum Win32API_WS_EX : int
{
WS_EX_LAYERED = 0x00080000,
WS_EX_TRANSPARENT = 0x00000020,
WS_EX_TOOLWINDOW = 0x80,
}
这个示例中,我们首先创建了一个NotifyIcon控件,并设置了它的Icon和Visible属性。然后,我们注册了NotifyIcon控件的BalloonTipShown和BalloonTipClosed事件,用于处理BalloonTip提示窗口的显示和关闭。接着,我们实现了ShowCustomNotification方法,用于弹出一个自定义的BalloonTip提示窗口。
在ShowCustomNotification方法中,我们首先创建了一个Panel控件,用于作为BalloonTip的容器。然后,我们在容器中添加了一个文本框和两个按钮,用于显示提示信息和完成一些操作。接着,我们创建了一个自定义的BalloonTip控件,设置它的样式和大小,并将容器添加到它的Controls集合中。最后,我们调用了BalloonTip的Show方法,显示这个自定义的BalloonTip提示窗口。
在CustomBalloonTip中,我们重写了一些方法,用于自定义BalloonTip的样式和位置。其中,CreateHandle方法用于设置BalloonTip的句柄为最顶层窗口,并将BalloonTip的样式设为WS_EX_TOOLWINDOW;CreateParams属性用于设置BalloonTip的ExStyle,使之显示阴影;OnPopup方法用于在BalloonTip显示时更新BalloonTip的位置;OnDraw方法用于绘制BalloonTip的边框和文本;UpdateOwner方法用于更新BalloonTip的位置,使之显示在NotifyIcon控件的下方。
好了,以上就是关于“C# 重写Notification提示窗口的示例代码”的完整攻略了。希望对你有所帮助。
本站文章如无特殊说明,均为本站原创,如若转载,请注明出处:C# 重写Notification提示窗口的示例代码 - Python技术站