/
MainForm.cs
128 lines (112 loc) · 4.16 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
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
namespace ThinkMachine
{
public partial class MainForm : Form
{
public MainForm(bool ScreenSaver, GoogleImageSearch.SearchSettings Settings)
{
InitializeComponent();
this._GoogleImageSearch = new GoogleImageSearch();
this._GoogleImageSearch.Settings = Settings;
this._AsyncImageSource = new AsyncImageSource();
this._AsyncImageSource.Source = this._GoogleImageSearch;
this._AsyncImageSource.MaxQueueSize = 5;
this._AsyncImageSource.ThreadAmount = 3;
RepeatImageSource ris = new RepeatImageSource();
this._FinalImageSource = ris;
ris.Source = this._AsyncImageSource;
ris.Reserve = 40;
Cursor.Hide();
Timer mtimer = new Timer();
mtimer.Interval = 250;
mtimer.Tick += new EventHandler(delegate
{
Image next = this._FinalImageSource.Next();
if (next != null)
{
this.ShownImage = next;
}
});
this._ScreenSaver = ScreenSaver;
this._Start = DateTime.Now;
mtimer.Start();
}
/// <summary>
/// The image currently shown by the form.
/// </summary>
public Image ShownImage
{
get
{
return this._ShownImage;
}
set
{
this._ShownImage = value;
this.Refresh();
}
}
protected override void OnPaint(PaintEventArgs e)
{
base.OnPaint(e);
e.Graphics.FillRectangle(Brushes.Black, e.ClipRectangle);
if (this._ShownImage != null)
{
int padding = 20;
Rectangle screenrect = e.ClipRectangle;
Rectangle imagerect = new Rectangle(0, 0, this._ShownImage.Width, this._ShownImage.Height);
screenrect = new Rectangle(screenrect.X + padding, screenrect.Y + padding,
screenrect.Width - (2 * padding), screenrect.Height - (2 * padding));
if (imagerect.Width * screenrect.Height > screenrect.Width * imagerect.Height)
{
imagerect.Height = screenrect.Width * imagerect.Height / imagerect.Width;
imagerect.Width = screenrect.Width;
imagerect.X = screenrect.X;
imagerect.Y = screenrect.Y + (screenrect.Height / 2) - (imagerect.Height / 2);
}
else
{
imagerect.Width = screenrect.Height * imagerect.Width / imagerect.Height;
imagerect.Height = screenrect.Height;
imagerect.X = screenrect.X + (screenrect.Width / 2) - (imagerect.Width / 2);
imagerect.Y = screenrect.Y;
}
e.Graphics.DrawImage(this._ShownImage, imagerect);
}
#if DEBUG
// Debug information
Font f = new Font(FontFamily.GenericMonospace, 12);
e.Graphics.DrawString(
"Downloaded Images: " + this._GoogleImageSearch.DownloadedImages,
f, Brushes.White, new Point(0, 0));
#endif
}
private void MainForm_KeyDown(object sender, KeyEventArgs e)
{
// Escape key exit
if (e.KeyData == Keys.Escape)
{
this.Close();
}
}
private void MainForm_MouseMove(object sender, MouseEventArgs e)
{
if (this._ScreenSaver && (DateTime.Now - this._Start) > TimeSpan.FromMilliseconds(500))
{
this.Close();
}
}
private Image _ShownImage;
private ImageSource _FinalImageSource;
private GoogleImageSearch _GoogleImageSearch;
private AsyncImageSource _AsyncImageSource;
private bool _ScreenSaver;
private DateTime _Start;
}
}