-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathForm1.cs
445 lines (385 loc) · 14.5 KB
/
Form1.cs
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
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
using System.DirectoryServices;
using System.Linq.Expressions;
using System.Runtime.CompilerServices;
namespace tokei_wform
{
public partial class Form1 : Form
{
public Form1()
{
this.clockCountCancel = null;
this.clockCountTask = null;
this.ClockString = "";
InitializeComponent();
this.currentOffset = null;
this.Opacity = 0; // 初回表示が見えなくなるように
}
// private Dictionary<Guid, string> config_files = new Dictionary<Guid, string>();
private string configcontents;
// private static string config_filename = "tokeiconfig.config";
private string MakeConfigFilename()
{
try
{
// 自分自身の仮想デスクトップIDを取得
var my_guid = VirtualDesktopManager.DesktopManager.GetWindowDesktopId(this.Handle);
return $"tokei_{my_guid.ToString().ToUpper()}.config";
}
catch (Exception ex)
{
return "";
}
}
private bool ReadConfigFile()
{
// 設定ファイルの読み込み
StreamReader? sr = null;
string cur_config = "";
try
{
sr = new StreamReader(this.MakeConfigFilename());
cur_config = sr.ReadToEnd();
}
catch
{
return false;
}
finally
{
if (sr != null)
{
sr.Close();
sr.Dispose();
}
}
if (string.IsNullOrEmpty(cur_config)) return false;
this.configcontents = cur_config;
return true;
}
private void WriteConfigFile()
{
// 設定ファイルの書き出し
StreamWriter sw = null;
try
{
sw = new StreamWriter(this.MakeConfigFilename());
sw.Write(this.configcontents);
sw.Close();
}
catch
{
// 何もしない
}
finally
{
if (sw != null)
{
sw.Close();
sw.Dispose();
}
}
}
private Task? clockCountTask;
private CancellationTokenSource? clockCountCancel;
private void Form1_Load(object sender, EventArgs e)
{
// アプリケーションロード完了
// 時刻カウントタスクの起動
this.clockCountCancel = new CancellationTokenSource();
this.clockCountTask = Task.Factory.StartNew(() =>
{
this.ClockCount();
});
}
// 描画文字列
private string ClockString;
private ClockFontData? clockData = null;
// 描画イベント
protected override void OnPaint(PaintEventArgs e)
{
base.OnPaint(e);
if (string.IsNullOrEmpty(this.ClockString) == false)
{
// 描画
lock (this.ClockString)
{
e.Graphics.TextRenderingHint = this.clockData.fontQuality;
e.Graphics.DrawString(this.ClockString, this.clockData.font, this.clockData.foreColor, this.clockData.padding);
}
}
}
private void DispClock(DateTime _d)
{
// 時刻の描画
lock (this.ClockString)
{
this.ClockString = $"{_d.Year:D04}/{_d.Month:D02}/{_d.Day:D02}({System.Globalization.CultureInfo.CurrentCulture.DateTimeFormat.GetDayName(_d.DayOfWeek)}) {_d.Hour:D02}:{_d.Minute:D02}:{_d.Second:D02}";
Graphics _g = this.CreateGraphics();
SizeF _s = _g.MeasureString(this.ClockString, this.clockData.font);
// パディング分確保してみる
this.ClientSize = new Size((int)(_s.Width + this.clockData.padding.X * 2), (int)(_s.Height + this.clockData.padding.Y * 2));
_g.Dispose();
this.BackColor = this.clockData.backColor;
this.Opacity = this.clockData.opacity;
this.Location = new Point((int)(this.clockData.windowtop.X), (int)(this.clockData.windowtop.Y));
}
// 再描画要求
this.Invalidate();
}
private void ClockCount()
{
if (this.clockCountCancel == null) { return; }
CancellationToken _t = this.clockCountCancel.Token;
IAsyncResult? ar = null;
DateTime? cur_dt = null;
if (_t.IsCancellationRequested == false)
{
// 前処理:まずは一発目の表示
cur_dt = DateTime.Now;
ar = this.BeginInvoke(new Action(() =>
{
this.DispClock(cur_dt ?? DateTime.Now);
}));
}
while (_t.IsCancellationRequested == false)
{
// 時間が経つまで待つ
DateTime _c = DateTime.Now;
// ”秒”が変わるまで待つ
if (_c.Second == cur_dt?.Second)
{
if (_c.Millisecond < 900)
{
Thread.Sleep(50);
}
else
{
Thread.Sleep(1);
}
continue;
}
// 前の処理が終わるまで待つ
if (ar == null) break;
this.EndInvoke(ar);
// 次の処理
if (_t.IsCancellationRequested) break;
cur_dt = _c;
ar = this.BeginInvoke(new Action(() =>
{
this.DispClock(_c);
}));
}
}
private void Form1_FormClosing(object sender, FormClosingEventArgs e)
{
// 閉じる前処理
this.clockCountCancel?.Cancel();
}
private void Form1_FormClosed(object sender, FormClosedEventArgs e)
{
// 閉じる後処理
while (this.clockCountTask?.Wait(1) == false)
{
Application.DoEvents();
}
}
private Point? currentOffset;
private void Form1_MouseDown(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Left)
{
// ウィンドウ移動モード開始
// この時点でのマウスカーソルとウィンドウのtop/leftの差分を記憶
// つーても、そもそも e.Location が差分らしいのでそれで。
this.currentOffset = e.Location;
}
else if (e.Button == MouseButtons.Right)
{
// メニュー出す(フォント変更・終了)
ContextMenuStrip _m = new ContextMenuStrip();
ToolStripLabel _i_fontchange = new ToolStripLabel();
_i_fontchange.Text = $"フォントを変更";
_i_fontchange.Click += (sender, e) =>
{
// クリックされたのでフォント変更ダイアログを出す
ChangeFontForm _fm = new ChangeFontForm(this.clockData.DeepCopy());
if (_fm.ShowDialog() == DialogResult.OK)
{
this.clockData = _fm.curArg;
}
};
_m.Items.Add(_i_fontchange);
// 設定ファイルを書き出す
ToolStripLabel _i_config = new ToolStripLabel();
_i_config.Text = $"設定を保存";
_i_config.Click += (sender, _e) =>
{
// クリックされたので設定ファイルを更新する
if (this.clockData != null)
{
this.configcontents = this.clockData.ToConfig();
// ファイルに書き出し
this.WriteConfigFile();
}
};
_m.Items.Add(_i_config);
_m.Items.Add(new ToolStripSeparator());
ToolStripLabel _i_exit = new ToolStripLabel();
_i_exit.Text = $"終了";
_i_exit.Click += (sender, e) =>
{
// アプリ終了
Application.Exit();
};
_m.Items.Add(_i_exit);
_m.Show(this, e.Location);
}
}
private void Form1_MouseUp(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Left && this.currentOffset != null)
{
// ウィンドウ移動モード終了
this.currentOffset = null;
}
}
private void Form1_MouseMove(object sender, MouseEventArgs e)
{
// ウィンドウ移動モードなら追従して移動する
if (this.currentOffset != null)
{
Point _app = this.Location;
this.Location = new Point(_app.X + e.Location.X - this.currentOffset.Value.X, _app.Y + e.Location.Y - this.currentOffset.Value.Y);
this.clockData.windowtop = this.Location;
}
}
private void Form1_Shown(object sender, EventArgs e)
{
// 初回の表示のみ
// 設定ファイルの読み込み
if (this.ReadConfigFile())
{
// 読めたので採用
try
{
this.clockData = new ClockFontData(this.configcontents);
}
catch
{
this.clockData = null;
}
}
if (this.clockData == null)
{
// デフォルトで
this.clockData = new ClockFontData(new Font(this.Font.Name, 32.0f), new SolidBrush(this.ForeColor), this.BackColor, 1.00 / 1.2, new PointF(1, 1), new PointF(this.Location.X, this.Location.Y), System.Drawing.Text.TextRenderingHint.SystemDefault);
}
}
}
public class ClockFontData
{
public Font? font;
public SolidBrush? foreColor;
public Color backColor;
public double opacity;
// 描画パディング(上と左のみ:下は上と同じ、右は左と同じ
public PointF padding;
public PointF windowtop;
public System.Drawing.Text.TextRenderingHint fontQuality;
public ClockFontData(Font _f, SolidBrush _fb, Color _bc, double _o, PointF clockPadding, PointF windowtop, System.Drawing.Text.TextRenderingHint fontQuality)
{
this.font = _f;
this.foreColor = _fb;
this.backColor = _bc;
this.opacity = _o;
this.padding = clockPadding;
this.windowtop = windowtop;
this.fontQuality = fontQuality;
}
public float FontSize
{
get => font.Size;
set
{
Font _f = new Font(this.font.Name, value);
this.font = _f;
}
}
public ClockFontData DeepCopy()
{
var _d = new ClockFontData(this.font, this.foreColor, this.backColor, this.opacity, this.padding, this.windowtop, this.fontQuality);
return _d;
}
public string ToConfig()
{
// コンフィグ文字列へ変換
List<string> _c = new List<string>();
_c.Add($"font: {this.font.Name}, {this.font.Size}");
_c.Add($"quality: {this.fontQuality.ToString()}");
_c.Add($"color: {this.foreColor.Color.ToArgb()}, {this.backColor.ToArgb()}");
_c.Add($"padding: {this.padding.X}, {this.padding.Y}");
_c.Add($"opacity: {this.opacity}");
_c.Add($"windowlocate: {this.windowtop.X}, {this.windowtop.Y}");
return string.Join(System.Environment.NewLine, _c);
}
public ClockFontData(string config)
{
// コンフィグ文字列から変換
string[] _t = config.Split(System.Environment.NewLine);
Font _cf = null;
SolidBrush _cfb = null;
Color _cbc = Color.Black;
double _co = -1;
PointF _cp = new PointF(0,0);
PointF _cw = new PointF(0, 0);
System.Drawing.Text.TextRenderingHint _cq = System.Drawing.Text.TextRenderingHint.SystemDefault;
foreach ( var _tt in _t)
{
string[] _t0 = _tt.Split(new char[] { ':' });
if (_t0.Length != 2) continue;
switch (_t0[0].Trim())
{
case "font":
string[] _tf = _t0[1].Split(new char[] { ',' });
if ( _tf.Length != 2) continue;
float _ss = float.Parse(_tf[1].Trim());
_cf = new Font(_tf[0].Trim(), _ss);
break;
case "quality":
_cq = (System.Drawing.Text.TextRenderingHint)Enum.Parse(typeof(System.Drawing.Text.TextRenderingHint),_t0[1].Trim());
break;
case "color":
string[] _tc = _t0[1].Split(new char[] { ',' });
if (_tc.Length != 2) continue;
_cfb = new SolidBrush(Color.FromArgb(int.Parse(_tc[0].Trim())));
_cbc = Color.FromArgb(int.Parse(_tc[1].Trim()));
break;
case "padding":
string[] _tp = _t0[1].Split(new char[] { ',' });
if (_tp.Length != 2) continue;
_cp = new PointF(int.Parse(_tp[0].Trim()), int.Parse(_tp[1].Trim()));
break;
case "opacity":
_co = double.Parse(_t0[1].Trim());
break;
case "windowlocate":
string[] _tw = _t0[1].Split(new char[] { ',' });
if (_tw.Length != 2) continue;
_cw = new PointF(float.Parse(_tw[0].Trim()), float.Parse(_tw[1].Trim()));
break;
}
}
if ( _cf == null || _cfb == null )
{
throw new Exception($"Bad format config exception");
}
this.font = _cf;
this.fontQuality = _cq;
this.foreColor = _cfb;
this.backColor = _cbc;
this.opacity = _co;
this.padding = _cp;
this.windowtop = _cw;
}
}
}