-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathForm1.cs
323 lines (286 loc) · 8.46 KB
/
Form1.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
using System;
using System.Drawing;
using System.Drawing.Imaging;
using System.Runtime.InteropServices;
using System.Windows.Forms;
using Transitions;
namespace Snipper
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
pnlTitleBar.MouseDown += new MouseEventHandler(DragForm);
lblTitle.MouseDown += new MouseEventHandler(DragForm);
pnlClose.Click += new EventHandler(CloseForm);
pnlMaximize.Click += new EventHandler(MaximizeForm);
pnlMinimize.Click += new EventHandler(MinimizeForm);
lblClose.Click += new EventHandler(CloseForm);
lblMaximize.Click += new EventHandler(MaximizeForm);
lblMinimize.Click += new EventHandler(MinimizeForm);
Native.HotKeys.RegisterHotKey(Handle, 0, Native.HotKeys.KeyModifiers.Alt, Keys.X); // point 1 of snapshot
Native.HotKeys.RegisterHotKey(Handle, 1, Native.HotKeys.KeyModifiers.Alt, Keys.Y); // point 2 of snapshot
Native.HotKeys.RegisterHotKey(Handle, 2, Native.HotKeys.KeyModifiers.Alt, Keys.S); // automatically snapshot screen
FormClosing += (s, e) =>
{
Native.HotKeys.UnregisterHotKey(Handle, 0);
Native.HotKeys.UnregisterHotKey(Handle, 1);
Native.HotKeys.UnregisterHotKey(Handle, 2);
};
}
#region Window Functionality
private struct Native
{
[DllImport("user32.dll")]
public static extern bool ReleaseCapture();
[DllImport("user32.dll")]
private static extern int SendMessage(IntPtr HWnd, int Msg, int WParam, int LParam);
public static int SendMessage(IntPtr HWnd, int Msg, int WParam) => SendMessage(HWnd, Msg, WParam, 0);
public struct HotKeys
{
[DllImport("user32.dll")]
public static extern bool RegisterHotKey(IntPtr HWnd, int ID, KeyModifiers FSModifiers, Keys VK);
[DllImport("user32.dll")]
public static extern bool UnregisterHotKey(IntPtr HWnd, int ID);
[Flags]
public enum KeyModifiers
{
None = 0,
Alt = 1,
Control = 2,
Shift = 4,
Windows = 8,
}
}
}
/// <summary>
/// Gets a snapshot of the screen based on the coordinates specified.
/// </summary>
/// <param name="X">The x-location of the snapshot from the screen.</param>
/// <param name="Y">The y-location of the snapshot from the screen.</param>
/// <param name="Width">The width of the snapshot from the screen.</param>
/// <param name="Height">The height of the snapshot from the screen.</param>
/// <returns>A bitmap containing a snaphshot of the screen based on the specified parameters.</returns>
private Bitmap ScreenSnip(int X, int Y, int Width, int Height)
{
Bitmap bmp = new Bitmap(Width, Height);
using (Graphics gfx = Graphics.FromImage(bmp))
{
gfx.CopyFromScreen(new Point(X, Y), new Point(0, 0), new Size(Width, Height), CopyPixelOperation.SourceCopy);
}
return bmp;
}
private void DragForm(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Left)
{
Native.ReleaseCapture();
Native.SendMessage(Handle, 161, 2);
}
}
private void CloseForm(object sender, EventArgs e)
{
Timer t = new Timer { Interval = 15 };
t.Tick += (s, ee) =>
{
Opacity -= 0.05;
if (Opacity <= 0)
{
t.Enabled = false;
Opacity = 0;
Close();
}
};
t.Start();
}
private void MaximizeForm(object sender, EventArgs e)
{
Timer t = new Timer { Interval = 15 };
t.Tick += (s, ee) =>
{
Opacity -= 0.05;
if (Opacity <= 0)
{
t.Enabled = false;
Opacity = 0;
switch (WindowState)
{
case FormWindowState.Maximized: WindowState = FormWindowState.Normal; break;
case FormWindowState.Normal: WindowState = FormWindowState.Maximized; break;
}
Timer t2 = new Timer { Interval = 15 };
t2.Tick += (ss, eee) =>
{
Opacity += 0.05;
if (Opacity >= 1)
{
t2.Enabled = false;
Opacity = 1;
}
};
t2.Start();
}
};
t.Start();
}
private void MinimizeForm(object sender, EventArgs e)
{
Timer t = new Timer { Interval = 15 };
t.Tick += (s, ee) =>
{
Opacity -= 0.05;
if (Opacity <= 0)
{
t.Enabled = false;
Opacity = 0;
WindowState = FormWindowState.Minimized;
}
};
t.Start();
}
protected override void OnShown(EventArgs e)
{
base.OnShown(e);
Opacity = 0;
Timer t = new Timer { Interval = 15 };
t.Tick += (s, ee) =>
{
Opacity += 0.05;
if (Opacity >= 1)
{
t.Enabled = false;
Opacity = 1;
}
};
t.Start();
}
protected override void OnActivated(EventArgs e)
{
base.OnActivated(e);
if (WindowState == FormWindowState.Minimized) Opacity = 0;
Timer t = new Timer { Interval = 15 };
t.Tick += (s, ee) =>
{
Opacity += 0.05;
if (Opacity >= 1)
{
t.Enabled = false;
Opacity = 1;
}
};
t.Start();
}
protected override void WndProc(ref Message m)
{
try
{
switch (WindowState)
{
case FormWindowState.Maximized: lblMaximize.Text = "2"; break;
case FormWindowState.Normal: lblMaximize.Text = "1"; break;
}
}
catch { return; }
try
{
if (m.Msg == 0x0312)
{
switch ((int)m.WParam)
{
case 0:
{
decimal x = Cursor.Position.X;
decimal y = Cursor.Position.Y;
numXLocation.Value = x;
numYLocation.Value = y;
}
break;
case 1:
{
decimal w = Cursor.Position.X - numXLocation.Value;
decimal h = Cursor.Position.Y - numYLocation.Value;
numWidth.Value = w;
numHeight.Value = h;
}
break;
case 2:
{
TakeSnapshot(this, null);
}
break;
}
}
}
catch { return; }
base.WndProc(ref m);
}
private void Close_MSEnter(object sender, EventArgs e)
{
Transition.run(pnlClose, "BackColor", Color.Gainsboro, new TransitionType_Linear(70));
Transition.run(lblClose, "BackColor", Color.Gainsboro, new TransitionType_Linear(70));
}
private void Close_MSLeave(object sender, EventArgs e)
{
Transition.run(pnlClose, "BackColor", SystemColors.Control, new TransitionType_Linear(70));
Transition.run(lblClose, "BackColor", SystemColors.Control, new TransitionType_Linear(70));
}
private void Maximize_MSEnter(object sender, EventArgs e)
{
Transition.run(pnlMaximize, "BackColor", Color.Gainsboro, new TransitionType_Linear(70));
Transition.run(lblMaximize, "BackColor", Color.Gainsboro, new TransitionType_Linear(70));
}
private void Maximize_MSLeave(object sender, EventArgs e)
{
Transition.run(pnlMaximize, "BackColor", SystemColors.Control, new TransitionType_Linear(70));
Transition.run(lblMaximize, "BackColor", SystemColors.Control, new TransitionType_Linear(70));
}
private void Minimize_MSEnter(object sender, EventArgs e)
{
Transition.run(pnlMinimize, "BackColor", Color.Gainsboro, new TransitionType_Linear(70));
Transition.run(lblMinimize, "BackColor", Color.Gainsboro, new TransitionType_Linear(70));
}
private void Minimize_MSLeave(object sender, EventArgs e)
{
Transition.run(pnlMinimize, "BackColor", SystemColors.Control, new TransitionType_Linear(70));
Transition.run(lblMinimize, "BackColor", SystemColors.Control, new TransitionType_Linear(70));
}
#endregion
private void TakeSnapshot(object sender, EventArgs e)
{
int x = (int)numXLocation.Value;
int y = (int)numYLocation.Value;
int w = (int)numWidth.Value;
int h = (int)numHeight.Value;
picSnapshot.Image = ScreenSnip(x, y, w, h);
}
private void SaveSnapshot(object sender, EventArgs e)
{
if (picSnapshot.Image != null)
{
using (SaveFileDialog sfd = new SaveFileDialog
{
Title = "Please choose where to save the snapshot to...",
Filter = "Bitmap|*.bmp|Joint Photographic Experts Groups|*.jpeg; *.jpg|Portable Network Graphics|*.png|Supported Types|*.bmp; *.jpeg; *.jpg; *.png",
FilterIndex = 6,
})
{
if (sfd.ShowDialog() == DialogResult.OK)
{
string imageFile = sfd.FileName;
string extension = imageFile.Split('.')[1];
switch (extension)
{
case "bmp": picSnapshot.Image.Save(imageFile, ImageFormat.Bmp); break;
case "jpeg": picSnapshot.Image.Save(imageFile, ImageFormat.Jpeg); break;
case "jpg": picSnapshot.Image.Save(imageFile, ImageFormat.Jpeg); break;
case "png": picSnapshot.Image.Save(imageFile, ImageFormat.Png); break;
}
}
}
}
else MessageBox.Show("A snapshot is required in order to save it to an image file.", "Snapshot Required",
MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
}