Skip to content

Commit 5fd5e07

Browse files
committed
添加日程计划功能
代码清理
1 parent afa8842 commit 5fd5e07

File tree

13 files changed

+596
-66
lines changed

13 files changed

+596
-66
lines changed

TimeControl/Data/Task.cs

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
using System;
2+
using System.Windows.Forms;
3+
using TimeControl.Tools;
4+
5+
namespace TimeControl.Data
6+
{
7+
public class Task
8+
{
9+
public Task()
10+
{ }
11+
12+
public Task(string name, TimeSpan duration, bool isFocus, bool isDeepFocus)
13+
{
14+
Name = name;
15+
Duration = duration;
16+
IsFocus = isFocus;
17+
this.isDeepFocus = isDeepFocus;
18+
}
19+
20+
public string Name;
21+
public TimeSpan Duration;
22+
public bool IsFocus;
23+
private bool isDeepFocus;
24+
private DateTime startTime = new DateTime(0);
25+
private DateTime endTime;
26+
public bool IsDeepFocus
27+
{ get { return IsFocus && isDeepFocus; } }
28+
29+
public override string ToString()
30+
{
31+
return Name + " (" + Duration.Minutes + "min)";
32+
}
33+
34+
public void RunTask()
35+
{
36+
startTime = DateTime.Now;
37+
if (IsFocus)
38+
{
39+
LockHelper.StartLock(Password.unlockPasswordHash, (int)Duration.TotalMinutes);
40+
}
41+
else if (IsDeepFocus)
42+
{
43+
LockHelper.StartDeepLock((int)Duration.TotalMinutes);
44+
}
45+
else
46+
{
47+
MessageBox.Show(
48+
"任务已启动。该日程不需要启动任何计算机操作,请自行完成,预估时间" + Duration + "min");
49+
}
50+
}
51+
52+
public string EndTask()
53+
{
54+
if (startTime == new DateTime(0))
55+
{
56+
return "任务已完成";
57+
}
58+
endTime = DateTime.Now;
59+
return ToString() + "(实际" + (endTime - startTime) + ")";
60+
}
61+
}
62+
}

TimeControl/Tools/LockHelper.cs

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
using System;
2+
using System.IO;
3+
using System.Windows.Forms;
4+
using TimeControl.Windows;
5+
6+
namespace TimeControl.Tools
7+
{
8+
internal class LockHelper
9+
{
10+
public static void StartLock(string unlockPasswordHash, int minutes)
11+
{
12+
IntPtr nowDesktop = Dllimport.GetThreadDesktop(Dllimport.GetCurrentThreadId());
13+
IntPtr newDesktop = Dllimport.CreateDesktop("Lock", null, null, 0, Dllimport.ACCESS_MASK.GENERIC_ALL, IntPtr.Zero);
14+
Dllimport.SwitchDesktop(newDesktop);
15+
System.Threading.Tasks.Task.Factory.StartNew(() =>
16+
{
17+
Dllimport.SetThreadDesktop(newDesktop);
18+
Lock _lock;
19+
if (minutes != 0)
20+
_lock = new(minutes, unlockPasswordHash);
21+
else
22+
_lock = new(unlockPasswordHash);
23+
Application.Run(_lock);
24+
}).Wait();
25+
Dllimport.SwitchDesktop(nowDesktop);
26+
Dllimport.CloseDesktop(newDesktop);
27+
}
28+
29+
public static void Interrupt()
30+
{
31+
IntPtr nowDesktop = Dllimport.GetThreadDesktop(Dllimport.GetCurrentThreadId());
32+
IntPtr newDesktop = Dllimport.CreateDesktop("Lock", null, null, 0, Dllimport.ACCESS_MASK.GENERIC_ALL, IntPtr.Zero);
33+
Dllimport.SwitchDesktop(newDesktop);
34+
System.Threading.Tasks.Task.Factory.StartNew(() =>
35+
{
36+
Dllimport.SetThreadDesktop(newDesktop);
37+
InterruptWindow interruptWindow = new InterruptWindow();
38+
Application.Run(interruptWindow);
39+
}).Wait();
40+
Dllimport.SwitchDesktop(nowDesktop);
41+
Dllimport.CloseDesktop(newDesktop);
42+
}
43+
44+
public static void StartDeepLock(int minutes)
45+
{
46+
TimeSpan deepTime = new(0, minutes, 0);
47+
File.WriteAllText(TCFile.DeepTempTimeFile, DateTime.Now + Environment.NewLine + deepTime);
48+
SystemControl.Shutdown();
49+
Application.Exit();
50+
}
51+
}
52+
}

