Shutdown

using System;
using System.Runtime.InteropServices;
using System.Threading;
using System.Windows.Forms;
using Microsoft.Win32;

namespace ShutDown
{
    static class Program
    {
        #region DllImportAttribute
        [DllImport("user32.dll", EntryPoint = "ShowWindow")]
        static extern bool ShowWindow(IntPtr handle, int flags);

        [DllImport("user32.dll", EntryPoint = "SetForegroundWindow")]
        static extern bool SetForegroundWindow(IntPtr handle);
        #endregion

        [STAThread]
        static void Main()
        {
            #region Mutex
            bool isCreated; // 互斥体名称须唯一。
            using (Mutex newMutex = new Mutex(true, @"Local/DIYShutDown", out isCreated))
            {
                if (isCreated)
                {
                    Application.CurrentCulture = System.Globalization.CultureInfo.GetCultureInfo("zh-CN");
                    Application.EnableVisualStyles();
                    Application.SetCompatibleTextRenderingDefault(false);
                    using (RegistryKey subKey = Application.UserAppDataRegistry)
                    {
                        FormShutDown frame = new FormShutDown();
                        subKey.SetValue("Handle", frame.Handle);
                        Application.Run(frame);
                    }
                    newMutex.ReleaseMutex(); // 释放互斥体的所属权。
                }
                else
                {
                    string text = string.Format("“{0}”应用程序已经运行。", AppDomain.CurrentDomain.FriendlyName);
                    MessageBox.Show(text, "系统提示!", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                    using (RegistryKey subKey = Application.UserAppDataRegistry)
                    {
                        IntPtr handle = new IntPtr(Convert.ToInt32(subKey.GetValue("Handle")));
                        ShowWindow(handle, 1);
                        SetForegroundWindow(handle);
                    }
                    Application.ExitThread(); // 退出当前线程。
                }
            }
            #endregion
        }
    }
}

 

using System;
using System.Drawing;
using System.Runtime.InteropServices;
using System.Windows.Forms;

namespace ShutDown
{
    public partial class FormShutDown : Form
    {
        #region AnimateWindow
        [DllImport("user32.dll", EntryPoint = "AnimateWindow")]
        private static extern bool AnimateWindow(IntPtr handle, int ms, int flags);
        #endregion

        #region LockWorkStation
        [DllImport("user32.dll", EntryPoint = "LockWorkStation")]
        private static extern bool LockWorkStation();
        #endregion

        public FormShutDown()
        {
            #region
            InitializeComponent();
            this.TopMost = true; // 将窗体显示为最顶层窗体。
            this.ShowInTaskbar = false; // 在 Windows 任务栏中隐藏窗体。
            this.AutoScaleMode = AutoScaleMode.None; // 禁用自动缩放。
            this.FormBorderStyle = FormBorderStyle.None; // 窗体无边框。
            this.Location = new Point(Screen.GetWorkingArea(this).Size - this.Size);
            labelTime.Text = string.Format("{0:HH:mm:ss}", DateTime.Now);
            timePicker.Format = DateTimePickerFormat.Custom; // 自定义显示格式。
            timePicker.CustomFormat = " HH:mm:00";
            timePicker.ShowUpDown = true;
            timerInfo.Interval = 1000;
            timerInfo.Enabled = true;
            timerInfo.Tick += new EventHandler(timerInfo_Tick);
            notifyInfo.Visible = true;
            notifyInfo.Icon = Icon.ExtractAssociatedIcon(Application.ExecutablePath);
            notifyInfo.Text = AppDomain.CurrentDomain.FriendlyName;
            notifyInfo.MouseClick += new MouseEventHandler(notifyInfo_MouseClick);
            #endregion
        }

        #region TimeSpan
        private void timerInfo_Tick(object sender, EventArgs e)
        {
            labelTime.Text = string.Format("{0:HH:mm:ss}", DateTime.Now);
            if (buttonOK.Enabled)
                return;
            TimeSpan ts = timePicker.Value.Subtract(DateTime.Now);
            switch ((int)Math.Ceiling(ts.TotalSeconds))
            {
                case 30:
                    Console.Beep(); // 通过控制台扬声器播放提示音。
                    this.Visible = true;
                    break;
                case 0:
                    if (radioButtonSuspend.Checked)
                        Application.SetSuspendState(PowerState.Suspend, true, false); // 待机。
                    else if (radioButtonExit.Checked)
                        System.Diagnostics.Process.Start("shutdown.exe", "-s -f -t 0"); // 关机。
                    else if (radioButtonLock.Checked)
                        LockWorkStation(); // 锁机。
                    Application.Exit();
                    break;
            }
        }
        #endregion

