diff QRForm.cs @ 0:aca8706f4eec default tip

Initial commit
author Brad Greco <brad@bgreco.net>
date Mon, 13 Oct 2014 21:28:19 -0500
parents
children
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/QRForm.cs	Mon Oct 13 21:28:19 2014 -0500
@@ -0,0 +1,334 @@
+using System;
+using System.Collections.Generic;
+using System.ComponentModel;
+using System.Data;
+using System.Drawing;
+using System.Drawing.Imaging;
+using System.IO;
+using System.Runtime.InteropServices;
+using System.Text;
+using System.Windows.Forms;
+using ZXing;
+using Microsoft.Win32;
+
+/*
+ * Copyright (c) 2014 Brad Greco <brad@bgreco.net>
+ */
+namespace QuickQR
+{
+    public partial class QRForm : Form
+    {
+        const int WM_HOTKEY = 786;
+
+        private BarcodeWriter writer;
+        private KeyBinding keyBinding;
+        private String currentText;
+        private Settings settings;
+
+        [DllImport("user32.dll")]
+        private static extern int RegisterHotKey(IntPtr hwnd, int id, int fsModifiers, int vk);
+        [DllImport("user32.dll")]
+        private static extern int UnregisterHotKey(IntPtr hwnd, int id);
+
+        public QRForm()
+        {
+            InitializeComponent();
+        }
+
+        private void QRForm_Load(object sender, EventArgs e)
+        {
+            settings = new Settings();
+            loadSettings();
+            registerHotkey(keyBinding);
+            writer = new BarcodeWriter { Format = BarcodeFormat.QR_CODE };
+            writer.Options.Margin = 2;
+            fileHide.ShortcutKeyDisplayString = "Esc";
+            reset();
+            notifyIcon.Icon = new System.Drawing.Icon(this.Icon, 16, 16);
+        }
+
+        private void notifyIcon_DoubleClick(object sender, EventArgs e)
+        {
+            this.Show();
+            this.Activate();
+            generateFromClipboard();
+        }
+
+        private void generateFromClipboard()
+        {
+            if (Clipboard.ContainsText())
+            {
+                inputTextBox.Text = Clipboard.GetText();
+                generate(Clipboard.GetText());
+            }
+            else
+            {
+                reset();
+            }
+        }
+
+        private void generateFromTextBox()
+        {
+            if (inputTextBox.Text.Length > 0)
+                generate(inputTextBox.Text);
+            else
+                reset();
+        }
+
+        private void generate(String text)
+        {
+            System.Diagnostics.Debug.WriteLine("Generating: " + text);
+            currentText = text;
+            draw();
+        }
+
+        private void draw()
+        {
+            writer.Options.Width = QRImage.Width;
+            writer.Options.Height = QRImage.Height;
+            try
+            {
+                QRImage.Image = writer.Write(currentText);
+            }
+            catch (Exception e)
+            {
+                System.Diagnostics.Debug.WriteLine(e.Message);
+                showError(e.Message);
+                return;
+            }
+            finally
+            {
+                inputTextBox.SelectAll();
+                inputTextBox.Focus();
+            }
+            if (QRLabel.ForeColor != Color.Black)
+                QRLabel.ForeColor = Color.Black;
+            QRLabel.Text = currentText;
+            fileSaveAs.Enabled = true;
+            QRImage.ContextMenuStrip = imageMenu;
+        }
+
+        private void registerHotkey(KeyBinding hotkey)
+        {
+            settings.setHotkey(hotkey);
+            unregisterHotkey();
+            if (hotkey.key > 0)
+                RegisterHotKey(this.Handle, hotkey.getID(), hotkey.sum(), hotkey.key);
+            keyBinding = hotkey;
+        }
+
+        private void unregisterHotkey()
+        {
+            if (keyBinding.key > 0)
+                UnregisterHotKey(this.Handle, keyBinding.getID());
+        }
+
+        private void showHotkeyForm()
+        {
+            unregisterHotkey();
+            SetHotkey form = new SetHotkey(keyBinding);
+            if (form.ShowDialog() == DialogResult.OK)
+                registerHotkey(form.keyBinding);
+            form.Dispose();
+        }
+
+        private void loadSettings()
+        {
+            optionWinStart.Checked = menuAutorun.Checked = settings.autostart;
+            optionBackground.Checked = settings.background;
+            keyBinding = new KeyBinding(settings.hotkey);
+            registerAutostart();
+        }
+
+        private void registerAutostart()
+        {
+            RegistryKey key = Registry.CurrentUser.OpenSubKey("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run", true);
+            if (settings.autostart)
+                key.SetValue("QuickQR", Application.ExecutablePath.ToString());
+            else
+                key.DeleteValue("QuickQR", false);
+        }
+
+        private void showError(String message)
+        {
+            QRLabel.ForeColor = Color.Red;
+            QRLabel.Text = message;
+        }
+
+        private void reset()
+        {
+            inputTextBox.Text = string.Empty;
+            QRImage.Image = null;
+            QRLabel.Text = String.Empty;
+            inputTextBox.Focus();
+            currentText = string.Empty;
+            fileSaveAs.Enabled = false;
+            QRImage.ContextMenuStrip = null;
+        }
+
+        private void toggleBackground()
+        {
+            settings.setBackground(!settings.background);
+            optionBackground.Checked = settings.background;
+        }
+
+        private void toggleAutorun()
+        {
+            settings.setAutostart(!settings.autostart);
+            optionWinStart.Checked = menuAutorun.Checked = settings.autostart;
+            registerAutostart();
+        }
+
+        private void saveImage()
+        {
+            SaveFileDialog dialog = new SaveFileDialog();
+            dialog.DefaultExt = "png";
+            dialog.Filter = "PNG images (*.png)|*.png|All files (*.*)|*.*";
+            String fileName = currentText;
+            foreach (char c in Path.GetInvalidFileNameChars())
+            {
+                fileName = fileName.Replace(c.ToString(), "");
+            }
+            dialog.FileName = fileName;
+            if (dialog.ShowDialog() == DialogResult.OK)
+            {
+                QRImage.Image.Save(dialog.FileName, ImageFormat.Png);
+            }
+            dialog.Dispose();
+        }
+
+        [System.Security.Permissions.PermissionSet(System.Security.Permissions.SecurityAction.Demand, Name = "FullTrust")]
+        protected override void WndProc(ref Message m)
+        {
+            // Listen for operating system messages.
+            switch (m.Msg)
+            {
+                case WM_HOTKEY:
+                    System.Diagnostics.Debug.WriteLine(m.WParam.ToString());
+                    if (m.WParam.ToInt32() == keyBinding.getID())
+                    {
+                        System.Diagnostics.Debug.WriteLine("Our hotkey received");
+                        this.Show();
+                        this.Activate();
+                        generateFromClipboard();
+                    }
+                    break;
+            }
+            base.WndProc(ref m);
+        }
+
+        protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
+        {
+            if (keyData == Keys.Escape)
+            {
+                if (settings.background)
+                    this.Hide();
+                else
+                    Application.Exit();
+                return true;
+            }
+            return base.ProcessCmdKey(ref msg, keyData);
+        }
+
+        private void QRForm_Resize(object sender, EventArgs e)
+        {
+            if (currentText != string.Empty)
+                draw();
+        }
+
+        private void inputTextBox_KeyDown(object sender, KeyEventArgs e)
+        {
+            if (e.KeyCode == Keys.Enter)
+                generateFromTextBox();
+        }
+
+        private void generateButton_Click(object sender, EventArgs e)
+        {
+            generateFromTextBox();
+        }
+
+        private void fileHide_Click(object sender, EventArgs e)
+        {
+            this.Hide();
+        }
+
+        private void fileExit_Click(object sender, EventArgs e)
+        {
+            Application.Exit();
+        }
+
+        private void menuGenerateClipboard_Click(object sender, EventArgs e)
+        {
+            this.Show();
+            generateFromClipboard();
+        }
+
+        private void menuNew_Click(object sender, EventArgs e)
+        {
+            this.Show();
+            reset();
+        }
+
+        private void exitToolStripMenuItem_Click(object sender, EventArgs e)
+        {
+            Application.Exit();
+        }
+
+        private void QRForm_FormClosing(object sender, FormClosingEventArgs e)
+        {
+            System.Diagnostics.Debug.WriteLine("Reason: " + e.CloseReason);
+            if (settings.background && (e.CloseReason == CloseReason.None || e.CloseReason == CloseReason.UserClosing))
+            {
+                this.Hide();
+                e.Cancel = true;
+            }
+        }
+
+        private void optionHotkey_Click(object sender, EventArgs e)
+        {
+            showHotkeyForm();
+        }
+
+        private void menuAutorun_Click(object sender, EventArgs e)
+        {
+            toggleAutorun();
+        }
+
+        private void menuHotkey_Click(object sender, EventArgs e)
+        {
+            showHotkeyForm();
+        }
+
+        private void optionWinStart_Click(object sender, EventArgs e)
+        {
+            toggleAutorun();
+        }
+
+        private void optionBackground_Click(object sender, EventArgs e)
+        {
+            toggleBackground();
+        }
+
+        private void imageCopy_Click(object sender, EventArgs e)
+        {
+            Clipboard.SetImage(QRImage.Image);
+        }
+
+        private void fileSaveAs_Click(object sender, EventArgs e)
+        {
+            saveImage();
+        }
+
+        private void imageSaveAs_Click(object sender, EventArgs e)
+        {
+            saveImage();
+        }
+
+        private void helpAbout_Click(object sender, EventArgs e)
+        {
+            AboutForm form = new AboutForm();
+            form.ShowDialog();
+            form.Dispose();
+        }
+    }
+}