一個小小的托盤程序(定時關機)

主窗體. mainForm:

//mainForm.cs:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Diagnostics;


namespace C7Tray
{
    public partial class mainForm : Form
    {

        public mainForm()
        {
            InitializeComponent();
            timer.Enabled = false;
            this.notifyIcon.DoubleClick+=new System.EventHandler(
                                this.shutdownToolStripMenuItem_Click);//Db_click to show the "shutdown"
        }

        private static string processTime = "";
        public static string ProcessTime
        {
            get
            {
                return processTime;
            }
            set
            {
                processTime = value;
            }
        }

        private static string indicator = "s";  //let the default mode is shutdown
        public static string Indicator
        {
            get
            {
                return indicator;
            }
            set
            {
                indicator = value;
            }
        }


        private void shutdownToolStripMenuItem_Click(object sender, EventArgs e)
        {
            shutdown sd = new shutdown();
            sd.ShowDialog();
            if (ProcessTime != "")   //No this,if the user close the subForm and not input anythig, timer will start too.
            {
                timer.Enabled = true;
                timer.Start();
            }
        }

        private void timer_Tick(object sender, EventArgs e)
        {
            int counter = Int32.Parse(mainForm.ProcessTime);
            counter--;
            ProcessTime = counter.ToString();

            if (counter < 300)  //if remain 5 minutes, show the warning....
            {
                string showText = "After " + ProcessTime + "s, your PC will be shutdown!/n Click me to stop!";
                notifyIcon.ShowBalloonTip(1000, "Warning", showText, ToolTipIcon.Warning);
            }

            if (counter <= 0)
            {
                //MessageBox.Show("success!");
                shutdownFun sdf = new shutdownFun(Indicator);
                sdf.shutdown_Run();
            }
        }

        private void cancelToolStripMenuItem_Click(object sender, EventArgs e)
        {
            timer.Stop();
        }

        private void rebootToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Reboot rt = new Reboot();
            rt.ShowDialog();
            if (ProcessTime != "")  
            {
                timer.Enabled = true;
                timer.Start();
            }
        }

        private void eXitToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Application.Exit();
        }

        private void notifyIcon_BalloonTipClicked(object sender, EventArgs e)
        {
            timer.Stop();
        }

    }
    #region the Class for shutdown&reboot  //have a string parameter to indicate R or S
    public class shutdownFun    
    {
        public shutdownFun(string tempargs)
        {
            args = "-f -" + tempargs + " -t 0";
            ms_winDir = Environment.GetFolderPath(Environment.SpecialFolder.System);
        }
        public shutdownFun()
        {
            MessageBox.Show("Error!");
        }

        public void shutdown_Run()
        {
            ProcessStartInfo shutdown = new ProcessStartInfo();
            shutdown.FileName = ms_winDir + "//shutdown.exe";
            shutdown.Arguments = args;
            Process.Start(shutdown);
        }

        string args;
        string ms_winDir;
    }
    #endregion

//mainForm.Designer.cs:

namespace C7Tray
{
    partial class mainForm
    {
        /// <summary>
        /// Required designer variable.
        /// </summary>
        private System.ComponentModel.IContainer components = null;

        /// <summary>
        /// Clean up any resources being used.
        /// </summary>
        /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
        protected override void Dispose(bool disposing)
        {
            if (disposing && (components != null))
            {
                components.Dispose();
            }
            base.Dispose(disposing);
        }

        #region Windows Form Designer generated code

