做程序员2年了,感觉视力越来越差。有时候常常工作到忘记休息。于是就想写一个能够定时提醒的小东西(公司不让从网络下载别的程序)。
功能:
1.能够每隔一段时间提醒我休息,做做眼保健操。
2.能够自己设定时间间隔.
运行环境:.net framework 2.0
使用说明:
双击托盘 重新设定。
右键托盘 可以推出程序。
主界面如图,

能够设定提醒的时间间隔,以秒记,我一般设置为1800,半个小时,默认为半个小时。
设定后点击ok,关闭主窗口,任务栏会有个托盘。
实现原理:
启动一个新线程,让其sleep所设置的时间间隔的长度,然后弹出对话框。
下面是代码文件:只有一个类TimeNotifier
有三个PartialClass TimeNotifier.cs, TimeNotifier.event.cs,TimeNotifier.Designer.cs
TimeNotifier.cs

Code
1
using System;
2
using System.Collections.Generic;
3
using System.ComponentModel;
4
using System.Data;
5
using System.Drawing;
6
using System.Text;
7
using System.Windows.Forms;
8
9
namespace TimeNotifier
10

{
11
public partial class TimeNotifier : Form
12
{
13
/**//// <summary>
14
/// Const
15
/// </summary>
16
public class ConstDefs
17
{
18
internal static int DefaultSlot = 1800;
19
internal static int OneThousant = 1000;
20
}
21
22
/**//// <summary>
23
/// Constructor
24
/// </summary>
25
public TimeNotifier()
26
{
27
InitializeComponent();
28
SetDefault();
29
RegisterEvent();
30
TimeReminder();
31
}
32
33
/**//// <summary>
34
/// Exit
35
/// </summary>
36
/// <param name="sender"></param>
37
/// <param name="e"></param>
38
private void exitToolStripMenuItem_Click(object sender, EventArgs e)
39
{
40
this.newTimeThread.Abort();
41
Application.Exit();
42
}
43
44
/**//// <summary>
45
/// Reset
46
/// </summary>
47
/// <param name="sender"></param>
48
/// <param name="e"></param>
49
private void resetToolStripMenuItem_Click(object sender, EventArgs e)
50
{
51
this.Show();
52
}
53
54
private void btnCancel_Click(object sender, EventArgs e)
55
{
56
this.Hide();
57
}
58
}
59
}
TimeNotifier.event.cs

Code
1
using System;
2
using System.Collections.Generic;
3
using System.Text;
4
using System.Drawing;
5
using System.ComponentModel;
6
using System.Windows.Forms;
7
using System.Threading;
8
9
namespace TimeNotifier
10

{
11
public partial class TimeNotifier
12
{
13
/**//// <summary>
14
/// 注册事件.
15
/// </summary>
16
private void RegisterEvent()
17
{
18
this.Closing += new System.ComponentModel.CancelEventHandler(Tray_Closing);
19
this.Apply.Click += new EventHandler(Apply_Click);
20
}
21
22
/**//// <summary>
23
/// Closing
24
/// </summary>
25
/// <param name="sender"></param>
26
/// <param name="e"></param>
27
private void Tray_Closing(object sender, CancelEventArgs e)
28
{
29
this.Hide();
30
e.Cancel = true;
31
}
32
33
/**//// <summary>
34
/// Reset
35
/// </summary>
36
/// <param name="sender"></param>
37
/// <param name="e"></param>
38
private void Apply_Click(object sender, EventArgs e)
39
{
40
this.timeSlot = Int32.Parse(this.textBox1.Text);
41
this.newTimeThread.Abort();
42
this.TimeReminder();
43
}
44
45
46
/**//// <summary>
47
/// Set Tray
48
/// </summary>
49
private void SetDefault()
50
{
51
timeSlot = ConstDefs.DefaultSlot;
52
this.textBox1.Text = timeSlot.ToString();
53
}
54
55
/**//// <summary>
56
/// 启动新线程.
57
/// </summary>
58
private void TimeReminder()
59
{
60
if (newTimeThread != null)
61
{
62
newTimeThread.Abort();
63
}
64
newTimeThread = new Thread(new ThreadStart(TimeNotice));
65
newTimeThread.Start();
66
}
67
68
private void TimeNotice()
69
{
70
while (true)
71
{
72
Thread.Sleep(this.timeSlot * ConstDefs.OneThousant);
73
MessageBox.Show("Time to have a rest" + DateTime.Now.ToString());
74
75
}
76
}
77
}
78
79
80
}
TimeNotifier.Designer.cs

