zoukankan      html  css  js  c++  java
  • 自动关机代码

    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Linq;
    using System.Text;
    using System.Windows.Forms;
    using System.Runtime.InteropServices;
    
    namespace AutoShutDown
    {
        public partial class Form1 : Form
        {
            public Form1()
            {
                InitializeComponent();
                this.btnStop.Enabled = false;
            }
    
            int count = 0;
            int maxCount = 0;
            private void btnStart_Click(object sender, EventArgs e)
            {
                maxCount = (int)this.udTimeout.Value;
                count = 0;
                this.timer1.Interval = 60000;
                this.timer1.Start();
                this.btnStart.Enabled = this.udTimeout.Enabled = false;
                this.btnStop.Enabled = true;
                this.lblInfo.Text = string.Format("{0}分钟", maxCount);
            }
    
            private void btnStop_Click(object sender, EventArgs e)
            {
                this.timer1.Stop();
                this.btnStart.Enabled = this.udTimeout.Enabled = true;
                this.btnStop.Enabled = false;
            }
    
            private void timer1_Tick(object sender, EventArgs e)
            {
                count++;
                if (count >= maxCount)
                {
                    this.timer1.Stop();
                    ShutDown.DoExitWin(ShutDown.EWX_SHUTDOWN);
                }
                this.lblInfo.Text = string.Format("{0}分钟", maxCount - count);
            }
        }
    
        public class ShutDown
        {
            [StructLayout(LayoutKind.Sequential, Pack = 1)]
            internal struct TokPriv1Luid
            {
                public int Count;
                public long Luid;
                public int Attr;
            }
    
            [DllImport("kernel32.dll", ExactSpelling = true)]
            internal static extern IntPtr GetCurrentProcess();
    
            [DllImport("advapi32.dll", ExactSpelling = true, SetLastError = true)]
            internal static extern bool OpenProcessToken(IntPtr h, int acc, ref IntPtr phtok);
    
            [DllImport("advapi32.dll", SetLastError = true)]
            internal static extern bool LookupPrivilegeValue(string host, string name, ref long pluid);
    
            [DllImport("advapi32.dll", ExactSpelling = true, SetLastError = true)]
            internal static extern bool AdjustTokenPrivileges(IntPtr htok, bool disall, ref TokPriv1Luid newst, int len, IntPtr prev, IntPtr relen);
    
            [DllImport("user32.dll", ExactSpelling = true, SetLastError = true)]
            internal static extern bool ExitWindowsEx(int flg, int rea);
    
            internal const int SE_PRIVILEGE_ENABLED = 0x00000002;
            internal const int TOKEN_QUERY = 0x00000008;
            internal const int TOKEN_ADJUST_PRIVILEGES = 0x00000020;
            internal const string SE_SHUTDOWN_NAME = "SeShutdownPrivilege";
            internal const int EWX_LOGOFF = 0x00000000;
            internal const int EWX_SHUTDOWN = 0x00000001;
            internal const int EWX_REBOOT = 0x00000002;
            internal const int EWX_FORCE = 0x00000004;
            internal const int EWX_POWEROFF = 0x00000008;
            internal const int EWX_FORCEIFHUNG = 0x00000010;
    
            public static void DoExitWin(int flg)
            {
                bool ok;
                TokPriv1Luid tp;
                IntPtr hproc = GetCurrentProcess();
                IntPtr htok = IntPtr.Zero;
                ok = OpenProcessToken(hproc, TOKEN_ADJUST_PRIVILEGES | TOKEN_QUERY, ref htok);
                tp.Count = 1;
                tp.Luid = 0;
                tp.Attr = SE_PRIVILEGE_ENABLED;
                ok = LookupPrivilegeValue(null, SE_SHUTDOWN_NAME, ref tp.Luid);
                ok = AdjustTokenPrivileges(htok, false, ref tp, 0, IntPtr.Zero, IntPtr.Zero);
                ok = ExitWindowsEx(flg, 0);
            }
        }
    
    }
    namespace AutoShutDown
    {
        partial class Form1
        {
            /// <summary>
            /// 必需的设计器变量。
            /// </summary>
            private System.ComponentModel.IContainer components = null;
    
            /// <summary>
            /// 清理所有正在使用的资源。
            /// </summary>
            /// <param name="disposing">如果应释放托管资源,为 true;否则为 false。</param>
            protected override void Dispose(bool disposing)
            {
                if (disposing && (components != null))
                {
                    components.Dispose();
                }
                base.Dispose(disposing);
            }
    
            #region Windows 窗体设计器生成的代码
    
            /// <summary>
            /// 设计器支持所需的方法 - 不要
            /// 使用代码编辑器修改此方法的内容。
            /// </summary>
            private void InitializeComponent()
            {
                this.components = new System.ComponentModel.Container();
                this.label1 = new System.Windows.Forms.Label();
                this.udTimeout = new System.Windows.Forms.NumericUpDown();
                this.btnStart = new System.Windows.Forms.Button();
                this.btnStop = new System.Windows.Forms.Button();
                this.timer1 = new System.Windows.Forms.Timer(this.components);
                this.label2 = new System.Windows.Forms.Label();
                this.lblInfo = new System.Windows.Forms.Label();
                ((System.ComponentModel.ISupportInitialize)(this.udTimeout)).BeginInit();
                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(65, 12);
                this.label1.TabIndex = 0;
                this.label1.Text = "设定时间:";
                // 
                // udTimeout
                // 
                this.udTimeout.Location = new System.Drawing.Point(83, 7);
                this.udTimeout.Name = "udTimeout";
                this.udTimeout.Size = new System.Drawing.Size(120, 21);
                this.udTimeout.TabIndex = 1;
                this.udTimeout.Value = new decimal(new int[] {
                30,
                0,
                0,
                0});
                // 
                // btnStart
                // 
                this.btnStart.Location = new System.Drawing.Point(246, 6);
                this.btnStart.Name = "btnStart";
                this.btnStart.Size = new System.Drawing.Size(75, 23);
                this.btnStart.TabIndex = 2;
                this.btnStart.Text = "开始";
                this.btnStart.UseVisualStyleBackColor = true;
                this.btnStart.Click += new System.EventHandler(this.btnStart_Click);
                // 
                // btnStop
                // 
                this.btnStop.Location = new System.Drawing.Point(246, 35);
                this.btnStop.Name = "btnStop";
                this.btnStop.Size = new System.Drawing.Size(75, 23);
                this.btnStop.TabIndex = 3;
                this.btnStop.Text = "终止";
                this.btnStop.UseVisualStyleBackColor = true;
                this.btnStop.Click += new System.EventHandler(this.btnStop_Click);
                // 
                // timer1
                // 
                this.timer1.Tick += new System.EventHandler(this.timer1_Tick);
                // 
                // label2
                // 
                this.label2.AutoSize = true;
                this.label2.Location = new System.Drawing.Point(12, 40);
                this.label2.Name = "label2";
                this.label2.Size = new System.Drawing.Size(77, 12);
                this.label2.TabIndex = 4;
                this.label2.Text = "离关机还剩:";
                // 
                // lblInfo
                // 
                this.lblInfo.AutoSize = true;
                this.lblInfo.Location = new System.Drawing.Point(150, 40);
                this.lblInfo.Name = "lblInfo";
                this.lblInfo.Size = new System.Drawing.Size(53, 12);
                this.lblInfo.TabIndex = 5;
                this.lblInfo.Text = "剩余时间";
                // 
                // Form1
                // 
                this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F);
                this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
                this.ClientSize = new System.Drawing.Size(340, 64);
                this.Controls.Add(this.lblInfo);
                this.Controls.Add(this.label2);
                this.Controls.Add(this.btnStop);
                this.Controls.Add(this.btnStart);
                this.Controls.Add(this.udTimeout);
                this.Controls.Add(this.label1);
                this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.Fixed3D;
                this.MaximizeBox = false;
                this.Name = "Form1";
                this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
                this.Text = "Form1";
                ((System.ComponentModel.ISupportInitialize)(this.udTimeout)).EndInit();
                this.ResumeLayout(false);
                this.PerformLayout();
    
            }
    
            #endregion
    
            private System.Windows.Forms.Label label1;
            private System.Windows.Forms.NumericUpDown udTimeout;
            private System.Windows.Forms.Button btnStart;
            private System.Windows.Forms.Button btnStop;
            private System.Windows.Forms.Timer timer1;
            private System.Windows.Forms.Label label2;
            private System.Windows.Forms.Label lblInfo;
        }
    }
  • 相关阅读:
    javascript循环结构练习
    个人复习JavaScript重点(总结一)
    第十章汽车租凭系统
    员工执行
    第六章预习
    第五章使用Dictionary替换List<t>实现功能
    第5章体检套餐管理系统
    JAVA面试题
    AJAX
    SSM中的登陆验证码
  • 原文地址:https://www.cnblogs.com/AndyGe/p/3168821.html
Copyright © 2011-2022 走看看