        /// <summary>
        /// Required method for Designer support - do not modify
        /// the contents of this method with the code editor.
        /// </summary>
        private void InitializeComponent()
        {
            this.components = new System.ComponentModel.Container();
            System.ComponentModel.ComponentResourceManager resources = new System.ComponentModel.ComponentResourceManager(typeof(mainForm));
            this.notifyIcon = new System.Windows.Forms.NotifyIcon(this.components);
            this.contextMenuStrip = new System.Windows.Forms.ContextMenuStrip(this.components);
            this.shutdownToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
            this.rebootToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
            this.cancelToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
            this.eXitToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
            this.timer = new System.Windows.Forms.Timer(this.components);
            this.contextMenuStrip.SuspendLayout();
            this.SuspendLayout();
            //
            // notifyIcon
            //
            this.notifyIcon.ContextMenuStrip = this.contextMenuStrip;
            this.notifyIcon.Icon = ((System.Drawing.Icon)(resources.GetObject("notifyIcon.Icon")));
            this.notifyIcon.Text = "Crazy7/'s Tools";
            this.notifyIcon.Visible = true;
            this.notifyIcon.BalloonTipClicked += new System.EventHandler(this.notifyIcon_BalloonTipClicked);
            //
            // contextMenuStrip
            //
            this.contextMenuStrip.AccessibleRole = System.Windows.Forms.AccessibleRole.None;
            this.contextMenuStrip.Items.AddRange(new System.Windows.Forms.ToolStripItem[] {
            this.shutdownToolStripMenuItem,
            this.rebootToolStripMenuItem,
            this.cancelToolStripMenuItem,
            this.eXitToolStripMenuItem});
            this.contextMenuStrip.Name = "contextMenuStrip";
            this.contextMenuStrip.Size = new System.Drawing.Size(131, 92);
            //
            // shutdownToolStripMenuItem
            //
            this.shutdownToolStripMenuItem.Name = "shutdownToolStripMenuItem";
            this.shutdownToolStripMenuItem.Size = new System.Drawing.Size(130, 22);
            this.shutdownToolStripMenuItem.Text = "&Shutdown";
            this.shutdownToolStripMenuItem.Click += new System.EventHandler(this.shutdownToolStripMenuItem_Click);
            //
            // rebootToolStripMenuItem
            //
            this.rebootToolStripMenuItem.Name = "rebootToolStripMenuItem";
            this.rebootToolStripMenuItem.Size = new System.Drawing.Size(130, 22);
            this.rebootToolStripMenuItem.Text = "&Reboot";
            this.rebootToolStripMenuItem.Click += new System.EventHandler(this.rebootToolStripMenuItem_Click);
            //
            // cancelToolStripMenuItem
            //
            this.cancelToolStripMenuItem.Name = "cancelToolStripMenuItem";
            this.cancelToolStripMenuItem.Size = new System.Drawing.Size(130, 22);
            this.cancelToolStripMenuItem.Text = "&Cancel";
            this.cancelToolStripMenuItem.Click += new System.EventHandler(this.cancelToolStripMenuItem_Click);
            //
            // eXitToolStripMenuItem
            //
            this.eXitToolStripMenuItem.Name = "eXitToolStripMenuItem";
            this.eXitToolStripMenuItem.Size = new System.Drawing.Size(130, 22);
            this.eXitToolStripMenuItem.Text = "e&Xit";
            this.eXitToolStripMenuItem.Click += new System.EventHandler(this.eXitToolStripMenuItem_Click);
            //
            // timer
            //
            this.timer.Interval = 1000;

            this.timer.Tick += new System.EventHandler(this.timer_Tick);
            //
            // mainForm
            //
            this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
            this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
            this.ClientSize = new System.Drawing.Size(233, 162);
            this.Icon = ((System.Drawing.Icon)(resources.GetObject("$this.Icon")));
            this.Name = "mainForm";
            this.ShowInTaskbar = false;
            this.Text = "rCrayz7/'s Tray tools";
            this.WindowState = System.Windows.Forms.FormWindowState.Minimized;
            this.contextMenuStrip.ResumeLayout(false);
            this.ResumeLayout(false);

        }

        #endregion

