-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathWarningForm.cs
93 lines (80 loc) · 3.23 KB
/
WarningForm.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
using System;
using System.Drawing;
using System.Windows.Forms;
using System.Timers;
using System.Diagnostics;
using Timer = System.Timers.Timer;
namespace Sleeper
{
public partial class WarningForm : Form
{
private WarningConfiguration WarningConfiguration;
private Timer CountdownTimer = new Timer(interval: 1000);
// [DllImport("user32.dll")]
// [return: MarshalAs(UnmanagedType.Bool)]
// private static extern bool SetWindowPos(IntPtr hWnd, IntPtr hWndInsertAfter, int X, int Y, int cx, int cy, uint uFlags);
public WarningForm(WarningConfiguration Config)
{
WarningConfiguration = Config;
InitializeComponent();
}
internal void UpdateRemainingSeconds(ushort RemainingSeconds)
{
WarningConfiguration.RemainingSeconds = RemainingSeconds;
}
private void LoadWarningForm(object sender, EventArgs e)
{
CountdownTimer.Elapsed += CountdownTimerTick;
CountdownTimer.Start();
// Centering window position
Location = new Point((Screen.PrimaryScreen.WorkingArea.Width - Width) / 2, (Screen.PrimaryScreen.WorkingArea.Height - Height) / 2);
// Set HWND_TOPMOST if you like
// SetWindowPos(Handle, new IntPtr(-1), (Screen.PrimaryScreen.WorkingArea.Width - Width) / 2, (Screen.PrimaryScreen.WorkingArea.Height - Height) / 2, Bounds.Width, Bounds.Height, 0);
}
private void CountdownTimerTick(object sender, ElapsedEventArgs e)
{
if (WarningConfiguration.RemainingSeconds > 0)
{
WarningConfiguration.RemainingSeconds -= 1;
Invalidate();
}
else
{
CountdownTimer.Stop();
}
}
protected override void OnPaint(PaintEventArgs e)
{
base.OnPaint(e);
using (Font Font = new Font("Microsoft Sans Serif", 22))
{
StringFormat StringFormat = new StringFormat()
{
Alignment = StringAlignment.Center,
LineAlignment = StringAlignment.Center
};
string Statement = "Computer will " + WarningConfiguration.Operation + (WarningConfiguration.RemainingSeconds > 0 ? " in " + WarningConfiguration.RemainingSeconds + " second" + (WarningConfiguration.RemainingSeconds > 1 ? "s" : "") : " now");
e.Graphics.DrawString(Statement, Font, Brushes.Black, ClientRectangle, StringFormat);
}
}
// prevent the window from closing with ALT + F4 action
protected override void OnFormClosing(FormClosingEventArgs e)
{
if (new StackTrace(8, false).GetFrame(0).GetMethod().Name == "DefWndProc")
{
e.Cancel = true;
}
base.OnFormClosing(e);
}
// Prevent window from showing in alt tab list
protected override CreateParams CreateParams
{
get
{
CreateParams CreateParams = base.CreateParams;
CreateParams.ExStyle |= 0x80; // WS_EX_TOOLWINDOW
return CreateParams;
}
}
}
}