-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMainForm.cs
352 lines (260 loc) · 8.66 KB
/
MainForm.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
/*
* Created by SharpDevelop.
* User: Michael
* Date: 04/11/2015
* Time: 21:05
*
* To change this template use Tools | Options | Coding | Edit Standard Headers.
*/
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Diagnostics;
using System.Drawing.Drawing2D;
using System.Drawing.Imaging;
using System.Runtime.InteropServices;
using System.IO;
using System.Windows.Forms;
namespace FlyingToasters
{
/// <summary>
/// Draws the toasters.
/// </summary>
public partial class MainForm : Form
{
[DllImport("user32.dll")]
static extern IntPtr SetParent(IntPtr hWndChild, IntPtr hWndNewParent);
[DllImport("user32.dll")]
static extern int SetWindowLong(IntPtr hWnd, int nIndex, IntPtr dwNewLong);
[DllImport("user32.dll", SetLastError = true)]
static extern int GetWindowLong(IntPtr hWnd, int nIndex);
[DllImport("user32.dll")]
static extern bool GetClientRect(IntPtr hWnd, out Rectangle lpRect);
private Random rnd_i = new Random();
private const int NUM_TOASTERS = 10;
private const int NUM_TOASTS = 5;
private bool isCurrentlyAnimating_i = false;
private List<FlyingObject> objects_i = new List<FlyingObject>();
private float fScreenScalingFactor_i = 1.0F;
private Rectangle virtualScreenBounds_i;
private Boolean isPreviewMode_i = false;
public MainForm()
{
//
// The InitializeComponent() call is required for Windows Forms designer support.
//
InitializeComponent();
this.SetStyle(ControlStyles.UserPaint, true);
this.SetStyle(ControlStyles.OptimizedDoubleBuffer, true);
this.SetStyle(ControlStyles.AllPaintingInWmPaint, true);
//
// Make our form fill ALL of the available screens
//
Rectangle totalSize = GetAllScreensBounds();
this.Bounds = totalSize;
SetVirtualScreenBounds();
Cursor.Hide();
CreateFlyingObjects();
}
/// <summary>
/// Alternative constructor that allows us to embed the screensaver in a preview
/// window. Thanks to http://www.codeproject.com/Articles/31376/Making-a-C-screensaver
/// for the tips.
/// </summary>
/// <param name="hParentWnd_p">Window handle of the preview window</param>
public MainForm(IntPtr hParentWnd_p)
{
//
// The InitializeComponent() call is required for Windows Forms designer support.
//
InitializeComponent();
isPreviewMode_i = true;
//
// Set the preview window as the parent of this one
//
SetParent(this.Handle, hParentWnd_p);
//
// Tell Windows that our form is a child window; this means that we'll
// be closed automatically whenever the parent window closes.
//
SetWindowLong(this.Handle, -16, new IntPtr(GetWindowLong(this.Handle, -16) | 0x40000000));
//
// Set the window size to be the same as that of the parent window
//
Rectangle parentBounds;
GetClientRect(hParentWnd_p, out parentBounds);
this.Size = parentBounds.Size;
this.Location = new Point(0,0);
fScreenScalingFactor_i = 10.0F;
SetVirtualScreenBounds();
this.SetStyle(ControlStyles.UserPaint, true);
this.SetStyle(ControlStyles.OptimizedDoubleBuffer, true);
this.SetStyle(ControlStyles.AllPaintingInWmPaint, true);
Cursor.Hide();
CreateFlyingObjects();
}
/// <summary>
/// Mark it as a WS_EX_TOOLWINDOW window, so the icon doesn't show up in Alt Tab
/// </summary>
protected override CreateParams CreateParams
{
get
{
CreateParams originalParams = base.CreateParams;
if (isPreviewMode_i)
{
originalParams.ExStyle |= 0x08000000; // WS_EX_NOACTIVATE
}
return originalParams;
}
}
/// <summary>
/// Creates a dummy screen rectangle that, in preview mode at least, is
/// a scaled up version of the real window.
/// </summary>
private void SetVirtualScreenBounds()
{
virtualScreenBounds_i = new Rectangle(this.Left,
this.Top,
(int) (this.Width * fScreenScalingFactor_i),
(int) (this.Height * fScreenScalingFactor_i));
}
private Rectangle GetAllScreensBounds()
{
Rectangle totalSize = Rectangle.Empty;
foreach (Screen s in Screen.AllScreens)
{
totalSize = System.Drawing.Rectangle.Union(totalSize, s.Bounds);
}
return totalSize;
}
//
// Loads a Bitmap out of this project/exe's resources. To get them in there
// in the first place (in SharpDevelop, at least), right-click the project and
// "Add Existing Item" to add the image file to the project, then go to that
// image file's properties in SharpDevelop and set the "Build action" property
// to "Embedded Resource".
//
private Bitmap GetBitmapResource(string sResourceName_p)
{
System.Reflection.Assembly myAssembly = System.Reflection.Assembly.GetExecutingAssembly();
Stream myStream = myAssembly.GetManifestResourceStream(sResourceName_p);
Bitmap gif = new Bitmap(myStream);
return gif;
}
private void CreateFlyingObjects()
{
//
// Add some toasters and toast
//
for (int i=0; i < NUM_TOASTERS; i++)
{
int iSpeed = rnd_i.Next(1,10);
Bitmap gif = GetBitmapResource("toaster.gif");
Point pos = GetRandomStartingPosition();
FlyingObject o = new FlyingObject(gif, virtualScreenBounds_i, pos.X, pos.Y, iSpeed);
objects_i.Add(o);
}
for (int i=0; i < NUM_TOASTS; i++)
{
int iSpeed = rnd_i.Next(1,10);
Bitmap gif = GetBitmapResource("toast.gif");
Point pos = GetRandomStartingPosition();
FlyingObject o = new FlyingObject(gif, virtualScreenBounds_i, pos.X, pos.Y, iSpeed);
objects_i.Add(o);
}
}
private Point GetRandomStartingPosition()
{
return new Point(rnd_i.Next(0,virtualScreenBounds_i.Width + virtualScreenBounds_i.Height), rnd_i.Next(0,virtualScreenBounds_i.Height + 100));
}
void MainFormMouseMove(object sender, MouseEventArgs e)
{
}
void MainFormMouseClick(object sender, MouseEventArgs e)
{
this.Close();
}
void MainFormKeyDown(object sender, KeyEventArgs e)
{
this.Close();
}
/// <summary>
/// Instructs the ImageAnimator to check to see whether any of our toasters
/// need to move to the next frame.
/// </summary>
private void AnimateImage()
{
if (! isCurrentlyAnimating_i)
{
foreach (FlyingObject o in objects_i)
{
ImageAnimator.Animate(o.Gif, new EventHandler(this.OnFrameChanged));
}
isCurrentlyAnimating_i = true;
}
}
/// <summary>
/// Fired when the ImageAnimator decides that a particular image/toaster needs to
/// change. It's probably not really required given that we call Invalidate() on
/// a pretty fast background timer anyway.
/// </summary>
void OnFrameChanged(object sender, EventArgs e)
{
this.Invalidate();
}
/// <summary>
/// Triggered by the Invalidate() method, this is where we draw the toasters
/// and toast, and also trigger another 'AnimateImage' cycle.
/// </summary>
void MainFormPaint(object sender, PaintEventArgs e)
{
AnimateImage();
ImageAnimator.UpdateFrames();
float fImageScalingFactor = 2 / fScreenScalingFactor_i;
if (isPreviewMode_i)
{
//
// Scale down using a high quality interpolation, so you can make
// out what the tiny objects are.
//
e.Graphics.InterpolationMode = InterpolationMode.HighQualityBicubic;
}
else
{
//
// Scale the images up using retro chunky interpolation, not this
// fancy new bicubic nonsense.
//
e.Graphics.InterpolationMode = InterpolationMode.NearestNeighbor;
}
foreach (FlyingObject o in objects_i)
{
//e.Graphics.DrawImage(o.Gif, o.Left, o.Top, o.Gif.Width*SCALE, o.Gif.Height*SCALE);
e.Graphics.DrawImage(o.Gif,
o.Left / fScreenScalingFactor_i,
o.Top / fScreenScalingFactor_i,
o.Gif.Width*fImageScalingFactor,
o.Gif.Height*fImageScalingFactor);
}
}
void TmrTopMostTick(object sender, EventArgs e)
{
//
// Ensure that this window is definitely the topmost. If I'm honest, this
// has just been added because one of my other projects
// (https://github.com/michaelomichael/OfficeChromeFix) doesn't detect screensavers
// just yet, and so tries to add a kind of window chrome to the fullscreen window!
//
this.TopMost = true;
}
void TmrAnimationTick(object sender, EventArgs e)
{
foreach (FlyingObject o in objects_i)
{
o.Move();
}
this.Invalidate();
}
}
}