-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMainForm.cs
331 lines (278 loc) · 7.89 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
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Drawing.Imaging;
using System.Runtime.InteropServices;
using System.Windows.Forms;
namespace Xnipper
{
public partial class MainForm : Form
{
public MainForm()
{
InitializeComponent();
m_aeroEnabled = false;
Bitmap iconBmp = new Bitmap(picIcon.Width, picIcon.Height);
using (Graphics g = Graphics.FromImage(iconBmp))
g.DrawIcon(Icon, new Rectangle(0, 0, picIcon.Width, picIcon.Height));
picIcon.Image = iconBmp;
RegisterHotKey(Handle, 0, Modifiers.Alt | Modifiers.Shift, Keys.D1);
RegisterHotKey(Handle, 1, Modifiers.Alt | Modifiers.Shift, Keys.D2);
RegisterHotKey(Handle, 2, Modifiers.Alt | Modifiers.Shift, Keys.S);
regionPoint1 = new Point(imageRegion.X, imageRegion.Y);
regionPoint2 = new Point(imageRegion.X + imageRegion.Width, imageRegion.Y + imageRegion.Height);
int index = 0;
foreach (Screen screen in Screen.AllScreens)
{
ToolStripMenuItem displayMenuItem = new ToolStripMenuItem();
displayMenuItem.Name = $"display{index + 1}ToolStripMenuItem";
displayMenuItem.Text = $"Display {index + 1}";
displayMenuItem.Click += (s, e) =>
{
imageRegion = screen.Bounds;
};
viewToolStripMenuItem.DropDownItems.Insert(viewToolStripMenuItem.DropDownItems.Count - 2, displayMenuItem);
index++;
}
}
[Flags]
private enum Modifiers
{
None = 0,
Alt = 1,
Control = 2,
Shift = 4,
}
[DllImport("user32.dll")]
private static extern bool ReleaseCapture();
[DllImport("user32.dll")]
private static extern int SendMessage(IntPtr hWnd, int Msg, int wParam, int lParam);
[DllImport("user32.dll")]
private static extern bool RegisterHotKey(IntPtr hWnd, int Id, Modifiers fsModifiers, Keys vk);
[DllImport("user32.dll")]
private static extern bool UnregisterHotKey(IntPtr hWnd, int Id);
private Rectangle imageRegion = Screen.PrimaryScreen.Bounds;
private Point regionPoint1, regionPoint2;
private Image screenshot;
private Dictionary<string, int> resizeBorders = new Dictionary<string, int>
{
{ "Left1", 10 },
{ "Left2", 10 },
{ "Left3", 10 },
{ "Right1", 11 },
{ "Right2", 11 },
{ "Right3", 11 },
{ "Top", 12 },
{ "TopLeft", 13 },
{ "TopRight", 14 },
{ "Bottom", 15 },
{ "BottomLeft", 16 },
{ "BottomRight", 17 },
};
private void CalculateImageRegion()
{
// taken from:
// https://stackoverflow.com/questions/4103943/how-to-create-a-rectanglef-using-two-pointf
imageRegion = new Rectangle(
Math.Min(regionPoint1.X, regionPoint2.X),
Math.Min(regionPoint1.Y, regionPoint2.Y),
Math.Abs(regionPoint1.X - regionPoint2.X),
Math.Abs(regionPoint1.Y - regionPoint2.Y)
);
}
private void TakeScreenshot()
{
screenshot = new Bitmap(imageRegion.Width, imageRegion.Height);
using (Graphics g = Graphics.FromImage(screenshot))
g.CopyFromScreen(imageRegion.Location, new Point(0, 0), imageRegion.Size, CopyPixelOperation.SourceCopy);
picScreenshot.Image = screenshot;
GC.Collect();
}
private void DragForm(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Left)
{
ReleaseCapture();
SendMessage(Handle, 161, 2, 0);
}
}
private void ResizeForm(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Left)
{
ReleaseCapture();
SendMessage(Handle, 161, resizeBorders[((PictureBox)sender).Name.Substring(3)], 0);
}
}
private void CloseForm(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Left)
Application.Exit();
}
private void MaximizeForm(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Left)
{
if (WindowState == FormWindowState.Maximized)
{
WindowState = FormWindowState.Normal;
lblMaximize.Text = "1";
}
else if (WindowState == FormWindowState.Normal)
{
WindowState = FormWindowState.Maximized;
lblMaximize.Text = "2";
}
}
}
private void MinimizeForm(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Left)
WindowState = FormWindowState.Minimized;
}
private void CloseForm_Enter(object sender, EventArgs e)
{
lblClose.BackColor = Color.Silver;
}
private void CloseForm_Leave(object sender, EventArgs e)
{
lblClose.BackColor = Color.Gainsboro;
}
private void MaximizeForm_Enter(object sender, EventArgs e)
{
lblMaximize.BackColor = Color.Silver;
}
private void MaximizeForm_Leave(object sender, EventArgs e)
{
lblMaximize.BackColor = Color.Gainsboro;
}
private void MinimizeForm_Enter(object sender, EventArgs e)
{
lblMinimize.BackColor = Color.Silver;
}
private void MinimizeForm_Leave(object sender, EventArgs e)
{
lblMinimize.BackColor = Color.Gainsboro;
}
private void TakeScreenshot_Click(object sender, EventArgs e)
{
TakeScreenshot();
}
private void EditRegion_Click(object sender, EventArgs e)
{
using (RegionEditForm regionEdit = new RegionEditForm(this, imageRegion))
{
regionEdit.ShowDialog();
if (regionEdit.Confirmed)
imageRegion = regionEdit.ImageRegion;
}
}
private void DisplayShortcuts_Click(object sender, EventArgs e)
{
MessageBox.Show("Take Screenshot: Alt + Shift + S\n" +
"Set Region Point #1: Alt + Shift + 1\n" +
"Set Region Point #2: Alt + Shift + 2", "Xnipper Shortcuts",
MessageBoxButtons.OK, MessageBoxIcon.Question);
}
private void ResetScreenshot_Click(object sender, EventArgs e)
{
screenshot = null;
picScreenshot.Image = null;
}
private void SaveScreenshot_Click(object sender, EventArgs e)
{
if (screenshot != null)
{
using (SaveFileDialog sfd = new SaveFileDialog
{
Title = "Please choose where to save the screenshot...",
Filter = "Image|*.jpeg; *.jpg",
}) if (sfd.ShowDialog() == DialogResult.OK)
screenshot.Save(sfd.FileName, ImageFormat.Jpeg);
}
else MessageBox.Show("No screenshot was taken.", "No Screenshot To Save",
MessageBoxButtons.OK, MessageBoxIcon.Error);
}
protected override void OnFormClosing(FormClosingEventArgs e)
{
base.OnFormClosing(e);
UnregisterHotKey(Handle, 0);
UnregisterHotKey(Handle, 1);
UnregisterHotKey(Handle, 2);
}
[DllImport("dwmapi.dll")]
private static extern int DwmExtendFrameIntoClientArea(IntPtr hWnd, ref MARGINS pMarInset);
[DllImport("dwmapi.dll")]
private static extern int DwmSetWindowAttribute(IntPtr hWnd, int attr, ref int attrValue, int attrSize);
[DllImport("dwmapi.dll")]
private static extern int DwmIsCompositionEnabled(ref int pfEnabled);
private bool m_aeroEnabled;
private struct MARGINS
{
public int leftWidth;
public int rightWidth;
public int topHeight;
public int bottomHeight;
}
protected override void WndProc(ref Message m)
{
if (m_aeroEnabled)
{
int v = 2;
DwmSetWindowAttribute(Handle, 2, ref v, 4);
MARGINS margins = new MARGINS
{
bottomHeight = 0,
topHeight = 1,
leftWidth = 0,
rightWidth = 0,
};
DwmExtendFrameIntoClientArea(Handle, ref margins);
}
if (m.Msg == 0x0312)
{
switch ((int)m.WParam)
{
case 0:
regionPoint1 = MousePosition;
CalculateImageRegion();
break;
case 1:
regionPoint2 = MousePosition;
CalculateImageRegion();
break;
case 2:
TakeScreenshot();
picScreenshot.Image = screenshot;
break;
}
}
base.WndProc(ref m);
}
protected override CreateParams CreateParams
{
get
{
CreateParams cp = base.CreateParams;
m_aeroEnabled = CheckAeroEnabled();
if (!m_aeroEnabled)
{
const int CS_DROPSHADOW = 0x20000;
cp = base.CreateParams;
cp.ClassStyle |= CS_DROPSHADOW;
}
return cp;
}
}
private bool CheckAeroEnabled()
{
if (Environment.OSVersion.Version.Major >= 6)
{
int enabled = 0;
DwmIsCompositionEnabled(ref enabled);
return (enabled == 1) ? true : false;
}
return false;
}
}
}