-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSplashScreen.cs
More file actions
355 lines (312 loc) · 10.6 KB
/
SplashScreen.cs
File metadata and controls
355 lines (312 loc) · 10.6 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
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
using System;
using System.Collections;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.IO;
using System.Threading;
using System.Windows.Forms;
using System.Xml;
namespace SplashScreen
{
// The SplashScreen class definition. AKO Form
public partial class SplashScreen : Form
{
#region Member Variables
// Threading
private static SplashScreen ms_frmSplash = null;
private static Thread ms_oThread = null;
// Fade in and out.
private double m_dblOpacityIncrement = .05;
private double m_dblOpacityDecrement = .08;
private const int TIMER_INTERVAL = 50;
// Status and progress bar
private string m_sStatus;
private string m_sTimeRemaining;
private double m_dblCompletionFraction = 0.0;
private Rectangle m_rProgress;
// Progress smoothing
private double m_dblLastCompletionFraction = 0.0;
private double m_dblPBIncrementPerTimerInterval = .015;
// Self-calibration support
private int m_iIndex = 1;
private int m_iActualTicks = 0;
private ArrayList m_alPreviousCompletionFraction;
private ArrayList m_alActualTimes = new ArrayList();
private DateTime m_dtStart;
private bool m_bFirstLaunch = false;
private bool m_bDTSet = false;
#endregion Member Variables
/// <summary>
/// Constructor
/// </summary>
public SplashScreen()
{
InitializeComponent();
this.Opacity = 0.0;
UpdateTimer.Interval = TIMER_INTERVAL;
UpdateTimer.Start();
this.ClientSize = this.Size;
}
#region Public Static Methods
// A static method to create the thread and
// launch the SplashScreen.
static public void ShowSplashScreen()
{
// Make sure it's only launched once.
if (ms_frmSplash != null)
return;
ms_oThread = new Thread(new ThreadStart(SplashScreen.ShowForm));
ms_oThread.IsBackground = true;
ms_oThread.SetApartmentState(ApartmentState.STA);
ms_oThread.Start();
while (ms_frmSplash == null || ms_frmSplash.IsHandleCreated == false)
{
System.Threading.Thread.Sleep(TIMER_INTERVAL);
}
}
// Close the form without setting the parent.
static public void CloseForm()
{
if (ms_frmSplash != null && ms_frmSplash.IsDisposed == false)
{
// Make it start going away.
ms_frmSplash.m_dblOpacityIncrement = -ms_frmSplash.m_dblOpacityDecrement;
}
ms_oThread = null; // we don't need these any more.
ms_frmSplash = null;
}
// A static method to set the status and update the reference.
static public void SetStatus(string newStatus)
{
SetStatus(newStatus, true);
}
// A static method to set the status and optionally update the reference.
// This is useful if you are in a section of code that has a variable
// set of status string updates. In that case, don't set the reference.
static public void SetStatus(string newStatus, bool setReference)
{
if (ms_frmSplash == null)
return;
ms_frmSplash.m_sStatus = newStatus;
if (setReference)
ms_frmSplash.SetReferenceInternal();
}
// Static method called from the initializing application to
// give the splash screen reference points. Not needed if
// you are using a lot of status strings.
static public void SetReferencePoint()
{
if (ms_frmSplash == null)
return;
ms_frmSplash.SetReferenceInternal();
}
#endregion Public Static Methods
#region Private Methods
// A private entry point for the thread.
static private void ShowForm()
{
ms_frmSplash = new SplashScreen();
Application.Run(ms_frmSplash);
}
// Internal method for setting reference points.
private void SetReferenceInternal()
{
if (m_bDTSet == false)
{
m_bDTSet = true;
m_dtStart = DateTime.Now;
ReadIncrements();
}
double dblMilliseconds = ElapsedMilliSeconds();
m_alActualTimes.Add(dblMilliseconds);
m_dblLastCompletionFraction = m_dblCompletionFraction;
if (m_alPreviousCompletionFraction != null && m_iIndex < m_alPreviousCompletionFraction.Count)
m_dblCompletionFraction = (double)m_alPreviousCompletionFraction[m_iIndex++];
else
m_dblCompletionFraction = (m_iIndex > 0) ? 1 : 0;
}
// Utility function to return elapsed Milliseconds since the
// SplashScreen was launched.
private double ElapsedMilliSeconds()
{
TimeSpan ts = DateTime.Now - m_dtStart;
return ts.TotalMilliseconds;
}
// Function to read the checkpoint intervals from the previous invocation of the
// splashscreen from the XML file.
private void ReadIncrements()
{
string sPBIncrementPerTimerInterval = SplashScreenXMLStorage.Interval;
double dblResult;
if (Double.TryParse(sPBIncrementPerTimerInterval, System.Globalization.NumberStyles.Float, System.Globalization.NumberFormatInfo.InvariantInfo, out dblResult) == true)
m_dblPBIncrementPerTimerInterval = dblResult;
else
m_dblPBIncrementPerTimerInterval = .0015;
string sPBPreviousPctComplete = SplashScreenXMLStorage.Percents;
if (sPBPreviousPctComplete != "")
{
string[] aTimes = sPBPreviousPctComplete.Split(null);
m_alPreviousCompletionFraction = new ArrayList();
for (int i = 0; i < aTimes.Length; i++)
{
double dblVal;
if (Double.TryParse(aTimes[i], System.Globalization.NumberStyles.Float, System.Globalization.NumberFormatInfo.InvariantInfo, out dblVal) == true)
m_alPreviousCompletionFraction.Add(dblVal);
else
m_alPreviousCompletionFraction.Add(1.0);
}
}
else
{
m_bFirstLaunch = true;
m_sTimeRemaining = "";
}
}
// Method to store the intervals (in percent complete) from the current invocation of
// the splash screen to XML storage.
private void StoreIncrements()
{
string sPercent = "";
double dblElapsedMilliseconds = ElapsedMilliSeconds();
for (int i = 0; i < m_alActualTimes.Count; i++)
sPercent += ((double)m_alActualTimes[i] / dblElapsedMilliseconds).ToString("0.####", System.Globalization.NumberFormatInfo.InvariantInfo) + " ";
SplashScreenXMLStorage.Percents = sPercent;
m_dblPBIncrementPerTimerInterval = 1.0 / (double)m_iActualTicks;
SplashScreenXMLStorage.Interval = m_dblPBIncrementPerTimerInterval.ToString("#.000000", System.Globalization.NumberFormatInfo.InvariantInfo);
}
public static SplashScreen GetSplashScreen()
{
return ms_frmSplash;
}
#endregion Private Methods
#region Event Handlers
// Tick Event handler for the Timer control. Handle fade in and fade out and paint progress bar.
private void UpdateTimer_Tick(object sender, System.EventArgs e)
{
lblStatus.Text = m_sStatus;
// Calculate opacity
if (m_dblOpacityIncrement > 0) // Starting up splash screen
{
m_iActualTicks++;
if (this.Opacity < 1)
this.Opacity += m_dblOpacityIncrement;
}
else // Closing down splash screen
{
if (this.Opacity > 0)
this.Opacity += m_dblOpacityIncrement;
else
{
StoreIncrements();
UpdateTimer.Stop();
this.Close();
}
}
// Paint progress bar
if (m_bFirstLaunch == false && m_dblLastCompletionFraction < m_dblCompletionFraction)
{
m_dblLastCompletionFraction += m_dblPBIncrementPerTimerInterval;
int width = (int)Math.Floor(pnlStatus.ClientRectangle.Width * m_dblLastCompletionFraction);
int height = pnlStatus.ClientRectangle.Height;
int x = pnlStatus.ClientRectangle.X;
int y = pnlStatus.ClientRectangle.Y;
if (width > 0 && height > 0)
{
m_rProgress = new Rectangle(x, y, width, height);
if (!pnlStatus.IsDisposed)
{
Graphics g = pnlStatus.CreateGraphics();
LinearGradientBrush brBackground = new LinearGradientBrush(m_rProgress, Color.FromArgb(58, 96, 151), Color.FromArgb(181, 237, 254), LinearGradientMode.Horizontal);
g.FillRectangle(brBackground, m_rProgress);
g.Dispose();
}
int iSecondsLeft = 1 + (int)(TIMER_INTERVAL * ((1.0 - m_dblLastCompletionFraction) / m_dblPBIncrementPerTimerInterval)) / 1000;
m_sTimeRemaining = (iSecondsLeft == 1) ? string.Format("1 second remaining") : string.Format("{0} seconds remaining", iSecondsLeft);
}
}
lblTimeRemaining.Text = m_sTimeRemaining;
}
// Close the form if they double click on it.
private void SplashScreen_DoubleClick(object sender, System.EventArgs e)
{
// Use the overload that doesn't set the parent form to this very window.
CloseForm();
}
#endregion Event Handlers
private void SplashScreen_Load(object sender, EventArgs e)
{
string ImagePath = Environment.CurrentDirectory + "\\screen.jpg";
BackgroundImage = Image.FromFile(ImagePath);
}
}
#region Auxiliary Classes
/// <summary>
/// A specialized class for managing XML storage for the splash screen.
/// </summary>
internal class SplashScreenXMLStorage
{
private static string ms_StoredValues = "SplashScreen.xml";
private static string ms_DefaultPercents = "";
private static string ms_DefaultIncrement = ".015";
// Get or set the string storing the percentage complete at each checkpoint.
static public string Percents
{
get { return GetValue("Percents", ms_DefaultPercents); }
set { SetValue("Percents", value); }
}
// Get or set how much time passes between updates.
static public string Interval
{
get { return GetValue("Interval", ms_DefaultIncrement); }
set { SetValue("Interval", value); }
}
// Store the file in a location where it can be written with only User rights. (Don't use install directory).
static private string StoragePath
{
get {return Path.Combine(Application.UserAppDataPath, ms_StoredValues);}
}
// Helper method for getting inner text of named element.
static private string GetValue(string name, string defaultValue)
{
if (!File.Exists(StoragePath))
return defaultValue;
try
{
XmlDocument docXML = new XmlDocument();
docXML.Load(StoragePath);
XmlElement elValue = docXML.DocumentElement.SelectSingleNode(name) as XmlElement;
return (elValue == null) ? defaultValue : elValue.InnerText;
}
catch
{
return defaultValue;
}
}
// Helper method for setting inner text of named element. Creates document if it doesn't exist.
static public void SetValue(string name,
string stringValue)
{
XmlDocument docXML = new XmlDocument();
XmlElement elRoot = null;
if (!File.Exists(StoragePath))
{
elRoot = docXML.CreateElement("root");
docXML.AppendChild(elRoot);
}
else
{
docXML.Load(StoragePath);
elRoot = docXML.DocumentElement;
}
XmlElement value = docXML.DocumentElement.SelectSingleNode(name) as XmlElement;
if (value == null)
{
value = docXML.CreateElement(name);
elRoot.AppendChild(value);
}
value.InnerText = stringValue;
docXML.Save(StoragePath);
}
}
#endregion Auxiliary Classes
}