        #region OKCancel
        private void buttonOK_Click(object sender, EventArgs e)
        {
            timePicker.Text = string.Format("{0:HH:mm:00}", timePicker.Value);
            this.Visible = false;
            buttonOK.Enabled = false;
        }

        private void buttonCancel_Click(object sender, EventArgs e)
        {
            timePicker.Text = string.Format("{0:HH:mm:00}", DateTime.Now);
            this.Visible = false;
            buttonOK.Enabled = true;
        }
        #endregion

        #region VisibleChanged
        protected override void OnVisibleChanged(EventArgs e)
        {
            base.OnVisibleChanged(e);
            if (this.Visible)
            {
                AnimateWindow(this.Handle, 1000, 0x60008);
                this.Activate(); // 激活窗体并给予它焦点。
                this.Refresh(); // 刷新。
            }
            else
            {
                AnimateWindow(this.Handle, 1000, 0x50004);
                notifyInfo.ShowBalloonTip(5, notifyInfo.Text, "在系统托盘中运行", ToolTipIcon.Info);
            }
        }
        #endregion

        #region FormClosing
        protected override void OnFormClosing(FormClosingEventArgs e)
        {
            base.OnFormClosing(e);
            this.Visible = false;
            switch (e.CloseReason)
            {
                case CloseReason.ApplicationExitCall:
                    notifyInfo.Visible = false;
                    break;
                case CloseReason.UserClosing:
                    e.Cancel = true;
                    break;
            }
        }
        #endregion

        #region NotifyIcon
        private void notifyInfo_MouseClick(object sender, MouseEventArgs e)
        {
            switch (e.Button)
            {
                case MouseButtons.Left:
                    this.Visible = !this.Visible;
                    break;
                case MouseButtons.Right:
                    if (MessageBox.Show(this, "您真的要退出“定时关机”系统吗?", "系统询问", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
                        Application.Exit();
                    break;
            }
        }
        #endregion
    }
}

 

namespace ShutDown
{
    partial class FormShutDown
    {
        private System.ComponentModel.IContainer components = null;

        protected override void Dispose(bool disposing)
        {
            if (disposing && (components != null))
            {
                components.Dispose();
            }
            base.Dispose(disposing);
        }