        private System.Windows.Forms.NotifyIcon notifyIcon;
        private System.Windows.Forms.ContextMenuStrip contextMenuStrip;
        private System.Windows.Forms.ToolStripMenuItem shutdownToolStripMenuItem;
        private System.Windows.Forms.ToolStripMenuItem rebootToolStripMenuItem;
        private System.Windows.Forms.ToolStripMenuItem eXitToolStripMenuItem;
        private System.Windows.Forms.ToolStripMenuItem cancelToolStripMenuItem;
        private System.Windows.Forms.Timer timer;
    }
}

子窗體:shutdown & reboot

shutdown.cs:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace C7Tray
{
    public partial class shutdown : Form
    {
        public shutdown()
        {
            InitializeComponent();
        }

        private void OK_Click(object sender, EventArgs e)
        {
            string temp=inputWaitMin.Text;
            if (System.Text.RegularExpressions.Regex.IsMatch(temp, @"^[0-9]+$"))
            {
                //mainForm.Indicator = "s";
                int shutdownTime = (Int32.Parse(inputWaitMin.Text)) * 60;
                mainForm.ProcessTime = shutdownTime.ToString();
                this.Close();
            }
            else
            {
                MessageBox.Show("Please input a number!","Error",MessageBoxButtons.OK,MessageBoxIcon.Error);
                inputWaitMin.Text = "";
            }

        }

        private void shutdown_Activated(object sender, EventArgs e)
        {
            inputWaitMin.Focus();
        }
    }
}

//shutdown.Designer.cs:

namespace C7Tray
{

    partial class shutdown
    {
        /// <summary>
        /// Required designer variable.
        /// </summary>
        private System.ComponentModel.IContainer components = null;

        /// <summary>
        /// Clean up any resources being used.
        /// </summary>
        /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
        protected override void Dispose(bool disposing)
        {
            if (disposing && (components != null))

            {
                components.Dispose();
            }
            base.Dispose(disposing);
        }

        #region Windows Form Designer generated code

        /// <summary>
        /// Required method for Designer support - do not modify
        /// the contents of this method with the code editor.
        /// </summary>
        private void InitializeComponent()
        {
            this.label1 = new System.Windows.Forms.Label();

            this.inputWaitMin = new System.Windows.Forms.TextBox();
            this.label2 = new System.Windows.Forms.Label();
            this.OK = new System.Windows.Forms.Button();
            this.SuspendLayout();
            //
            // label1
            //
            this.label1.AutoSize = true;
            this.label1.Location = new System.Drawing.Point(12, 9);
            this.label1.Name = "label1";
            this.label1.Size = new System.Drawing.Size(151, 13);
            this.label1.TabIndex = 0;
            this.label1.Text = "Please input the time(Minute):";
            //
            // inputWaitMin
            //
            this.inputWaitMin.Location = new System.Drawing.Point(51, 36);
            this.inputWaitMin.Name = "inputWaitMin";
            this.inputWaitMin.Size = new System.Drawing.Size(63, 23);
            this.inputWaitMin.TabIndex = 1;
            //
            // label2
            //
            this.label2.AutoSize = true;

            this.label2.Location = new System.Drawing.Point(120, 39);
            this.label2.Name = "label2";
            this.label2.Size = new System.Drawing.Size(23, 13);
            this.label2.TabIndex = 2;
            this.label2.Text = "min";
            //
            // OK
            //
            this.OK.Location = new System.Drawing.Point(160, 39);
            this.OK.Name = "OK";
            this.OK.Size = new System.Drawing.Size(61, 20);
            this.OK.TabIndex = 3;
            this.OK.Text = "OK";
            this.OK.UseVisualStyleBackColor = true;
            this.OK.Click += new System.EventHandler(this.OK_Click);
            //
            // shutdown
            //
            this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
            this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;

            this.ClientSize = new System.Drawing.Size(245, 87);
            this.Controls.Add(this.OK);
            this.Controls.Add(this.label2);
            this.Controls.Add(this.inputWaitMin);
            this.Controls.Add(this.label1);
            this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedSingle;
            this.Name = "shutdown";
            this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
            this.Text = "shutdown";
            this.Activated += new System.EventHandler(this.shutdown_Activated);
            this.ResumeLayout(false);
            this.PerformLayout();

        }