Code
1
using System;
2
using System.Collections.Generic;
3
using System.ComponentModel;
4
using System.Data;
5
using System.Drawing;
6
using System.Text;
7
using System.Windows.Forms;
8
using System.Threading;
9
10
namespace TimeNotifier
11

{
12
public partial class TimeNotifier
13
{
14
/**//// <summary>
15
/// Required designer variable.
16
/// </summary>
17
private System.ComponentModel.IContainer components = null;
18
private NotifyIcon TrayIcon;
19
private ContextMenu TrayMenu;
20
private MenuItem subMenuExit;
21
private MenuItem subMenuReset;
22
private int timeSlot;
23
private Thread newTimeThread;
24
25
/**//// <summary>
26
/// Clean up any resources being used.
27
/// </summary>
28
/// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
29
protected override void Dispose(bool disposing)
30
{
31
if (disposing && (components != null))
32
{
33
components.Dispose();
34
}
35
base.Dispose(disposing);
36
}
37
38
Windows Form Designer generated code#region Windows Form Designer generated code
39
40
/**//// <summary>
41
/// Required method for Designer support - do not modify
42
/// the contents of this method with the code editor.
43
/// </summary>
44
private void InitializeComponent()
45
{
46
this.components = new System.ComponentModel.Container();
47
System.ComponentModel.ComponentResourceManager resources = new System.ComponentModel.ComponentResourceManager(typeof(TimeNotifier));
48
this.Apply = new System.Windows.Forms.Button();
49
this.btnCancel = new System.Windows.Forms.Button();
50
this.gbSetting = new System.Windows.Forms.GroupBox();
51
this.label1 = new System.Windows.Forms.Label();
52
this.lblInfo = new System.Windows.Forms.Label();
53
this.textBox1 = new System.Windows.Forms.TextBox();
54
this.notifyIcon1 = new System.Windows.Forms.NotifyIcon(this.components);
55
this.contextMenuStrip1 = new System.Windows.Forms.ContextMenuStrip(this.components);
56
this.resetToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
57
this.exitToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
58
this.gbSetting.SuspendLayout();
59
this.contextMenuStrip1.SuspendLayout();
60
this.SuspendLayout();
61
//
62
// Apply
63
//
64
this.Apply.Location = new System.Drawing.Point(47, 173);
65
this.Apply.Name = "Apply";
66
this.Apply.Size = new System.Drawing.Size(75, 25);
67
this.Apply.TabIndex = 1;
68
this.Apply.Text = "OK";
69
this.Apply.UseVisualStyleBackColor = true;
70
//
71
// btnCancel
72
//
73
this.btnCancel.Location = new System.Drawing.Point(151, 173);
74
this.btnCancel.Name = "btnCancel";
75
this.btnCancel.Size = new System.Drawing.Size(75, 25);
76
this.btnCancel.TabIndex = 2;
77
this.btnCancel.Text = "Cancel";
78
this.btnCancel.UseVisualStyleBackColor = true;
79
this.btnCancel.Click += new System.EventHandler(this.btnCancel_Click);
80
//
81
// gbSetting
82
//
83
this.gbSetting.Controls.Add(this.label1);
84
this.gbSetting.Controls.Add(this.lblInfo);
85
this.gbSetting.Controls.Add(this.textBox1);
86
this.gbSetting.Location = new System.Drawing.Point(-2, 2);
87
this.gbSetting.Name = "gbSetting";
88
this.gbSetting.Size = new System.Drawing.Size(274, 85);
89
this.gbSetting.TabIndex = 4;
90
this.gbSetting.TabStop = false;
91
this.gbSetting.Text = "设置";
92
//
93
// label1
94
//
95
this.label1.AutoSize = true;
96
this.label1.Location = new System.Drawing.Point(124, 52);
97
this.label1.Name = "label1";
98
this.label1.Size = new System.Drawing.Size(19, 13);
99
this.label1.TabIndex = 6;
100
this.label1.Text = "秒";
101
//
102
// lblInfo
103
//
104
this.lblInfo.AutoSize = true;
105
this.lblInfo.Location = new System.Drawing.Point(47, 21);
106
this.lblInfo.Name = "lblInfo";
107
this.lblInfo.Size = new System.Drawing.Size(100, 13);
108
this.lblInfo.TabIndex = 5;
109
this.lblInfo.Text = "提醒间隔时间(秒):";
110
//
111
// textBox1
112
//
113
this.textBox1.Location = new System.Drawing.Point(49, 49);
114
this.textBox1.Name = "textBox1";
115
this.textBox1.Size = new System.Drawing.Size(69, 20);
116
this.textBox1.TabIndex = 4;
117
//
118
// notifyIcon1
119
//
120
this.notifyIcon1.ContextMenuStrip = this.contextMenuStrip1;
121
this.notifyIcon1.Icon = ((System.Drawing.Icon)(resources.GetObject("notifyIcon1.Icon")));
122
this.notifyIcon1.Text = "定时提醒";
123
this.notifyIcon1.Visible = true;
124
this.notifyIcon1.DoubleClick += new System.EventHandler(this.resetToolStripMenuItem_Click);
125
//
126
// contextMenuStrip1
127
//
128
this.contextMenuStrip1.Items.AddRange(new System.Windows.Forms.ToolStripItem[]
{
129
this.resetToolStripMenuItem,
130
this.exitToolStripMenuItem});
131
this.contextMenuStrip1.Name = "contextMenuStrip1";
132
this.contextMenuStrip1.RenderMode = System.Windows.Forms.ToolStripRenderMode.Professional;
133
this.contextMenuStrip1.Size = new System.Drawing.Size(153, 70);
134
//
135
// resetToolStripMenuItem
136
//
137
this.resetToolStripMenuItem.Image = ((System.Drawing.Image)(resources.GetObject("resetToolStripMenuItem.Image")));
138
this.resetToolStripMenuItem.Name = "resetToolStripMenuItem";
139
this.resetToolStripMenuItem.Size = new System.Drawing.Size(152, 22);
140
this.resetToolStripMenuItem.Text = "Reset";
141
this.resetToolStripMenuItem.Click += new System.EventHandler(this.resetToolStripMenuItem_Click);
142
//
143
// exitToolStripMenuItem
144
//
145
this.exitToolStripMenuItem.Name = "exitToolStripMenuItem";
146
this.exitToolStripMenuItem.Size = new System.Drawing.Size(152, 22);
147
this.exitToolStripMenuItem.Text = "Exit";
148
this.exitToolStripMenuItem.Click += new System.EventHandler(this.exitToolStripMenuItem_Click);
149
//
150
// TimeNotifier
151
//
152
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
153
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
154
this.ClientSize = new System.Drawing.Size(272, 224);
155
this.Controls.Add(this.gbSetting);
156
this.Controls.Add(this.btnCancel);
157
this.Controls.Add(this.Apply);
158
this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedToolWindow;
159
this.Icon = ((System.Drawing.Icon)(resources.GetObject("$this.Icon")));
160
this.MinimizeBox = false;
161
this.Name = "TimeNotifier";
162
this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
163
this.Text = "定时设置";
164
this.gbSetting.ResumeLayout(false);
165
this.gbSetting.PerformLayout();
166
this.contextMenuStrip1.ResumeLayout(false);
167
this.ResumeLayout(false);
168
169
}
170
171
172
173
#endregion
174
175
private Button Apply;
176
private Button btnCancel;
177
private GroupBox gbSetting;
178
private Label lblInfo;
179
private TextBox textBox1;
180
private Label label1;
181
private NotifyIcon notifyIcon1;
182
private ContextMenuStrip contextMenuStrip1;
183
private ToolStripMenuItem exitToolStripMenuItem;
184
private ToolStripMenuItem resetToolStripMenuItem;
185
186
}
187
}
188