【问题标题】:How to make Notification Message Box如何制作通知消息框
【发布时间】:2015-08-19 17:26:57
【问题描述】:

如何创建一个在最后几秒后自动关闭的通知弹出表单。用作应用程序中的通知消息框。

【问题讨论】:

    标签: c# winforms c#-4.0


    【解决方案1】:
    1. Timer 放入您的通知表单(从工具箱 拖放它)。
    2. 将它的Interval 设置为您希望表单显示的超时时间(在属性 窗口中)。
    3. 订阅Tick 定时器事件。在此事件处理程序关闭表单中:this.Close();
    4. 订阅Shown事件通知表。在此事件处理程序中启动计时器:timer1.Start();

    【讨论】:

    • @neo 没有可共享的匹配代码 - 大多数这些步骤都是在设计器中完成的。例如。将Timer从工具箱拖到窗体,在属性窗口设置间隔,在属性窗口订阅事件。
    • 如此简单又如此智能!
    【解决方案2】:

    我假设您说的是出现在屏幕右下角的气球?您可以使用名为NotifyIcon 的控件,然后您需要做的就是编辑BalloonTipTextBalloonTipTitle 属性。然后你可以通过调用ShowBalloonTip方法来显示它。

    示例代码:

    NotifyIcon n = new NotifyIcon();
    n.BalloonTipText = "Details of the message go here";
    n.BalloonTipTitle = "Message from Program";
    n.ShowBalloonTip(2000);
    

    如果您想让用户出于任何目的点击图标,您可以订阅一些事件,例如BalloonTipClicked

    【讨论】:

      【解决方案3】:

      感谢@Sergey 的回答,这是我制作的通知消息框。

      1. 创建一个新表单
      2. 向其中添加LabelTimer
      3. labelDock 属性设置为Fill
      4. TimerEnabled 属性设置为True
      5. 用这些替换它的代码(当然你可以改变命名空间)

        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
        
        }
        }
        
      6. 现在你可以通过两种方式调用它:

        frmNotification.Show("message"); //default interval would be 1000
        frmNotification.Show("message",2000);
        

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2019-12-16
        • 1970-01-01
        • 1970-01-01
        • 2021-11-25
        • 2017-02-23
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多