This repository has been archived by the owner on Feb 3, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathfrm_Main.cs
350 lines (292 loc) · 10.2 KB
/
frm_Main.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
using System;
using System.IO;
using System.Net;
using System.Text;
using System.Drawing;
using System.Diagnostics;
using System.Windows.Forms;
using System.ComponentModel;
using System.Collections.Specialized;
namespace hyperdesktop2
{
public partial class frm_Main : Form
{
Hotkeys hook;
Boolean snipper_open;
#region Main Form
public frm_Main()
{
InitializeComponent();
// Delete older executable on update
try {
if(Convert.ToInt32(Settings.Read("hyperdesktop2", "build")) < Settings.build) {
File.Delete(Settings.exe_path);
Settings.Write("hyperdesktop2", "build", Settings.build.ToString());
}
} catch (Exception ex) {
Console.WriteLine("Couldn't delete old hyperdesktop2 executable.");
Console.WriteLine(ex.Message);
}
Global_Func.app_data_folder_create();
Global_Func.copy_files();
var web_client = new WebClient();
Int32 build = Convert.ToInt32(web_client.DownloadString(Settings.build_url));
// Confirm if user wants to add to system startup
// on first run
if(!File.Exists(Settings.ini_path)) {
DialogResult result = MessageBox.Show(
"Do you want to run Hyperdesktop2 at Windows startup?",
"First time run",
MessageBoxButtons.YesNo,
MessageBoxIcon.Question,
MessageBoxDefaultButton.Button1
);
if(result == DialogResult.Yes)
Global_Func.run_at_startup(true);
}
// Update notification
else if(build > Settings.build) {
DialogResult result = MessageBox.Show(
"A new version of Hyperdesktop2 has been released. Would you like to download it?",
"Update available",
MessageBoxButtons.YesNo,
MessageBoxIcon.Question,
MessageBoxDefaultButton.Button1
);
if(result == DialogResult.Yes) {
Process.Start(Settings.release_url);
Process.GetCurrentProcess().Kill();
}
}
register_hotkeys();
}
void Frm_MainLoad(object sender, EventArgs e)
{
Imgur.web_client.UploadProgressChanged += upload_progress_changed;
Imgur.web_client.UploadValuesCompleted += upload_progress_complete;
Settings.get_settings();
tray_icon.Visible = true;
Screen_Bounds.load();
}
void Frm_MainFormClosing(object sender, FormClosingEventArgs e)
{
inverse_tray_options(sender, e);
e.Cancel = true;
}
#endregion
#region Tray Icon
void balloon_tip(String text, String title, Int32 duration, ToolTipIcon icon = ToolTipIcon.Info) {
tray_icon.BalloonTipText = text;
tray_icon.BalloonTipTitle = title;
tray_icon.BalloonTipIcon = icon;
tray_icon.ShowBalloonTip(duration);
}
void Tray_iconBalloonTipClicked(object sender, System.EventArgs e)
{
Process.Start(tray_icon.BalloonTipText);
}
void inverse_tray_options(object sender, EventArgs e)
{
minimizeToTrayToolStripMenuItem.Text = (minimizeToTrayToolStripMenuItem.Text == "Open Window") ? "Minimize to Tray" : "Open Window";
ShowInTaskbar = !ShowInTaskbar;
Opacity = Opacity < 1 ? 100 : 0;
}
#endregion
#region Drag and Drop
void drag_enter(object sender, DragEventArgs e)
{
e.Effect = e.Data.GetDataPresent(DataFormats.FileDrop) ? DragDropEffects.Move : DragDropEffects.None;
}
void drag_drop(object sender, DragEventArgs e)
{
var files = (String[])e.Data.GetData(DataFormats.FileDrop);
foreach(String file in files) {
work_image(new Bitmap(Image.FromFile(file)));
}
}
#endregion
#region Hotkeys
public void register_hotkeys()
{
hook = new Hotkeys();
hook.KeyPressed += hook_KeyPressed;
try {
hook.RegisterHotKey(global::ModifierKeys.Control | global::ModifierKeys.Shift, Keys.D3);
hook.RegisterHotKey(global::ModifierKeys.Control | global::ModifierKeys.Shift, Keys.D4);
hook.RegisterHotKey(global::ModifierKeys.Control | global::ModifierKeys.Shift, Keys.D5);
} catch {
MessageBox.Show("Couldn't register hotkeys. Perhaps they are already in use or try running as an Administrator.", "Error!", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
void hook_KeyPressed(object sender, KeyPressedEventArgs e)
{
switch(e.Key) {
case Keys.D3:
screen_capture("screen");
break;
case Keys.D4:
screen_capture("region");
break;
case Keys.D5:
screen_capture("window");
break;
}
}
#endregion
#region Image
Bitmap edit_screenshot(Bitmap bmp)
{
if (!Settings.edit_screenshot)
return null;
var edit = new frm_Edit(bmp);
edit.ShowDialog();
return edit.Result;
}
void save_screenshot(Bitmap bmp, String name = null)
{
if (!Settings.save_screenshots)
return;
if (name == null)
name = DateTime.Now.ToString("yyyy-MM-dd_HHmmss");
try
{
bmp.Save(
String.Format("{0}/{1}.{2}", Settings.save_folder, name, Settings.save_format),
Global_Func.ext_to_imageformat(Settings.save_format)
);
}
catch (Exception ex)
{
Console.WriteLine("Cannot save image.");
Console.WriteLine(ex.Message);
}
}
void screen_capture(String type)
{
Bitmap bmp = null;
switch (type)
{
case "screen":
bmp = Screen_Capture.screen(Settings.show_cursor);
break;
case "window":
bmp = Screen_Capture.window(Settings.show_cursor);
break;
default:
if (snipper_open)
return;
snipper_open = true;
var rect = frm_Snipper.get_region();
if(rect == new Rectangle(0, 0, 0, 0))
return;
bmp = Screen_Capture.region(rect);
snipper_open = false;
break;
}
work_image(bmp, true);
}
void work_image(Bitmap bmp, Boolean edit = false)
{
Global_Func.play_sound("capture.wav");
if (edit)
bmp = edit_screenshot(bmp);
if (bmp == null)
return;
if (Settings.upload_method == "imgur")
if (!Imgur.upload(bmp))
{
Global_Func.play_sound("error.wav");
if (Settings.balloon_messages)
balloon_tip("Error uploading file!", "Error", 2000, ToolTipIcon.Error);
}
save_screenshot(bmp);
}
#endregion
#region Buttons
void Btn_browseClick(object sender, EventArgs e)
{
var dialog = new OpenFileDialog();
dialog.Filter = "PNG|*.png|JPG|*.jpg|BMP|*.bmp|All Files (*.*)|*.*";
if(dialog.ShowDialog() == DialogResult.OK) {
work_image(new Bitmap(Image.FromFile(dialog.FileName)));
}
}
void Btn_captureClick(object sender, EventArgs e) { screen_capture("screen"); }
void Btn_windowClick() { screen_capture("window"); }
void Btn_capture_regionClick(object sender, EventArgs e) { screen_capture("region"); }
#endregion
#region Main Menu
void PreferencesToolStripMenuItemClick(object sender, EventArgs e)
{
new frm_Preferences().ShowDialog();
}
void ExitToolStripMenuItemClick(object sender, EventArgs e)
{
this.Dispose();
}
void AboutToolStripMenuItemClick(object sender, EventArgs e)
{
new frm_About().ShowDialog();
}
void RegisterHotkeysToolStripMenuItemClick(object sender, System.EventArgs e)
{
register_hotkeys();
}
#endregion
#region Image Links Menu
void OpenToolStripMenuItemClick(object sender, EventArgs e)
{
if(list_image_links.SelectedItems.Count > 0)
Process.Start(list_image_links.SelectedItems[0].Text);
}
void CopyToolStripMenuItemClick(object sender, EventArgs e)
{
if(list_image_links.SelectedItems.Count > 0)
Clipboard.SetText(list_image_links.SelectedItems[0].Text);
}
void DeleteToolStripMenuItemClick(object sender, EventArgs e)
{
if(list_image_links.SelectedItems.Count <= 0)
return;
if(Imgur.delete(list_image_links.SelectedItems[0].SubItems[1].Text))
list_image_links.SelectedItems[0].Remove();
else
{
Global_Func.play_sound("error.wav");
if (Settings.balloon_messages)
balloon_tip("Could not delete file!", "Error", 2000, ToolTipIcon.Error);
}
}
#endregion
#region Progress Bar
void upload_progress_changed(object sender, UploadProgressChangedEventArgs e)
{
try {
Int32 percent = Convert.ToInt32(e.BytesSent / e.TotalBytesToSend) * 100;
group_upload_progress.Text = String.Format("Upload Progress - {0}% ({1}kb/{2}kb)", percent, e.BytesSent / 1024, e.TotalBytesToSend / 1024);
progress.Value = percent;
} catch {
// below .NET 4.0, sometimes it throws an absurd
// number into the ProgressPercentage
}
}
void upload_progress_complete(object sender, UploadValuesCompletedEventArgs e)
{
group_upload_progress.Text = "Upload Progress";
progress.Value = 0;
String response = Encoding.UTF8.GetString(e.Result);
String delete_hash = Global_Func.get_text_inbetween(response, "deletehash\":\"", "\",\"name\"").Replace("\\", "");
String link = Global_Func.get_text_inbetween(response, "link\":\"", "\"}").Replace("\\", "");
list_image_links.Items.Add(
new ListViewItem(new String[] {link, delete_hash})
);
list_image_links.Items[list_image_links.Items.Count-1].EnsureVisible();
if(Settings.copy_links_to_clipboard)
Clipboard.SetText(link);
if(Settings.balloon_messages)
balloon_tip(link, "Upload Complete!", 2000);
Global_Func.play_sound("success.wav");
}
#endregion
}
}