-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMainWindow.cs
More file actions
206 lines (181 loc) · 7.38 KB
/
Copy pathMainWindow.cs
File metadata and controls
206 lines (181 loc) · 7.38 KB
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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Runtime.InteropServices;
using System.Runtime.Remoting.Metadata.W3cXsd2001;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace CountDown
{
public partial class MainWindow : Form
{
private Color transparentColor = Color.Black;
private bool allowTransparentClick = false;
private string endTime = "17:30:00";
public MainWindow()
{
InitializeComponent();
this.Setup();
}
public void Setup()
{
int screenWidth = Screen.PrimaryScreen.Bounds.Width;
int screenHeight = Screen.PrimaryScreen.Bounds.Height;
int formWidth = this.Width;
int formHeight = this.Height;
this.Location = new Point(screenWidth - formWidth - 20, screenHeight - formHeight - 20);
this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.None;
this.BackColor = transparentColor;
this.TransparencyKey = transparentColor;
this.TopMost = true;
this.AllowTransparency = true;
this.ShowInTaskbar = false;
this.SetStyle(ControlStyles.UserPaint, true);
this.SetStyle(ControlStyles.SupportsTransparentBackColor, false);
this.RegisterTaskWithSideEffect(this.CountDown);
this.RegisterCountDownEvents();
}
public void RegisterCountDownEvents()
{
Label countDown = (Label)this.GetControlByName("countDown");
countDown.EnableDrag();
countDown.EnableDoubleClickToClose();
countDown.MouseClick += (sender, e) =>
{
if(e.Button == MouseButtons.Right)
{
ColorDialog colorDialog = new ColorDialog();
colorDialog.Color = transparentColor;
if(colorDialog.ShowDialog() == DialogResult.OK)
{
if(colorDialog.Color != transparentColor)
countDown.ForeColor = colorDialog.Color;
else
{
MessageBox.Show("设置为该颜色将导致文字变为透明,不允许设置为当前颜色");
}
}
}
};
countDown.MouseClick += (sender, e) =>
{
if (e.Button != MouseButtons.Middle) return;
ColorDialog colorDialog = new ColorDialog();
colorDialog.Color = transparentColor;
if (colorDialog.ShowDialog() == DialogResult.OK)
{
if (colorDialog.Color != countDown.ForeColor)
{
transparentColor = colorDialog.Color;
this.TransparencyKey = transparentColor;
countDown.BackColor = transparentColor;
this.BackColor = transparentColor;
}
else
{
MessageBox.Show("设置为该颜色将导致文字变为透明,不允许设置为当前颜色");
}
}
};
}
public Task RegisterTaskWithSideEffect(Action action, Action taskEnd=null)
{
Task newTask = new Task(action);
newTask.Start();
newTask.ContinueWith(t=>taskEnd, TaskScheduler.FromCurrentSynchronizationContext());
return newTask;
}
public void CountDown()
{
DateTime now = DateTime.Now;
DateTime end = DateTime.Parse(now.ToString("yyyy-MM-dd "+this.endTime));
while (now < end)
{
end = DateTime.Parse(now.ToString("yyyy-MM-dd " + this.endTime));
if (now > end) continue;
TimeSpan timeSpan = end - now;
string leftTime = String.Format("{0:D2}:{1:D2}:{2:D2}", timeSpan.Hours, timeSpan.Minutes, timeSpan.Seconds);
MethodInvoker mi = new MethodInvoker(() =>
{
Label countDown = (Label)this.GetControlByName("countDown");
if (countDown == null) return;
countDown.Text = leftTime;
});
this.BeginInvoke(mi);
Thread.Sleep(1000);
now = DateTime.Now;
}
}
private Control GetControlByName(string name)
{
Control control = this.Controls.Find(name, true).FirstOrDefault();
return control;
}
private void NotifyMenuItemClose_Click(object sender, EventArgs e)
{
this.Close();
}
private void allowTransparentMenuItem_Click(object sender, EventArgs e)
{
this.allowTransparentClick = !this.allowTransparentClick;
this.allowTransparentMenuItem.Checked = this.allowTransparentClick;
this.SetStyle(ControlStyles.SupportsTransparentBackColor, this.allowTransparentClick);
if (this.allowTransparentClick)
{
this.onClickThrough();
}
else
{
this.offClickThrough();
}
}
private void setEndTimeMenuItem_Click(object sender, EventArgs e)
{
DateTime now = DateTime.Now;
DateTime endTime = DateTime.Parse(now.ToString("yyyy-MM-dd " + this.endTime));
Form setEndTimeDialog = new Config();
DateTimePicker endTimePicker = (DateTimePicker)setEndTimeDialog.Controls.Find("endTimePicker", true).FirstOrDefault();
endTimePicker.Value = endTime;
Button endTimeConfirm = (Button)setEndTimeDialog.Controls.Find("endTimeConfirm", true).FirstOrDefault();
endTimeConfirm.MouseClick += (s, _e) =>
{
endTime = endTimePicker.Value;
this.endTime = endTime.ToString("HH:mm:ss");
setEndTimeDialog.Close();
};
Button endTimeCancel = (Button)setEndTimeDialog.Controls.Find("endTimeCancel", true).First();
endTimeCancel.MouseClick += (s, _e) =>
{
setEndTimeDialog.Close();
};
setEndTimeDialog.ShowDialog();
}
private void onClickThrough()
{
const int WS_EX_TRANSPARENT = 0x20;
const int WS_EX_LAYERED = 0x80000;
const int GWL_EXSTYLE = -20;
int style = GetWindowLong(this.Handle, GWL_EXSTYLE);
style |= WS_EX_TRANSPARENT;
SetWindowLong(this.Handle, GWL_EXSTYLE, style);
}
private void offClickThrough()
{
const int WS_EX_TRANSPARENT = 0x20;
const int WS_EX_LAYERED = 0x80000;
const int GWL_EXSTYLE = -20;
int style = GetWindowLong(this.Handle, GWL_EXSTYLE);
style &= ~WS_EX_TRANSPARENT;
SetWindowLong(this.Handle, GWL_EXSTYLE, style);
}
[DllImport("user32.dll")]
private static extern int GetWindowLong(IntPtr hWnd, int nIndex);
[DllImport("user32.dll")]
private static extern int SetWindowLong(IntPtr hWnd, int nIndex, int dwNewLong);
}
}