TimeControl/Tools/Password.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ namespace TimeControl.Tools
55
{
66
internal static class Password
77
{
8+
public static string unlockPasswordHash = "";//The hash of the password
9+
810
public static string ComputeHash(string str)
911
{
1012
byte[] bytes = Encoding.UTF8.GetBytes(str);

TimeControl/Tools/SystemControl.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ public static void Shutdown()
1616
Process process = Process.Start(processStartInfo);
1717
process.StandardInput.WriteLine("shutdown -s -f -t 0");
1818
}
19+
1920
public static void ProgramRestart()
2021
{
2122
Process.Start(AppDomain.CurrentDomain.BaseDirectory + "TimeControl.exe");

TimeControl/Tools/TCFile.cs

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,9 @@ public static class TCFile
1717
//密码
1818
public static readonly string PassLocation = BaseLocation + "\\TCPass.txt";//获取密码位置
1919

20+
//Task Planner
21+
public static readonly string TaskLocation = BaseLocation + "\\Tasks.xml";
22+
2023
//计时
2124
public static readonly string TimeFileDirectory = BaseLocation
2225
+ "\\TCTimeData";
@@ -35,13 +38,36 @@ public static class TCFile
3538

3639
//Title management
3740
public static readonly string TitleFile = BaseLocation + "Title.txt";
41+
3842
//Auto shutdown
3943
public static readonly string ShutdownSpan = BaseLocation + "\\Shutdown.txt";
4044

4145
//TimeData dir
4246
public static readonly string SavedData = BaseLocation + "\\SavedData.xml";
4347

4448
public static readonly string SavedDataDir = BaseLocation + "\\SavedData";
49+
50+
//Task Planner
51+
public static void SaveTasks(List<Data.Task> tasks)
52+
{
53+
using (StreamWriter sw = new StreamWriter(TaskLocation))
54+
{
55+
XmlSerializer xmlSerializer = new XmlSerializer(typeof(List<Task>));
56+
xmlSerializer.Serialize(sw, tasks);
57+
}
58+
}
59+
60+
public static List<Task> ReadTasks()
61+
{
62+
List<Task> tasks = new List<Task>();
63+
using (StreamReader sr = new StreamReader(TaskLocation))
64+
{
65+
XmlSerializer xmlSerializer = new XmlSerializer(typeof(List<Task>));
66+
tasks = (List<Task>)xmlSerializer.Deserialize(sr);
67+
}
68+
return tasks;
69+
}
70+
4571
//Title management
4672
public static void SaveTitle(ListBox titleListBox)
4773
{
@@ -52,6 +78,7 @@ public static void SaveTitle(ListBox titleListBox)
5278
}
5379
File.WriteAllLines(TCFile.TitleFile, titleList);
5480
}
81+
5582
public static void ReadTitle(ListBox titleListBox)
5683
{
5784
if (File.Exists(TitleFile))
@@ -63,6 +90,7 @@ public static void ReadTitle(ListBox titleListBox)
6390
}
6491
}
6592
}
93+
6694
//Apps
6795

6896
public static void SaveApps(List<App> apps)
@@ -145,6 +173,7 @@ public static string[] SavedDataFiles
145173
return Directory.GetFiles(SavedDataDir);
146174
}
147175
}
176+
148177
public static void SaveTimeData(TimeData time)
149178
{
150179
using (StreamWriter sw = new(SavedData))

TimeControl/Windows/ControlPanel.Designer.cs

Lines changed: 65 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)