        #region Windows 窗体设计器生成的代码
        private void InitializeComponent()
        {
            this.components = new System.ComponentModel.Container();
            this.labelTime = new System.Windows.Forms.Label();
            this.timePicker = new System.Windows.Forms.DateTimePicker();
            this.timerInfo = new System.Windows.Forms.Timer(this.components);
            this.notifyInfo = new System.Windows.Forms.NotifyIcon(this.components);
            this.radioButtonSuspend = new System.Windows.Forms.RadioButton();
            this.radioButtonExit = new System.Windows.Forms.RadioButton();
            this.radioButtonLock = new System.Windows.Forms.RadioButton();
            this.buttonOK = new System.Windows.Forms.Button();
            this.buttonCancel = new System.Windows.Forms.Button();
            this.SuspendLayout();
            //
            // labelTime
            //
            this.labelTime.AutoSize = true;
            this.labelTime.Font = new System.Drawing.Font("宋体", 16F, System.Drawing.FontStyle.Bold);
            this.labelTime.Location = new System.Drawing.Point(47, 18);
            this.labelTime.Name = "labelTime";
            this.labelTime.Size = new System.Drawing.Size(106, 22);
            this.labelTime.TabIndex = 0;
            this.labelTime.Text = "00:00:00";
            //
            // timePicker
            //
            this.timePicker.CustomFormat = " HH:mm:00";
            this.timePicker.Font = new System.Drawing.Font("宋体", 12F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(134)));
            this.timePicker.Format = System.Windows.Forms.DateTimePickerFormat.Custom;
            this.timePicker.Location = new System.Drawing.Point(43, 56);
            this.timePicker.Name = "timePicker";
            this.timePicker.ShowUpDown = true;
            this.timePicker.Size = new System.Drawing.Size(116, 26);
            this.timePicker.TabIndex = 1;
            //
            // radioButtonSuspend
            //
            this.radioButtonSuspend.AutoSize = true;
            this.radioButtonSuspend.Font = new System.Drawing.Font("宋体", 9F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(134)));
            this.radioButtonSuspend.Location = new System.Drawing.Point(18, 100);
            this.radioButtonSuspend.Name = "radioButtonSuspend";
            this.radioButtonSuspend.Size = new System.Drawing.Size(49, 16);
            this.radioButtonSuspend.TabIndex = 2;
            this.radioButtonSuspend.Text = "待机";
            this.radioButtonSuspend.UseVisualStyleBackColor = true;
            //
            // radioButtonExit
            //
            this.radioButtonExit.AutoSize = true;
            this.radioButtonExit.Checked = true;
            this.radioButtonExit.Font = new System.Drawing.Font("宋体", 9F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(134)));
            this.radioButtonExit.Location = new System.Drawing.Point(78, 100);
            this.radioButtonExit.Name = "radioButtonExit";
            this.radioButtonExit.Size = new System.Drawing.Size(49, 16);
            this.radioButtonExit.TabIndex = 3;
            this.radioButtonExit.TabStop = true;
            this.radioButtonExit.Text = "关机";
            this.radioButtonExit.UseVisualStyleBackColor = true;
            //
            // radioButtonLock
            //
            this.radioButtonLock.AutoSize = true;
            this.radioButtonLock.Font = new System.Drawing.Font("宋体", 9F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(134)));
            this.radioButtonLock.Location = new System.Drawing.Point(138, 100);
            this.radioButtonLock.Name = "radioButtonLock";
            this.radioButtonLock.Size = new System.Drawing.Size(49, 16);
            this.radioButtonLock.TabIndex = 4;
            this.radioButtonLock.Text = "锁机";
            this.radioButtonLock.UseVisualStyleBackColor = true;
            //
            // buttonOK
            //
            this.buttonOK.Font = new System.Drawing.Font("宋体", 9F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(134)));
            this.buttonOK.Location = new System.Drawing.Point(15, 134);
            this.buttonOK.Name = "buttonOK";
            this.buttonOK.Size = new System.Drawing.Size(75, 23);
            this.buttonOK.TabIndex = 5;
            this.buttonOK.Text = "确定(&O)";
            this.buttonOK.UseVisualStyleBackColor = true;
            this.buttonOK.Click += new System.EventHandler(this.buttonOK_Click);
            //
            // buttonCancel
            //
            this.buttonCancel.DialogResult = System.Windows.Forms.DialogResult.Cancel;
            this.buttonCancel.Font = new System.Drawing.Font("宋体", 9F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(134)));
            this.buttonCancel.Location = new System.Drawing.Point(111, 134);
            this.buttonCancel.Name = "buttonCancel";
            this.buttonCancel.Size = new System.Drawing.Size(75, 23);
            this.buttonCancel.TabIndex = 6;
            this.buttonCancel.Text = "取消(&C)";
            this.buttonCancel.UseVisualStyleBackColor = true;
            this.buttonCancel.Click += new System.EventHandler(this.buttonCancel_Click);
            //
            // FormShutDown
            //
            this.AcceptButton = this.buttonOK;
            this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F);
            this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
            this.BackColor = System.Drawing.SystemColors.Window;
            this.CancelButton = this.buttonCancel;
            this.ClientSize = new System.Drawing.Size(200, 180);
            this.Controls.Add(this.buttonCancel);
            this.Controls.Add(this.buttonOK);
            this.Controls.Add(this.radioButtonLock);
            this.Controls.Add(this.radioButtonExit);
            this.Controls.Add(this.radioButtonSuspend);
            this.Controls.Add(this.timePicker);
            this.Controls.Add(this.labelTime);
            this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.None;
            this.Name = "FormShutDown";
            this.ResumeLayout(false);
            this.PerformLayout();
        }
        #endregion

        #region
        private System.Windows.Forms.Button buttonCancel;
        private System.Windows.Forms.Button buttonOK;
        private System.Windows.Forms.DateTimePicker timePicker;
        private System.Windows.Forms.Label labelTime;
        private System.Windows.Forms.NotifyIcon notifyInfo;
        private System.Windows.Forms.RadioButton radioButtonExit;
        private System.Windows.Forms.RadioButton radioButtonLock;
        private System.Windows.Forms.RadioButton radioButtonSuspend;
        private System.Windows.Forms.Timer timerInfo;
        #endregion
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值