Ejemplo n.º 1
0
        private MainForm(Mode mode, IntPtr?destinationWindow = null)
        {
            InitializeComponent();

            if (mode == Mode.GlobalHooksOnly)
            {
                Debug.Assert(destinationWindow != null);
                _hooks = new GlobalHooks(destinationWindow.Value);
                _hooks.CallWndProc.Start();
                return;
            }

            Debug.Assert(destinationWindow == null);

            try
            {
                _serializedAreas = LoadAreasFromDisk();
                _areas           = SnapAreas.Deserialize(_serializedAreas);
            }
            catch (Exception ex) when(
                (ex is FileNotFoundException) ||
                (ex is DirectoryNotFoundException))
            {
                _serializedAreas = DefaultSnapAreas.Value;
                _areas           = SnapAreas.Deserialize(_serializedAreas);
            }
            catch (SerializationException)
            {
                MessageBox.Show("Your settings couldn't be parsed. Loading defaults.",
                                Application.ProductName, MessageBoxButtons.OK, MessageBoxIcon.Error);
                _serializedAreas = DefaultSnapAreas.Value;
                _areas           = SnapAreas.Deserialize(_serializedAreas);
            }

            _hooks = new GlobalHooks(this.Handle);
            _hooks.CallWndProc.CallWndProc += Hooked_WndProc;
            _hooks.CallWndProc.Start();

            _keyHook = new GlobalKeyboardHook();
            _keyHook.KeyboardPressed += _keyHook_KeyboardPressed;
            _overlay = new OverlayForm();

            trayIcon.Visible = true;
        }
Ejemplo n.º 2
0
        private void configurationMenuItem_Click(object sender, EventArgs e)
        {
            if (_configForm == null || _configForm.IsDisposed)
            {
                _configForm = new ConfigForm();
            }

            if (_configForm.Visible)
            {
                _configForm.BringToFront();
            }
            else
            {
                _configForm.Areas = _serializedAreas;
                if (_configForm.ShowDialog() != DialogResult.OK)
                {
                    return;
                }

                try
                {
                    _areas           = SnapAreas.Deserialize(_configForm.Areas);
                    _serializedAreas = _configForm.Areas;

                    string filename = GetConfigurationFilename();
                    Directory.CreateDirectory(Path.GetDirectoryName(filename));
                    File.WriteAllText(filename, _serializedAreas);
                }
                catch (SerializationException)
                {
                    MessageBox.Show("Not a valid format for configuration! Reverted.",
                                    Application.ProductName,
                                    MessageBoxButtons.OK,
                                    MessageBoxIcon.Error);
                }
            }
        }