通过 Notification 类 实现缩小窗体到任务栏(转)

Notification此类提供 Windows CE 通知功能的托管实现。只有 Pocket PC 上支持此类。

您可以创建通知,然后使用 Visible 属性来根据需要显示它们。InitialDuration 属性设置消息气球最初显示的时间。如果将 InitialDuration 设置为零,并将 Visible 设置为 true,则消息气球不会显示,但是标题栏中会有它的图标,单击可以重新激活它。每当显示或隐藏气球时,无论是使用 Visible 属性以编程方式进行此操作,还是通过用户交互方式进行此操作,都会发生 BalloonChanged 事件。

除了纯文本之外,您还可以在消息气球中创建 HTML 内容的用户通知。HTML 由 Pocket PC HTML 控件来呈现;您可以通过 Response 属性分析 ResponseSubmittedEventArgs 类提供的响应字符串来响应 HTML 窗体中的值。

Cmd:2 标识符

标识符“cmd:2”在 Windows CE 中具有特定的用途,用于关闭通知。如果 cmd:2 是消息气球中的 HTML 按钮或其他元素的名称,则不会引发 ResponseSubmitted 事件。通知被关闭了,但它的图标放置在标题栏上,以后还可以对它作出响应。

Note注意

如果有一个链接或元素的名称为“cmd:n”(其中 n 可以是任意整数),则不会引发 ResponseSubmitted 事件。不过,建议只将 cmd:2 用作关闭通知的标识符。

using System;
using System.Windows.Forms;
using System.Drawing;
using Microsoft.WindowsCE.Forms;
using System.Reflection;
using System.Text;
using System.IO;
namespace notificationtest
{
    public class Form1 : System.Windows.Forms.Form
    {
        private System.Windows.Forms.Button button1;
        private System.Windows.Forms.StatusBar statusBar1;
        private Microsoft.WindowsCE.Forms.Notification notification1;
        public Form1()
        {
            InitializeComponent();//初始化成员
           
            ConfigNotification();
            statusBar1.Text = "";
            // Display the OK button for closing the application.
            this.MinimizeBox = false;

        }
        protected override void Dispose(bool disposing)
        {
            base.Dispose(disposing);
        }
        private void InitializeComponent()
        {
            this.button1 = new System.Windows.Forms.Button();
            this.statusBar1 = new System.Windows.Forms.StatusBar();
            this.SuspendLayout();
//
// button1
//
            this.button1.Location = new System.Drawing.Point(6, 134);
            this.button1.Name = "button1";
            this.button1.TabIndex = 20;
            this.button1.Text = "Notify";
            this.button1.Click += new System.EventHandler(this.button1_Click);
//
// statusBar1
//
            this.statusBar1.Location = new System.Drawing.Point(0, 246);
            this.statusBar1.Name = "statusBar1";
            this.statusBar1.Size = new System.Drawing.Size(240, 22);
            this.statusBar1.Text = "";
//
// Form1
//
            this.ClientSize = new System.Drawing.Size(240, 268);
            this.Controls.Add(this.statusBar1);
            this.Controls.Add(this.button1);
            this.Name = "Form1";
            this.Text = "Notify Demo";
            this.ResumeLayout(false);
        }
        static void Main()
        {
            Application.Run(new Form1());
        }
    private void ConfigNotification()
    {
            // Create a Notification.
            notification1 = new Microsoft.WindowsCE.Forms.Notification();
           
            try
            {
                    // Provide an icon for the notification to appear in the title bar when dismissed.
                    // Assumes an icon file is compiled with the assembly as an embedded resource.
                   //ico图片资源
                   Assembly asm = Assembly.GetExecutingAssembly();
                   notification1.Icon = new Icon(asm.GetManifestResourceStream("notify.ico"),16,16);
                   //显示的标题
                    notification1.Caption = "Notification scenario - data download";
                  
                    // If notification is urgent, set to true.
                    notification1.Critical = false;
                   
                    // Create the text for the notification.
                    // Use a StringBuilder for better performance.
                    StringBuilder HTMLString = new StringBuilder();
                    HTMLString.Append("<html><body>");
                    HTMLString.Append("<font color=\"#0000FF\"><b>Data ready to download</b></font>");
                    HTMLString.Append("&nbsp;&nbsp;&nbsp;&nbsp;<a href=\"settings\">Settings</a>");
                    HTMLString.Append("<br><form method=\"GET\" action=notify>");
                    HTMLString.Append("<SELECT NAME=\"lstbx\">");
                    HTMLString.Append("<OPTION VALUE=\"0\">Start now</OPTION><OPTION VALUE=\"1\">In 1 hr</OPTION>");
                    HTMLString.Append("<OPTION VALUE=\"2\">In 2 hrs</OPTION><OPTION VALUE=\"3\">In 3 hrs</OPTION>");
                    HTMLString.Append("<OPTION VALUE=\"4\">In 4 hrs</OPTION></SELECT>");
                    HTMLString.Append("<input type=checkbox name=chkbx>Notify completion");
                    HTMLString.Append("<br><input type='submit'>");
                    HTMLString.Append("<input type=button name='cmd:2' value='Postpone'>");
                    HTMLString.Append("</body></html>");
                    // Set the Text property to the HTML string.
                    notification1.Text = HTMLString.ToString();
                    // Add event handlers.
                   
                    notification1.BalloonChanged += new BalloonChangedEventHandler(OnBalloonChanged);
                    notification1.ResponseSubmitted += new ResponseSubmittedEventHandler(OnResponseSubmitted);
            }
           
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
           
           
        }

