DisplayMagician/HeliosPlus/UIForms/MainForm.cs
Terry MacDonald 73284c03f8 Fixed open application window menu
The Open Application Window notification icon
popup menu item wouldn't restore the window
if it was minimised before running the Open
Application Window command. Added some cool
code from https://stackoverflow.com/questions/
354445/restore-windowstate-from-minimized and
then tweaked it to work in this situation. I could have
added this a much better place in the application but
I am planning on rewriting the app in WPF
eventually so I don't really need to do so.
2020-11-15 21:28:15 +13:00

238 lines
8.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;
using System.Runtime.InteropServices;
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;
RefreshNotifyIconMenus();
if (Program.AppProgramSettings.MinimiseOnStart)
{
// Make the form minimised on start
allowVisible = false;
// Hide the application to notification area when the form is closed
allowClose = false;
cb_minimise_notification_area.Checked = true;
}
else
{
// Make the form show to the user on startup
allowVisible = true;
// Really close the application when the form is closed
allowClose = true;
cb_minimise_notification_area.Checked = false;
}
}
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)
{
var menuItem = sender as ToolStripMenuItem;
ProfileItem profileToRun = null;
if (menuItem != null)
{
foreach (ProfileItem profile in ProfileRepository.AllProfiles)
{
if (profile.Name.Equals(menuItem.Text))
{
profileToRun = profile;
break;
}
}
// Run the shortcut if it's still there
if (profileToRun != null)
Program.ApplyProfile(profileToRun);
}
}
private void runShortcutToolStripMenuItem_Click(object sender, EventArgs e)
{
var menuItem = sender as ToolStripMenuItem;
ShortcutItem shortcutToRun = null;
if (menuItem != null)
{
foreach (ShortcutItem shortcut in ShortcutRepository.AllShortcuts)
{
if (shortcut.Name.Equals(menuItem.Text))
{
shortcutToRun = shortcut;
break;
}
}
// Run the shortcut if it's still there
if (shortcutToRun != null)
ShortcutRepository.RunShortcut(shortcutToRun);
}
}
private void openApplicationWindowToolStripMenuItem_Click(object sender, EventArgs e)
{
allowVisible = true;
Restore();
Show();
BringToFront();
}
private void exitToolStripMenuItem_Click(object sender, EventArgs e)
{
allowClose = true;
Application.Exit();
}
private void cb_minimise_notification_area_CheckedChanged(object sender, EventArgs e)
{
if (cb_minimise_notification_area.Checked)
{
// Make the form minimised on start
allowVisible = false;
// Hide the application to notification area when the form is closed
allowClose = false;
// Enable the MinimiseOnStart setting
Program.AppProgramSettings.MinimiseOnStart = true;
}
else
{
// Make the form show to the user on startup
allowVisible = true;
// Really close the application when the form is closed
allowClose = true;
// Disable the MinimiseOnStart setting
Program.AppProgramSettings.MinimiseOnStart = false;
}
}
[DllImport("user32.dll")]
private static extern int ShowWindow(IntPtr hWnd, uint Msg);
private const uint SW_RESTORE = 0x09;
public void Restore()
{
if (WindowState == FormWindowState.Minimized)
{
ShowWindow(Handle, SW_RESTORE);
}
}
}
}