0
|
1 using System;
|
|
2 using System.Collections.Generic;
|
|
3 using System.ComponentModel;
|
|
4 using System.Data;
|
|
5 using System.Drawing;
|
|
6 using System.Drawing.Imaging;
|
|
7 using System.IO;
|
|
8 using System.Runtime.InteropServices;
|
|
9 using System.Text;
|
|
10 using System.Windows.Forms;
|
|
11 using ZXing;
|
|
12 using Microsoft.Win32;
|
|
13
|
|
14 /*
|
|
15 * Copyright (c) 2014 Brad Greco <brad@bgreco.net>
|
|
16 */
|
|
17 namespace QuickQR
|
|
18 {
|
|
19 public partial class QRForm : Form
|
|
20 {
|
|
21 const int WM_HOTKEY = 786;
|
|
22
|
|
23 private BarcodeWriter writer;
|
|
24 private KeyBinding keyBinding;
|
|
25 private String currentText;
|
|
26 private Settings settings;
|
|
27
|
|
28 [DllImport("user32.dll")]
|
|
29 private static extern int RegisterHotKey(IntPtr hwnd, int id, int fsModifiers, int vk);
|
|
30 [DllImport("user32.dll")]
|
|
31 private static extern int UnregisterHotKey(IntPtr hwnd, int id);
|
|
32
|
|
33 public QRForm()
|
|
34 {
|
|
35 InitializeComponent();
|
|
36 }
|
|
37
|
|
38 private void QRForm_Load(object sender, EventArgs e)
|
|
39 {
|
|
40 settings = new Settings();
|
|
41 loadSettings();
|
|
42 registerHotkey(keyBinding);
|
|
43 writer = new BarcodeWriter { Format = BarcodeFormat.QR_CODE };
|
|
44 writer.Options.Margin = 2;
|
|
45 fileHide.ShortcutKeyDisplayString = "Esc";
|
|
46 reset();
|
|
47 notifyIcon.Icon = new System.Drawing.Icon(this.Icon, 16, 16);
|
|
48 }
|
|
49
|
|
50 private void notifyIcon_DoubleClick(object sender, EventArgs e)
|
|
51 {
|
|
52 this.Show();
|
|
53 this.Activate();
|
|
54 generateFromClipboard();
|
|
55 }
|
|
56
|
|
57 private void generateFromClipboard()
|
|
58 {
|
|
59 if (Clipboard.ContainsText())
|
|
60 {
|
|
61 inputTextBox.Text = Clipboard.GetText();
|
|
62 generate(Clipboard.GetText());
|
|
63 }
|
|
64 else
|
|
65 {
|
|
66 reset();
|
|
67 }
|
|
68 }
|
|
69
|
|
70 private void generateFromTextBox()
|
|
71 {
|
|
72 if (inputTextBox.Text.Length > 0)
|
|
73 generate(inputTextBox.Text);
|
|
74 else
|
|
75 reset();
|
|
76 }
|
|
77
|
|
78 private void generate(String text)
|
|
79 {
|
|
80 System.Diagnostics.Debug.WriteLine("Generating: " + text);
|
|
81 currentText = text;
|
|
82 draw();
|
|
83 }
|
|
84
|
|
85 private void draw()
|
|
86 {
|
|
87 writer.Options.Width = QRImage.Width;
|
|
88 writer.Options.Height = QRImage.Height;
|
|
89 try
|
|
90 {
|
|
91 QRImage.Image = writer.Write(currentText);
|
|
92 }
|
|
93 catch (Exception e)
|
|
94 {
|
|
95 System.Diagnostics.Debug.WriteLine(e.Message);
|
|
96 showError(e.Message);
|
|
97 return;
|
|
98 }
|
|
99 finally
|
|
100 {
|
|
101 inputTextBox.SelectAll();
|
|
102 inputTextBox.Focus();
|
|
103 }
|
|
104 if (QRLabel.ForeColor != Color.Black)
|
|
105 QRLabel.ForeColor = Color.Black;
|
|
106 QRLabel.Text = currentText;
|
|
107 fileSaveAs.Enabled = true;
|
|
108 QRImage.ContextMenuStrip = imageMenu;
|
|
109 }
|
|
110
|
|
111 private void registerHotkey(KeyBinding hotkey)
|
|
112 {
|
|
113 settings.setHotkey(hotkey);
|
|
114 unregisterHotkey();
|
|
115 if (hotkey.key > 0)
|
|
116 RegisterHotKey(this.Handle, hotkey.getID(), hotkey.sum(), hotkey.key);
|
|
117 keyBinding = hotkey;
|
|
118 }
|
|
119
|
|
120 private void unregisterHotkey()
|
|
121 {
|
|
122 if (keyBinding.key > 0)
|
|
123 UnregisterHotKey(this.Handle, keyBinding.getID());
|
|
124 }
|
|
125
|
|
126 private void showHotkeyForm()
|
|
127 {
|
|
128 unregisterHotkey();
|
|
129 SetHotkey form = new SetHotkey(keyBinding);
|
|
130 if (form.ShowDialog() == DialogResult.OK)
|
|
131 registerHotkey(form.keyBinding);
|
|
132 form.Dispose();
|
|
133 }
|
|
134
|
|
135 private void loadSettings()
|
|
136 {
|
|
137 optionWinStart.Checked = menuAutorun.Checked = settings.autostart;
|
|
138 optionBackground.Checked = settings.background;
|
|
139 keyBinding = new KeyBinding(settings.hotkey);
|
|
140 registerAutostart();
|
|
141 }
|
|
142
|
|
143 private void registerAutostart()
|
|
144 {
|
|
145 RegistryKey key = Registry.CurrentUser.OpenSubKey("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run", true);
|
|
146 if (settings.autostart)
|
|
147 key.SetValue("QuickQR", Application.ExecutablePath.ToString());
|
|
148 else
|
|
149 key.DeleteValue("QuickQR", false);
|
|
150 }
|
|
151
|
|
152 private void showError(String message)
|
|
153 {
|
|
154 QRLabel.ForeColor = Color.Red;
|
|
155 QRLabel.Text = message;
|
|
156 }
|
|
157
|
|
158 private void reset()
|
|
159 {
|
|
160 inputTextBox.Text = string.Empty;
|
|
161 QRImage.Image = null;
|
|
162 QRLabel.Text = String.Empty;
|
|
163 inputTextBox.Focus();
|
|
164 currentText = string.Empty;
|
|
165 fileSaveAs.Enabled = false;
|
|
166 QRImage.ContextMenuStrip = null;
|
|
167 }
|
|
168
|
|
169 private void toggleBackground()
|
|
170 {
|
|
171 settings.setBackground(!settings.background);
|
|
172 optionBackground.Checked = settings.background;
|
|
173 }
|
|
174
|
|
175 private void toggleAutorun()
|
|
176 {
|
|
177 settings.setAutostart(!settings.autostart);
|
|
178 optionWinStart.Checked = menuAutorun.Checked = settings.autostart;
|
|
179 registerAutostart();
|
|
180 }
|
|
181
|
|
182 private void saveImage()
|
|
183 {
|
|
184 SaveFileDialog dialog = new SaveFileDialog();
|
|
185 dialog.DefaultExt = "png";
|
|
186 dialog.Filter = "PNG images (*.png)|*.png|All files (*.*)|*.*";
|
|
187 String fileName = currentText;
|
|
188 foreach (char c in Path.GetInvalidFileNameChars())
|
|
189 {
|
|
190 fileName = fileName.Replace(c.ToString(), "");
|
|
191 }
|
|
192 dialog.FileName = fileName;
|
|
193 if (dialog.ShowDialog() == DialogResult.OK)
|
|
194 {
|
|
195 QRImage.Image.Save(dialog.FileName, ImageFormat.Png);
|
|
196 }
|
|
197 dialog.Dispose();
|
|
198 }
|
|
199
|
|
200 [System.Security.Permissions.PermissionSet(System.Security.Permissions.SecurityAction.Demand, Name = "FullTrust")]
|
|
201 protected override void WndProc(ref Message m)
|
|
202 {
|
|
203 // Listen for operating system messages.
|
|
204 switch (m.Msg)
|
|
205 {
|
|
206 case WM_HOTKEY:
|
|
207 System.Diagnostics.Debug.WriteLine(m.WParam.ToString());
|
|
208 if (m.WParam.ToInt32() == keyBinding.getID())
|
|
209 {
|
|
210 System.Diagnostics.Debug.WriteLine("Our hotkey received");
|
|
211 this.Show();
|
|
212 this.Activate();
|
|
213 generateFromClipboard();
|
|
214 }
|
|
215 break;
|
|
216 }
|
|
217 base.WndProc(ref m);
|
|
218 }
|
|
219
|
|
220 protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
|
|
221 {
|
|
222 if (keyData == Keys.Escape)
|
|
223 {
|
|
224 if (settings.background)
|
|
225 this.Hide();
|
|
226 else
|
|
227 Application.Exit();
|
|
228 return true;
|
|
229 }
|
|
230 return base.ProcessCmdKey(ref msg, keyData);
|
|
231 }
|
|
232
|
|
233 private void QRForm_Resize(object sender, EventArgs e)
|
|
234 {
|
|
235 if (currentText != string.Empty)
|
|
236 draw();
|
|
237 }
|
|
238
|
|
239 private void inputTextBox_KeyDown(object sender, KeyEventArgs e)
|
|
240 {
|
|
241 if (e.KeyCode == Keys.Enter)
|
|
242 generateFromTextBox();
|
|
243 }
|
|
244
|
|
245 private void generateButton_Click(object sender, EventArgs e)
|
|
246 {
|
|
247 generateFromTextBox();
|
|
248 }
|
|
249
|
|
250 private void fileHide_Click(object sender, EventArgs e)
|
|
251 {
|
|
252 this.Hide();
|
|
253 }
|
|
254
|
|
255 private void fileExit_Click(object sender, EventArgs e)
|
|
256 {
|
|
257 Application.Exit();
|
|
258 }
|
|
259
|
|
260 private void menuGenerateClipboard_Click(object sender, EventArgs e)
|
|
261 {
|
|
262 this.Show();
|
|
263 generateFromClipboard();
|
|
264 }
|
|
265
|
|
266 private void menuNew_Click(object sender, EventArgs e)
|
|
267 {
|
|
268 this.Show();
|
|
269 reset();
|
|
270 }
|
|
271
|
|
272 private void exitToolStripMenuItem_Click(object sender, EventArgs e)
|
|
273 {
|
|
274 Application.Exit();
|
|
275 }
|
|
276
|
|
277 private void QRForm_FormClosing(object sender, FormClosingEventArgs e)
|
|
278 {
|
|
279 System.Diagnostics.Debug.WriteLine("Reason: " + e.CloseReason);
|
|
280 if (settings.background && (e.CloseReason == CloseReason.None || e.CloseReason == CloseReason.UserClosing))
|
|
281 {
|
|
282 this.Hide();
|
|
283 e.Cancel = true;
|
|
284 }
|
|
285 }
|
|
286
|
|
287 private void optionHotkey_Click(object sender, EventArgs e)
|
|
288 {
|
|
289 showHotkeyForm();
|
|
290 }
|
|
291
|
|
292 private void menuAutorun_Click(object sender, EventArgs e)
|
|
293 {
|
|
294 toggleAutorun();
|
|
295 }
|
|
296
|
|
297 private void menuHotkey_Click(object sender, EventArgs e)
|
|
298 {
|
|
299 showHotkeyForm();
|
|
300 }
|
|
301
|
|
302 private void optionWinStart_Click(object sender, EventArgs e)
|
|
303 {
|
|
304 toggleAutorun();
|
|
305 }
|
|
306
|
|
307 private void optionBackground_Click(object sender, EventArgs e)
|
|
308 {
|
|
309 toggleBackground();
|
|
310 }
|
|
311
|
|
312 private void imageCopy_Click(object sender, EventArgs e)
|
|
313 {
|
|
314 Clipboard.SetImage(QRImage.Image);
|
|
315 }
|
|
316
|
|
317 private void fileSaveAs_Click(object sender, EventArgs e)
|
|
318 {
|
|
319 saveImage();
|
|
320 }
|
|
321
|
|
322 private void imageSaveAs_Click(object sender, EventArgs e)
|
|
323 {
|
|
324 saveImage();
|
|
325 }
|
|
326
|
|
327 private void helpAbout_Click(object sender, EventArgs e)
|
|
328 {
|
|
329 AboutForm form = new AboutForm();
|
|
330 form.ShowDialog();
|
|
331 form.Dispose();
|
|
332 }
|
|
333 }
|
|
334 }
|