KeyWatch/KeyWatch++/Program.cs

69 lines
2.5 KiB
C#

using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace KeyWatch__
{
static class Program
{
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main() {
var progs = System.Diagnostics.Process.GetProcessesByName("KeyWatch++").ToList();
if (progs.Count > 1) { return; }
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new KeyWatchContext());
}
}
public class KeyWatchContext : ApplicationContext
{
NotifyIcon notifyIcon = new NotifyIcon();
Form1 configWindow = new Form1();
public KeyWatchContext() {
MenuItem configMenuItem = new MenuItem("Configuration", new EventHandler(ShowConfig));
MenuItem exitMenuItem = new MenuItem("Exit", new EventHandler(Exit));
notifyIcon.Icon = KeyWatch__.Properties.Resources.AppIcon;
notifyIcon.ContextMenu = new ContextMenu(new MenuItem[] { configMenuItem, exitMenuItem });
notifyIcon.Visible = true;
notifyIcon.DoubleClick += (o, e) => { ShowConfig(o, e); };
LoadSettings();
}
void LoadSettings() {
configWindow.check_Numlock.Checked = Properties.KeyWatch.Default.Numlock;
configWindow.check_Capslock.Checked = Properties.KeyWatch.Default.Capslock;
configWindow.check_Scrolllock.Checked = Properties.KeyWatch.Default.Scrolllock;
configWindow.comboBox_numlock.SelectedItem = Properties.KeyWatch.Default.NumlockText;
configWindow.comboBox_capslock.SelectedItem = Properties.KeyWatch.Default.CapslockText;
configWindow.comboBox_scrolllock.SelectedItem = Properties.KeyWatch.Default.ScrolllockText;
}
void ShowConfig(object sender, EventArgs e) {
// If we are already showing the window meerly focus it.
if (configWindow.Visible)
configWindow.Focus();
else
configWindow.ShowDialog();
}
void Exit(object sender, EventArgs e) {
// We must manually tidy up and remove the icon before we exit.
// Otherwise it will be left behind until the user mouses over.
notifyIcon.Visible = false;
Application.Exit();
}
}
}