        #endregion

        private System.Windows.Forms.Label label1;
        private System.Windows.Forms.TextBox inputWaitMin;
        private System.Windows.Forms.Label label2;
        private System.Windows.Forms.Button OK;
    }
}

//Reboot.cs:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace C7Tray
{
    public partial class Reboot : Form
    {
        public Reboot()
        {
            InitializeComponent();
        }

        private void OK_Click(object sender, EventArgs e)
        {
            string temp = inputWaitMin.Text;
            if (System.Text.RegularExpressions.Regex.IsMatch(temp, @"^[0-9]+$"))
            {
                mainForm.Indicator = "r";
                int shutdownTime = (Int32.Parse(inputWaitMin.Text)) * 60;
                mainForm.ProcessTime = shutdownTime.ToString();
                this.Close();
            }
            else
            {
                MessageBox.Show("Please input a number!", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                inputWaitMin.Text = "";
            }

        }
    }
}

//Reboot.Designer.cs:

namespace C7Tray
{
    partial class Reboot
    {
        /// <summary>
        /// Required designer variable.
        /// </summary>
        private System.ComponentModel.IContainer components = null;

        /// <summary>
        /// Clean up any resources being used.
        /// </summary>
        /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
        protected override void Dispose(bool disposing)
        {
            if (disposing && (components != null))
            {
                components.Dispose();
            }
            base.Dispose(disposing);
        }

        #region Windows Form Designer generated code

        /// <summary>
        /// Required method for Designer support - do not modify
        /// the contents of this method with the code editor.
        /// </summary>
        private void InitializeComponent()
        {
            this.OK = new System.Windows.Forms.Button();
            this.label2 = new System.Windows.Forms.Label();
            this.inputWaitMin = new System.Windows.Forms.TextBox();
            this.label1 = new System.Windows.Forms.Label();
            this.SuspendLayout();
            //
            // OK
            //
            this.OK.Location = new System.Drawing.Point(160, 50);
            this.OK.Name = "OK";
            this.OK.Size = new System.Drawing.Size(61, 20);
            this.OK.TabIndex = 7;
            this.OK.Text = "OK";
            this.OK.UseVisualStyleBackColor = true;
            this.OK.Click += new System.EventHandler(this.OK_Click);
            //
            // label2
            //
            this.label2.AutoSize = true;
            this.label2.Location = new System.Drawing.Point(120, 50);
            this.label2.Name = "label2";
            this.label2.Size = new System.Drawing.Size(23, 13);
            this.label2.TabIndex = 6;
            this.label2.Text = "min";
            //
            // inputWaitMin
            //
            this.inputWaitMin.Location = new System.Drawing.Point(51, 47);
            this.inputWaitMin.Name = "inputWaitMin";
            this.inputWaitMin.Size = new System.Drawing.Size(63, 23);
            this.inputWaitMin.TabIndex = 5;
            //
            // label1
            //
            this.label1.AutoSize = true;
            this.label1.Location = new System.Drawing.Point(12, 20);
            this.label1.Name = "label1";
            this.label1.Size = new System.Drawing.Size(151, 13);
            this.label1.TabIndex = 4;
            this.label1.Text = "Please input the time(Minute):";
            //
            // Reboot
            //
            this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
            this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
            this.ClientSize = new System.Drawing.Size(236, 96);
            this.Controls.Add(this.OK);
            this.Controls.Add(this.label2);
            this.Controls.Add(this.inputWaitMin);
            this.Controls.Add(this.label1);
            this.Name = "Reboot";
            this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
            this.Text = "Reboot";
            this.ResumeLayout(false);
            this.PerformLayout();

        }

        #endregion

        private System.Windows.Forms.Button OK;
        private System.Windows.Forms.Label label2;
        private System.Windows.Forms.TextBox inputWaitMin;
        private System.Windows.Forms.Label label1;
    }
}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章