        // Clicking the button creates a notification
        // that initally displays for 20 seconds.
        // A button is used here for demonstration purposes only;
        // normally, a notification is sent in response to another event. 
        private void button1_Click(object sender, System.EventArgs e)
        {
            notification1.InitialDuration = 20;
            notification1.Visible = true;
            statusBar1.Text = "";
        }
        // You can use the BalloonChanged event
        // created by tracking each time the notification is made visible.
        private void OnBalloonChanged(object obj, BalloonChangedEventArgs balevent)
        {
            if (balevent.Visible == true)
            {
                // You can add code here to add
                // functionality such as user interface
                // changes that should occur when
                // the notification is displayed.
            }
        }
       
        // When a ResponseSubmitted event occurs, this event handler
        // parses the response to determine values in the HTML form.
        private void OnResponseSubmitted(object obj, ResponseSubmittedEventArgs resevent)
        {
            // Use a StringBuilder to create a log of the response.
            StringBuilder LogResponse = new StringBuilder();

            // If the response contains the name specified for the action value
            // of the HTML form, in this case "notify," get the value of the
            // selected option from the SELECT list. An example of the
            // response string would be notify?lstbx=0.
            if (resevent.Response.Substring(0, 6) == "notify")
            {
                int choice = Convert.ToInt32(resevent.Response.Substring(13, 1));
                switch (choice)
                {
                    case 0:
                        LogResponse.Equals("submit");
                        break;
                    case 1:
                        LogResponse.Equals("opt 1");
                        break;
                    case 2:
                        LogResponse.Equals("opt 2");
                        break;
                    case 3:
                        LogResponse.Equals("opt 3");
                        break;
                    case 4:
                        LogResponse.Equals("opt 4");
                        break;
                }
                // If the checkbox in the form is checked, the response
                // string could be as follows: notify?lstbx=0chkbx=on
                // You can determine whether the check box is selected
                // by checking whether the response ends with "on".
                if (resevent.Response.EndsWith("on"))
                    LogResponse.Equals("checkbox");
            }
            // If the user clicked the settings link,
            // log the response. This example could display
            // a dialog box by activating another form.
            else if (resevent.Response == "settings")
            {
                // Display a settings dialog by activating
                // a form named 'Settings':
                // Settings.Activate
                LogResponse.Equals("Postponed by clicking link");
                // The user needs to respond to the notification
                // after checking the settings, so set the
                // InitialDuration and Visible properties so
                // that the icon appears in the title bar.
                notification1.InitialDuration = 0;
                notification1.Visible = true;
            }
            // Display the response on the status bar.
            statusBar1.Text = LogResponse.ToString() + " HTML: " + resevent.Response.ToString();
        }
    }
}
以下是我做的例子 缩小窗体到 任务栏
posted @ 2011-06-16 11:26  董雨  阅读(261)  评论(0编辑  收藏  举报