【发布时间】:2015-08-19 17:26:57
【问题描述】:
如何创建一个在最后几秒后自动关闭的通知弹出表单。用作应用程序中的通知消息框。
【问题讨论】:
如何创建一个在最后几秒后自动关闭的通知弹出表单。用作应用程序中的通知消息框。
【问题讨论】:
Timer 放入您的通知表单(从工具箱 拖放它)。 Interval 设置为您希望表单显示的超时时间(在属性 窗口中)。 Tick 定时器事件。在此事件处理程序关闭表单中:this.Close();
Shown事件通知表。在此事件处理程序中启动计时器:timer1.Start();
【讨论】:
Timer从工具箱拖到窗体,在属性窗口设置间隔,在属性窗口订阅事件。
我假设您说的是出现在屏幕右下角的气球?您可以使用名为NotifyIcon 的控件,然后您需要做的就是编辑BalloonTipText 和BalloonTipTitle 属性。然后你可以通过调用ShowBalloonTip方法来显示它。
示例代码:
NotifyIcon n = new NotifyIcon();
n.BalloonTipText = "Details of the message go here";
n.BalloonTipTitle = "Message from Program";
n.ShowBalloonTip(2000);
如果您想让用户出于任何目的点击图标,您可以订阅一些事件,例如BalloonTipClicked。
【讨论】:
感谢@Sergey 的回答,这是我制作的通知消息框。
Label 和Timer。label 的Dock 属性设置为Fill
Timer 的Enabled 属性设置为True
用这些替换它的代码(当然你可以改变命名空间)
using System;
using System.Drawing;
using System.Windows.Forms;
namespace myNotificationBox
{
public partial class frmNotification : Form
{
private string _message;
private int _time = 1000;
public frmNotification(string message)
{
InitializeComponent();
_message = message;
}
public frmNotification(string message,int time)
{
InitializeComponent();
_message = message;
_time = time;
}
private void tmr_Tick(object sender, EventArgs e)
{
this.Close();
}
private void frmNotification_Load(object sender, EventArgs e)
{
lblMessage.Text = _message;
this.Width = _message.Length*10;
this.Height = 30;
tmr.Interval = _time;
tmr.Start();
}
protected override void OnPaint(PaintEventArgs e)
{
base.OnPaint(e);
ControlPaint.DrawBorder(e.Graphics, ClientRectangle,
Color.Teal, 3, ButtonBorderStyle.Solid,
Color.Teal, 3, ButtonBorderStyle.Solid,
Color.Teal, 3, ButtonBorderStyle.Solid,
Color.Teal, 3, ButtonBorderStyle.Solid);
//Rectangle rect = this.ClientRectangle;
//LinearGradientBrush brush = new LinearGradientBrush(rect, Color.Snow, Color.SeaShell, 60); //LightCyan Lavender LightGray
//e.Graphics.FillRectangle(brush, rect);
//base.OnPaint(e);
}
#region Overloaded Show message to display message box.
/// <summary>
/// Show method is overloaded which is used to display message
/// and this is static method so that we don't need to create
/// object of this class to call this method.
/// </summary>
/// <param name="messageText"></param>
///
internal static DialogResult Show(string messageText)
{
frmNotification frmNotification = new frmNotification(messageText);
frmNotification.ShowDialog();
return frmNotification.DialogResult;
}
internal static DialogResult Show(string messageText, int time)
{
frmNotification frmNotification = new frmNotification(messageText, time);
frmNotification.ShowDialog();
return frmNotification.DialogResult;
}
#endregion
}
}
现在你可以通过两种方式调用它:
frmNotification.Show("message"); //default interval would be 1000
frmNotification.Show("message",2000);
【讨论】: