• C#编写自动关机程序复习的知识


    首先一个程序第一要素是logo

    在设置里面可以设置程序图标,在ICON里设置。

    ICON图标可以在网上下载。

    这些都是表面功夫

    程序中涉及到Buton、Label、Timer、Notiflcon控件

    Button按钮控件,可以设计点击事件

    如下所示:

    private void button1_Click(object sender, EventArgs e)
            {
               // int shi, fen, miao;
                if (Flag_True == 0)
                {
                    Flag_True = 1;
                }
                else
                {
                    button1.Text = "确定";
                    label6.Text = " ";
                    label7.Text = " ";
                    label5.Text = " ";
                    //label1.Text = "定时关机设置";
                    Flag_True = 0;
                }
                shi = (int)numericUpDown3.Value;
                fen = (int)numericUpDown2.Value;
                miao = (int)numericUpDown1.Value;
                time_set = shi * 3600 + fen * 60 + miao;
            }
    

     label控件操作简单

    能够显示字符,并且其成员有text,可以随时更改文本

    timer控件相当于嵌入式中的定时器,在属性中行为一栏中设置ENABLE 并且设置interval时间间隔500就是半秒。

    private void timer1_Tick(object sender, EventArgs e)
            {
                Int32 time_now;
                Int32 extra;
                if (Flag_True == 1)
                {
                    if (DateTime.Now.Minute == fen && DateTime.Now.Hour == shi && DateTime.Now.Second == miao)
                    {
                        button1.Text = "取消";
                        label6.Text = "剩余关机时间";
                        label7.Text = "秒";
                        label5.Text = "0";
                        System.Diagnostics.Process.Start("shutdown","-s -t 0");//关机程序
                    }//shutdown
                    else
                    {
                        time_now = DateTime.Now.Second + DateTime.Now.Minute * 60 + DateTime.Now.Hour * 3600;
                        extra = time_set - time_now;
                        if (extra > 0)
                        {
                            button1.Text = "取消";
                            label6.Text = "剩余关机时间";
                            label7.Text = "秒";
                            //extra/3600
                            label5.Text = extra.ToString();
                        }
                        else
                        {
                            Flag_True = 0;
                        }
                            
                        
                    }
                }
            }
    

     上面我每隔半秒进入中断一次,判断,如果已经设置过定时关机,就判断是否到达关机时间,并显示还剩多少秒关机。如果没有设置定时关机,就不显示。

    其中button1和Label的text都可以随时更改。

    基本功能设置完成

    接下来还有一个最小化到托盘的设置

    用到Notiflcon控件

    此控件设置最小化图标,在设置里可以设置icon图标。

    他带有的事件有鼠标单击,鼠标双击,单击,双击。

    private void notifyIcon1_MouseClick(object sender, MouseEventArgs e)
            {
                this.Visible = true; 
                this.WindowState = FormWindowState.Normal; 
                this.notifyIcon1.Visible = false; 
            }
    

     上述我设置了鼠标单击,代码里是恢复可视化,正常窗口。

    再之得设置程序最小化时隐藏在下边

    private void Form1_SizeChanged(object sender, EventArgs e)
            {
                if (this.WindowState == FormWindowState.Minimized) 
                { 
                    this.Hide(); 
                    this.notifyIcon1.Visible = true; 
                } 
            }
    

     上述就是一个关机程序,自己做着玩的。。

    整体构架如下图所示。

    namespace 关机任务管理V1._0
    {
        public partial class Form1 : Form
        {
            int shi, fen, miao;
            Int32 time_set;
            int Flag_True = 0;
            public Form1()
            {
                
                InitializeComponent();
    
            }
    
            private void Form1_Load(object sender, EventArgs e)
            {
                
            }
    
            private void numericUpDown1_ValueChanged(object sender, EventArgs e)
            {
                if (numericUpDown1.Value == -1)
                    numericUpDown1.Value = 60;
                else if (numericUpDown1.Value == 61)
                    numericUpDown1.Value = 0;
            }
    
            private void numericUpDown2_ValueChanged(object sender, EventArgs e)
            {
                if (numericUpDown2.Value == -1)
                    numericUpDown2.Value = 60;
                else if (numericUpDown2.Value == 61)
                    numericUpDown2.Value = 0;
            
            }
    
            private void numericUpDown3_ValueChanged(object sender, EventArgs e)
            {
                if (numericUpDown3.Value == 25)
                    numericUpDown3.Value = 0;
                else if (numericUpDown3.Value == -1)
                    numericUpDown3.Value = 24;
            }
    
            private void button1_Click(object sender, EventArgs e)
            {
               // int shi, fen, miao;
                if (Flag_True == 0)
                {
                    Flag_True = 1;
                }
                else
                {
                    button1.Text = "确定";
                    label6.Text = " ";
                    label7.Text = " ";
                    label5.Text = " ";
                    //label1.Text = "定时关机设置";
                    Flag_True = 0;
                }
                shi = (int)numericUpDown3.Value;
                fen = (int)numericUpDown2.Value;
                miao = (int)numericUpDown1.Value;
                time_set = shi * 3600 + fen * 60 + miao;
            }
    
            private void timer1_Tick(object sender, EventArgs e)
            {
                Int32 time_now;
                Int32 extra;
                if (Flag_True == 1)
                {
                    if (DateTime.Now.Minute == fen && DateTime.Now.Hour == shi && DateTime.Now.Second == miao)
                    {
                        button1.Text = "取消";
                        label6.Text = "剩余关机时间";
                        label7.Text = "秒";
                        label5.Text = "0";
                        System.Diagnostics.Process.Start("shutdown","-s -t 0");//关机程序
                    }//shutdown
                    else
                    {
                        time_now = DateTime.Now.Second + DateTime.Now.Minute * 60 + DateTime.Now.Hour * 3600;
                        extra = time_set - time_now;
                        if (extra > 0)
                        {
                            button1.Text = "取消";
                            label6.Text = "剩余关机时间";
                            label7.Text = "秒";
                            //extra/3600
                            label5.Text = extra.ToString();
                        }
                        else
                        {
                            Flag_True = 0;
                        }
                            
                        
                    }
                }
            }
    
            private void label5_Click(object sender, EventArgs e)
            {
    
            }
    
            private void label4_Click(object sender, EventArgs e)
            {
    
            }
    
            private void label3_Click(object sender, EventArgs e)
            {
    
            }
    
            private void label6_Click(object sender, EventArgs e)
            {
    
            }
    
            private void notifyIcon1_MouseClick(object sender, MouseEventArgs e)
            {
                this.Visible = true; 
                this.WindowState = FormWindowState.Normal; 
                this.notifyIcon1.Visible = false; 
            }
            //最小化代码
            private void Form1_SizeChanged(object sender, EventArgs e)
            {
                if (this.WindowState == FormWindowState.Minimized) 
                { 
                    this.Hide(); 
                    this.notifyIcon1.Visible = true; 
                } 
            }
        }
    }
    

     界面如下:

  • 相关阅读:
    java面试第八天
    java面试第七天
    java面试第六天
    java面试第五天
    java面试第四天
    SpringMVC导出Excel
    75. Autorelease机制及释放时机
    关于 SQLNET.AUTHENTICATION_SERVICES 验证方式的说明
    硬件十万个为什么——运放篇(五)PCB设计技巧
    eclipse到Android Studio的项目迁移
  • 原文地址:https://www.cnblogs.com/flyingjun/p/4790059.html
Copyright © 2020-2023  润新知