-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathScreenSaverForm.cs
More file actions
199 lines (171 loc) · 6.32 KB
/
ScreenSaverForm.cs
File metadata and controls
199 lines (171 loc) · 6.32 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
using System;
using System.Drawing;
using System.Windows.Forms;
namespace DavidVidmar.WindowsFader
{
/// <summary>
/// The main form, which dims the screen.
/// </summary>
internal partial class ScreenSaverForm : Form
{
#region Private Fields
/// <summary>
/// Keep track of whether the screensaver has become active.
/// </summary>
private bool _active;
/// <summary>
/// Current mouse location.
/// </summary>
private Point _mouseLocation;
/// <summary>
/// Old mouse location.
/// </summary>
private Point _oldMouseLocation;
/// <summary>
/// Doing fade in?
/// </summary>
private bool _fadeIn;
private const bool Debug = false;
private DateTime _startTime;
#endregion
/// <summary>
/// Initializes a new instance of the <see cref="ScreenSaverForm"/> class.
/// </summary>
public ScreenSaverForm()
{
this.InitializeComponent();
this.SetupScreenSaver();
}
/// <summary>
/// Set up the main form as a full screen screensaver.
/// </summary>
private void SetupScreenSaver()
{
// Use double buffering to improve drawing performance
this.SetStyle(ControlStyles.OptimizedDoubleBuffer, true);
// Capture the mouse
this.Capture = true;
// Set the application to full screen mode and hide the mouse
Cursor.Hide();
Bounds = GetAllScreensBounds();
ShowInTaskbar = false;
Opacity = 0;
DoubleBuffered = true;
// Set up timer
timer.Interval = Convert.ToInt32(Settings.CompleteFadeInSeconds * 1000.0 / 200.0 * (1.0 / Settings.EndOpacityPercent));
// Set the color
if (Settings.FadeColor != 0)
this.BackColor = Color.FromArgb(Settings.FadeColor);
this._oldMouseLocation = Cursor.Position;
Cursor.Position = new Point(Bounds.Right + 1, Bounds.Bottom + 1);
if (Debug)
{
fadeToLabel.Visible = true;
fadeTimeLabel.Visible = true;
currentTimeLabel.Visible = true;
currentTransparencyLabel.Visible = true;
fadeToLabel.Text = $"Fade to: {Settings.EndOpacityPercent}";
fadeTimeLabel.Text = $"Fade time: {Settings.CompleteFadeInSeconds*1000} ms";
currentUserLabel.Text = System.Security.Principal.WindowsIdentity.GetCurrent().Name;
_startTime = DateTime.Now;
}
}
/// <summary>
/// Gets screens bounds and handles multi-monitor setups.
/// </summary>
/// <returns>Rectangle object representing the screen bounds.</returns>
private static Rectangle GetAllScreensBounds()
{
var allScreens = Rectangle.Empty;
foreach (var screen in Screen.AllScreens)
{
System.Diagnostics.Debug.WriteLine(screen.DeviceName + " " + screen.Bounds);
allScreens = Rectangle.Union(allScreens, screen.Bounds);
}
return allScreens;
}
/// <summary>
/// Fades in the screen.
/// </summary>
private void FadeIn()
{
this.timer.Interval = 10;
this._fadeIn = true;
}
/// <summary>
/// Handles the MouseMove event of the current form.
/// </summary>
/// <param name="sender">The source of the event.</param>
/// <param name="e">The <see cref="System.Windows.Forms.MouseEventArgs"/> instance containing the event data.</param>
private void ScreenSaverForm_MouseMove(object sender, MouseEventArgs e)
{
// Set IsActive and MouseLocation only the first time this event is called.
if (!_active)
{
_mouseLocation = MousePosition;
_active = true;
}
else
{
// If the mouse has moved significantly since first call, close.
if ((Math.Abs(MousePosition.X - _mouseLocation.X) > 1) ||
(Math.Abs(MousePosition.Y - _mouseLocation.Y) > 1))
{
FadeIn();
}
else
{
_mouseLocation = MousePosition;
}
}
}
/// <summary>
/// Handles the KeyDown event of the current form.
/// </summary>
/// <param name="sender">The source of the event.</param>
/// <param name="e">The <see cref="System.Windows.Forms.KeyEventArgs"/> instance containing the event data.</param>
private void ScreenSaverForm_KeyDown(object sender, KeyEventArgs e)
{
this.FadeIn();
}
/// <summary>
/// Handles the MouseDown event of the current form.
/// </summary>
/// <param name="sender">The source of the event.</param>
/// <param name="e">The <see cref="System.Windows.Forms.MouseEventArgs"/> instance containing the event data.</param>
private void ScreenSaverForm_MouseDown(object sender, MouseEventArgs e)
{
this.FadeIn();
}
/// <summary>
/// Handles the Tick event of the timer.
/// </summary>
/// <param name="sender">The source of the event.</param>
/// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
private void timer_Tick(object sender, EventArgs e)
{
if (!this._fadeIn)
{
if (Opacity < Settings.EndOpacityPercent)
{
Opacity += 0.005;
if (Debug)
{
currentTransparencyLabel.Text = $"Current opacity: {Opacity:N2}";
currentTimeLabel.Text = $"Elapsed time: {DateTime.Now.Subtract(_startTime).TotalMilliseconds} ms";
}
}
}
else
{
Cursor.Position = this._oldMouseLocation;
Opacity -= 0.12;
if (Opacity <= 0)
{
Close();
}
}
Application.DoEvents();
}
}
}