文章详情

短信预约-IT技能 免费直播动态提醒

请输入下面的图形验证码

提交验证

短信预约提醒成功

C#如何编写自动关机程序

2023-06-17 05:35

关注

这篇文章主要为大家展示了“C#如何编写自动关机程序”,内容简而易懂,条理清晰,希望能够帮助大家解决疑惑,下面让小编带领大家一起研究并学习一下“C#如何编写自动关机程序”这篇文章吧。

C#如何编写自动关机程序

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

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

ICON图标可以在网上下载。

这些都是表面功夫

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

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

如下所示:

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

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就是半秒。

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

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图标。

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

1

2

3

4

5

6

private void notifyIcon1_MouseClick(object sender, MouseEventArgs e)

        {

            this.Visible = true;

            this.WindowState = FormWindowState.Normal;

            this.notifyIcon1.Visible = false;

        }

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

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

1

2

3

4

5

6

7

8

private void Form1_SizeChanged(object sender, EventArgs e)

        {

            if (this.WindowState == FormWindowState.Minimized)

            {

                this.Hide();

                this.notifyIcon1.Visible = true;

            }

        }

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

整体构架如下图所示。

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

79

80

81

82

83

84

85

86

87

88

89

90

91

92

93

94

95

96

97

98

99

100

101

102

103

104

105

106

107

108

109

110

111

112

113

114

115

116

117

118

119

120

121

122

123

124

125

126

127

128

129

130

131

132

133

134

135

136

137

138

139

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;

            }

        }

    }

}

界面如下

C#如何编写自动关机程序

以上是“C#如何编写自动关机程序”这篇文章的所有内容,感谢各位的阅读!相信大家都有了一定的了解,希望分享的内容对大家有所帮助,如果还想学习更多知识,欢迎关注编程网行业资讯频道!

阅读原文内容投诉

免责声明:

① 本站未注明“稿件来源”的信息均来自网络整理。其文字、图片和音视频稿件的所属权归原作者所有。本站收集整理出于非商业性的教育和科研之目的,并不意味着本站赞同其观点或证实其内容的真实性。仅作为临时的测试数据,供内部测试之用。本站并未授权任何人以任何方式主动获取本站任何信息。

② 本站未注明“稿件来源”的临时测试数据将在测试完成后最终做删除处理。有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341

软考中级精品资料免费领

  • 历年真题答案解析
  • 备考技巧名师总结
  • 高频考点精准押题
  • 2024年上半年信息系统项目管理师第二批次真题及答案解析(完整版)

    难度     807人已做
    查看
  • 【考后总结】2024年5月26日信息系统项目管理师第2批次考情分析

    难度     351人已做
    查看
  • 【考后总结】2024年5月25日信息系统项目管理师第1批次考情分析

    难度     314人已做
    查看
  • 2024年上半年软考高项第一、二批次真题考点汇总(完整版)

    难度     433人已做
    查看
  • 2024年上半年系统架构设计师考试综合知识真题

    难度     221人已做
    查看

相关文章

发现更多好内容

猜你喜欢

AI推送时光机
位置:首页-资讯-后端开发
咦!没有更多了?去看看其它编程学习网 内容吧
首页课程
资料下载
问答资讯