DisplayMagician/HeliosPlus/UIForms/MainForm.cs
Terry MacDonald 23930a2a15 Added initial ability to minimise to notification area
Used some awesome help from Hans Passant to build
logic to allow minimise to notification area as well as
be able to change profiles etc from the notification area.

https://stackoverflow.com/questions/1730731/how-to-start-winform-app-minimized-to-tray
2020-10-26 14:30:00 +13:00

154 lines
5.1 KiB
C#

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using HeliosPlus.GameLibraries;
using System.Threading;
using System.Reflection;
using HeliosPlus.Shared;
namespace HeliosPlus.UIForms
{
public partial class MainForm : Form
{
private bool allowVisible; // ContextMenu's Show command used
private bool allowClose; // ContextMenu's Exit command used
public MainForm()
{
InitializeComponent();
btn_setup_display_profiles.Parent = splitContainer1.Panel1;
btn_setup_game_shortcuts.Parent = splitContainer1.Panel2;
lbl_version.Text = string.Format(lbl_version.Text, Assembly.GetExecutingAssembly().GetName().Version);
notifyIcon.Visible = true;
// Make the form show
allowVisible = true;
// Close the application when the form is closed
allowClose = true;
RefreshNotifyIconMenus();
}
protected override void SetVisibleCore(bool value)
{
if (!allowVisible)
{
value = false;
if (!this.IsHandleCreated) CreateHandle();
}
base.SetVisibleCore(value);
}
protected override void OnFormClosing(FormClosingEventArgs e)
{
if (!allowClose)
{
this.Hide();
e.Cancel = true;
}
base.OnFormClosing(e);
}
private void btn_exit_Click(object sender, EventArgs e)
{
Application.Exit();
}
private void pb_display_profile_Click(object sender, EventArgs e)
{
var displayProfileForm = new DisplayProfileForm();
displayProfileForm.ShowDialog(this);
}
private void btn_setup_display_profiles_Click(object sender, EventArgs e)
{
var displayProfileForm = new DisplayProfileForm();
displayProfileForm.ShowDialog(this);
}
private void pb_game_shortcut_Click(object sender, EventArgs e)
{
var shortcutForm = new ShortcutForm();
shortcutForm.ShowDialog(this);
}
private void btn_setup_game_shortcuts_Click(object sender, EventArgs e)
{
var shortcutLibraryForm = new ShortcutLibraryForm();
shortcutLibraryForm.ShowDialog(this);
}
private void MainForm_Load(object sender, EventArgs e)
{
// Start loading the Steam Games just after the Main form opens
//SteamGame.GetAllInstalledGames();
}
private void RefreshNotifyIconMenus()
{
// Clear all the profiles
profileToolStripMenuItem.DropDownItems.Clear();
// Prepare the heading shortcuts
ToolStripMenuItem heading = new ToolStripMenuItem();
heading.Text = "Display Profiles";
Font headingFont = new Font(heading.Font, FontStyle.Italic);
heading.Font = headingFont;
heading.Enabled = false;
profileToolStripMenuItem.DropDownItems.Add(heading);
ToolStripSeparator separator = new ToolStripSeparator();
profileToolStripMenuItem.DropDownItems.Add(separator);
// Add the current slist of profiles into the NotifyIcon context menu
foreach (ProfileItem profile in ProfileRepository.AllProfiles)
{
profileToolStripMenuItem.DropDownItems.Add(profile.Name,profile.ProfileBitmap, runProfileToolStripMenuItem_Click);
}
// Clear all the shortcuts
shortcutToolStripMenuItem.DropDownItems.Clear();
// Prepare the heading shortcuts
heading = new ToolStripMenuItem();
heading.Text = "Game Shortcuts";
heading.Font = headingFont;
heading.Enabled = false;
shortcutToolStripMenuItem.DropDownItems.Add(heading);
separator = new ToolStripSeparator();
shortcutToolStripMenuItem.DropDownItems.Add(separator);
// Add the current list of profiles into the NotifyIcon context menu
foreach (ShortcutItem shortcut in ShortcutRepository.AllShortcuts)
{
shortcutToolStripMenuItem.DropDownItems.Add(shortcut.Name,shortcut.ShortcutBitmap, runShortcutToolStripMenuItem_Click);
}
}
private void runProfileToolStripMenuItem_Click(object sender, EventArgs e)
{
this.Show();
}
private void runShortcutToolStripMenuItem_Click(object sender, EventArgs e)
{
this.Show();
}
private void openApplicationWindowToolStripMenuItem_Click(object sender, EventArgs e)
{
allowVisible = true;
Show();
BringToFront();
}
private void exitToolStripMenuItem_Click(object sender, EventArgs e)
{
allowClose = true;
Application.Exit();
}
}
}