diff --git a/Reminder/MainFrm.Designer.cs b/Reminder/MainFrm.Designer.cs index 880a354..ee09ddf 100644 --- a/Reminder/MainFrm.Designer.cs +++ b/Reminder/MainFrm.Designer.cs @@ -42,10 +42,11 @@ private void InitializeComponent() this.notifyIcon1 = new System.Windows.Forms.NotifyIcon(this.components); this.contextMenuStrip1 = new System.Windows.Forms.ContextMenuStrip(this.components); this.主窗体ToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem(); - this.exit_ToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem(); this.关于ToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem(); + this.exit_ToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem(); this.label4 = new System.Windows.Forms.Label(); this.ckBoxInput = new System.Windows.Forms.CheckBox(); + this.checkBox1 = new System.Windows.Forms.CheckBox(); ((System.ComponentModel.ISupportInitialize)(this.numWrkTime)).BeginInit(); ((System.ComponentModel.ISupportInitialize)(this.numRstTime)).BeginInit(); this.contextMenuStrip1.SuspendLayout(); @@ -54,9 +55,10 @@ private void InitializeComponent() // label1 // this.label1.AutoSize = true; - this.label1.Location = new System.Drawing.Point(26, 50); + this.label1.Location = new System.Drawing.Point(35, 62); + this.label1.Margin = new System.Windows.Forms.Padding(4, 0, 4, 0); this.label1.Name = "label1"; - this.label1.Size = new System.Drawing.Size(65, 12); + this.label1.Size = new System.Drawing.Size(82, 15); this.label1.TabIndex = 0; this.label1.Text = "工作时间:"; // @@ -64,18 +66,20 @@ private void InitializeComponent() // this.label2.AutoSize = true; this.label2.Font = new System.Drawing.Font("宋体", 10.5F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(134))); - this.label2.Location = new System.Drawing.Point(11, 19); + this.label2.Location = new System.Drawing.Point(15, 24); + this.label2.Margin = new System.Windows.Forms.Padding(4, 0, 4, 0); this.label2.Name = "label2"; - this.label2.Size = new System.Drawing.Size(67, 14); + this.label2.Size = new System.Drawing.Size(84, 18); this.label2.TabIndex = 3; this.label2.Text = "计时器:"; // // label3 // this.label3.AutoSize = true; - this.label3.Location = new System.Drawing.Point(26, 77); + this.label3.Location = new System.Drawing.Point(35, 96); + this.label3.Margin = new System.Windows.Forms.Padding(4, 0, 4, 0); this.label3.Name = "label3"; - this.label3.Size = new System.Drawing.Size(65, 12); + this.label3.Size = new System.Drawing.Size(82, 15); this.label3.TabIndex = 5; this.label3.Text = "休息时间:"; // @@ -84,9 +88,10 @@ private void InitializeComponent() this.btn_start.BackColor = System.Drawing.Color.Transparent; this.btn_start.FlatAppearance.BorderColor = System.Drawing.Color.DimGray; this.btn_start.FlatStyle = System.Windows.Forms.FlatStyle.Flat; - this.btn_start.Location = new System.Drawing.Point(71, 178); + this.btn_start.Location = new System.Drawing.Point(96, 240); + this.btn_start.Margin = new System.Windows.Forms.Padding(4, 4, 4, 4); this.btn_start.Name = "btn_start"; - this.btn_start.Size = new System.Drawing.Size(75, 23); + this.btn_start.Size = new System.Drawing.Size(100, 29); this.btn_start.TabIndex = 11; this.btn_start.Text = "开始"; this.btn_start.UseVisualStyleBackColor = false; @@ -95,32 +100,35 @@ private void InitializeComponent() // label5 // this.label5.AutoSize = true; - this.label5.Location = new System.Drawing.Point(146, 51); + this.label5.Location = new System.Drawing.Point(195, 64); + this.label5.Margin = new System.Windows.Forms.Padding(4, 0, 4, 0); this.label5.Name = "label5"; - this.label5.Size = new System.Drawing.Size(53, 12); + this.label5.Size = new System.Drawing.Size(67, 15); this.label5.TabIndex = 14; this.label5.Text = "(分钟)"; // // label6 // this.label6.AutoSize = true; - this.label6.Location = new System.Drawing.Point(146, 78); + this.label6.Location = new System.Drawing.Point(195, 98); + this.label6.Margin = new System.Windows.Forms.Padding(4, 0, 4, 0); this.label6.Name = "label6"; - this.label6.Size = new System.Drawing.Size(53, 12); + this.label6.Size = new System.Drawing.Size(67, 15); this.label6.TabIndex = 15; this.label6.Text = "(分钟)"; // // numWrkTime // this.numWrkTime.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle; - this.numWrkTime.Location = new System.Drawing.Point(96, 47); + this.numWrkTime.Location = new System.Drawing.Point(128, 59); + this.numWrkTime.Margin = new System.Windows.Forms.Padding(4, 4, 4, 4); this.numWrkTime.Maximum = new decimal(new int[] { 120, 0, 0, 0}); this.numWrkTime.Name = "numWrkTime"; - this.numWrkTime.Size = new System.Drawing.Size(51, 21); + this.numWrkTime.Size = new System.Drawing.Size(68, 25); this.numWrkTime.TabIndex = 16; this.numWrkTime.Value = new decimal(new int[] { 60, @@ -132,14 +140,15 @@ private void InitializeComponent() // this.numRstTime.BackColor = System.Drawing.Color.White; this.numRstTime.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle; - this.numRstTime.Location = new System.Drawing.Point(95, 74); + this.numRstTime.Location = new System.Drawing.Point(127, 92); + this.numRstTime.Margin = new System.Windows.Forms.Padding(4, 4, 4, 4); this.numRstTime.Maximum = new decimal(new int[] { 30, 0, 0, 0}); this.numRstTime.Name = "numRstTime"; - this.numRstTime.Size = new System.Drawing.Size(51, 21); + this.numRstTime.Size = new System.Drawing.Size(68, 25); this.numRstTime.TabIndex = 17; this.numRstTime.Value = new decimal(new int[] { 5, @@ -157,41 +166,43 @@ private void InitializeComponent() // contextMenuStrip1 // this.contextMenuStrip1.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Center; + this.contextMenuStrip1.ImageScalingSize = new System.Drawing.Size(20, 20); this.contextMenuStrip1.Items.AddRange(new System.Windows.Forms.ToolStripItem[] { this.主窗体ToolStripMenuItem, this.关于ToolStripMenuItem, this.exit_ToolStripMenuItem}); this.contextMenuStrip1.Name = "contextMenuStrip1"; - this.contextMenuStrip1.Size = new System.Drawing.Size(181, 92); + this.contextMenuStrip1.Size = new System.Drawing.Size(124, 76); // // 主窗体ToolStripMenuItem // this.主窗体ToolStripMenuItem.Name = "主窗体ToolStripMenuItem"; - this.主窗体ToolStripMenuItem.Size = new System.Drawing.Size(180, 22); + this.主窗体ToolStripMenuItem.Size = new System.Drawing.Size(123, 24); this.主窗体ToolStripMenuItem.Text = "首选项"; this.主窗体ToolStripMenuItem.Click += new System.EventHandler(this.主窗体ToolStripMenuItem_Click); // - // exit_ToolStripMenuItem - // - this.exit_ToolStripMenuItem.Name = "exit_ToolStripMenuItem"; - this.exit_ToolStripMenuItem.Size = new System.Drawing.Size(180, 22); - this.exit_ToolStripMenuItem.Text = "退出"; - this.exit_ToolStripMenuItem.Click += new System.EventHandler(this.退出ToolStripMenuItem_Click); - // // 关于ToolStripMenuItem // this.关于ToolStripMenuItem.Name = "关于ToolStripMenuItem"; - this.关于ToolStripMenuItem.Size = new System.Drawing.Size(180, 22); + this.关于ToolStripMenuItem.Size = new System.Drawing.Size(123, 24); this.关于ToolStripMenuItem.Text = "关于"; this.关于ToolStripMenuItem.Click += new System.EventHandler(this.关于ToolStripMenuItem_Click); // + // exit_ToolStripMenuItem + // + this.exit_ToolStripMenuItem.Name = "exit_ToolStripMenuItem"; + this.exit_ToolStripMenuItem.Size = new System.Drawing.Size(123, 24); + this.exit_ToolStripMenuItem.Text = "退出"; + this.exit_ToolStripMenuItem.Click += new System.EventHandler(this.退出ToolStripMenuItem_Click); + // // label4 // this.label4.AutoSize = true; this.label4.Font = new System.Drawing.Font("宋体", 10.5F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(134))); - this.label4.Location = new System.Drawing.Point(11, 117); + this.label4.Location = new System.Drawing.Point(15, 146); + this.label4.Margin = new System.Windows.Forms.Padding(4, 0, 4, 0); this.label4.Name = "label4"; - this.label4.Size = new System.Drawing.Size(52, 14); + this.label4.Size = new System.Drawing.Size(65, 18); this.label4.TabIndex = 18; this.label4.Text = "选项:"; // @@ -200,19 +211,35 @@ private void InitializeComponent() this.ckBoxInput.AutoSize = true; this.ckBoxInput.Checked = true; this.ckBoxInput.CheckState = System.Windows.Forms.CheckState.Checked; - this.ckBoxInput.Location = new System.Drawing.Point(28, 144); + this.ckBoxInput.Location = new System.Drawing.Point(37, 180); + this.ckBoxInput.Margin = new System.Windows.Forms.Padding(4, 4, 4, 4); this.ckBoxInput.Name = "ckBoxInput"; - this.ckBoxInput.Size = new System.Drawing.Size(144, 16); + this.ckBoxInput.Size = new System.Drawing.Size(179, 19); this.ckBoxInput.TabIndex = 19; this.ckBoxInput.Text = "休息时屏蔽键盘和鼠标"; this.ckBoxInput.UseVisualStyleBackColor = true; // + // checkBox1 + // + this.checkBox1.AutoSize = true; + this.checkBox1.Checked = true; + this.checkBox1.CheckState = System.Windows.Forms.CheckState.Checked; + this.checkBox1.Location = new System.Drawing.Point(38, 207); + this.checkBox1.Margin = new System.Windows.Forms.Padding(4); + this.checkBox1.Name = "checkBox1"; + this.checkBox1.Size = new System.Drawing.Size(179, 19); + this.checkBox1.TabIndex = 20; + this.checkBox1.Text = "休息时屏蔽键盘和鼠标"; + this.checkBox1.UseVisualStyleBackColor = true; + this.checkBox1.CheckedChanged += new System.EventHandler(this.checkBox1_CheckedChanged); + // // MainFrm // - this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F); + this.AutoScaleDimensions = new System.Drawing.SizeF(8F, 15F); this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; this.BackColor = System.Drawing.Color.White; - this.ClientSize = new System.Drawing.Size(213, 226); + this.ClientSize = new System.Drawing.Size(284, 282); + this.Controls.Add(this.checkBox1); this.Controls.Add(this.ckBoxInput); this.Controls.Add(this.label4); this.Controls.Add(this.numRstTime); @@ -224,6 +251,7 @@ private void InitializeComponent() this.Controls.Add(this.label2); this.Controls.Add(this.label1); this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedDialog; + this.Margin = new System.Windows.Forms.Padding(4, 4, 4, 4); this.MaximizeBox = false; this.MinimizeBox = false; this.Name = "MainFrm"; @@ -258,5 +286,6 @@ private void InitializeComponent() private System.Windows.Forms.ToolStripMenuItem 关于ToolStripMenuItem; private System.Windows.Forms.Label label4; private System.Windows.Forms.CheckBox ckBoxInput; + private System.Windows.Forms.CheckBox checkBox1; } } \ No newline at end of file diff --git a/Reminder/MainFrm.cs b/Reminder/MainFrm.cs index f90efdd..4b51e68 100644 --- a/Reminder/MainFrm.cs +++ b/Reminder/MainFrm.cs @@ -6,6 +6,9 @@ using System.Linq; using System.Text; using System.Windows.Forms; +using System.IO; +using System.Diagnostics; +using IWshRuntimeLibrary; namespace Reminder { @@ -35,6 +38,10 @@ private void Btn_start_Click(object sender, EventArgs e) input_flag = false; } + bool auto_start; + auto_start = checkBox1.Checked; + SetAutoStart(auto_start); + int wrkTime = (int)this.numWrkTime.Value; int rstTime = (int)this.numRstTime.Value; wrkFrm = new WorkFrm(wrkTime,rstTime,input_flag); @@ -78,5 +85,107 @@ private void 关于ToolStripMenuItem_Click(object sender, EventArgs e) AboutBox aboutBox = new AboutBox(); aboutBox.ShowDialog(); } + + private string QuickName = "Reminder"; + + private string systemStartPath { get { return Environment.GetFolderPath(Environment.SpecialFolder.Startup); } } + + private string appAllPath { get { return Process.GetCurrentProcess().MainModule.FileName; } } + + private void SetAutoStart(bool auto_run) + { + // get the path set of this software + List shortcurPaths = GetQuickFromFolder(systemStartPath, appAllPath); + if (auto_run) + { + if (shortcurPaths.Count > 1) + { + for (int i = 1; i < shortcurPaths.Count; i++) + { + DeleteFile(shortcurPaths[i]); + } + } + else if (shortcurPaths.Count == 0) + { + bool res = CreateShortcut(systemStartPath, QuickName, appAllPath, "Reminder"); + } + } + else + { + if (shortcurPaths.Count > 0) + { + for (int i = 0; i < shortcurPaths.Count; i++) + { + DeleteFile(shortcurPaths[i]); + } + } + } + } + + private bool CreateShortcut(string systemStartPath, string quickName, string appAllPath, string description = null) + { + try + { + if (!Directory.Exists(systemStartPath)) Directory.CreateDirectory(systemStartPath); + string shortcutPath = Path.Combine(systemStartPath, string.Format("{0}.lnk", quickName)); + WshShell shell = new IWshRuntimeLibrary.WshShell(); + IWshShortcut shortcut = (IWshRuntimeLibrary.IWshShortcut)shell.CreateShortcut(shortcutPath); + shortcut.TargetPath = appAllPath; + shortcut.WorkingDirectory = Path.GetDirectoryName(shortcutPath); + shortcut.WindowStyle = 1; + shortcut.Description = description; + shortcut.IconLocation = "ICO2.ico"; + shortcut.Save(); + return true; + } + catch (Exception ex) + { + string message = ex.Message; + } + return false; + } + + private void DeleteFile(string path) + { + FileAttributes attr = System.IO.File.GetAttributes(path); + if (attr == FileAttributes.Directory) + { + Directory.Delete(path, true); + } + else + { + System.IO.File.Delete(path); + } + } + + private List GetQuickFromFolder(string systemStartPath, string appAllPath) + { + List tmpStrs = new List(); + String tmpStr = null; + String[] files = Directory.GetFiles(systemStartPath, "*.lnk"); + for (int i = 0; i < files.Length; i++) + { + tmpStr = GetAppPathFromQuick(files[i]); + if (tmpStr == appAllPath) + { + tmpStrs.Add(files[i]); + } + } + return tmpStrs; + } + + private string GetAppPathFromQuick(string shortcutPath) + { + if (System.IO.File.Exists(shortcutPath)) + { + WshShell shell = new WshShell(); + IWshShortcut shortcut = (IWshShortcut)shell.CreateShortcut(shortcutPath); + return shortcut.TargetPath; + } + else + { + return null; + } + } } }