Centered the shortcutform loading window

This was WAY harder than it needed to be, as the loading window runs in a separate thread. It does a bit of trickery to get it to work reliably, but now it works ok. I really should be factoring the work into a background thread rather than running it in the UI thread, but that's something I'll revisit when I rebuild it under .net 6.0 and MAUI.
This commit is contained in:
Terry MacDonald 2021-11-23 21:04:28 +13:00
parent 971a45b2e8
commit 6a9f3abc6d
7 changed files with 83 additions and 26 deletions

View File

@ -43,6 +43,7 @@ namespace DisplayMagician {
public static ProgramSettings AppProgramSettings;
public static MainForm AppMainForm;
public static LoadingForm AppSplashScreen;
public static ShortcutLoadingForm AppShortcutLoadingSplashScreen;
private static readonly NLog.Logger logger = NLog.LogManager.GetCurrentClassLogger();
private static SharedLogger sharedLogger;
private static bool _gamesLoaded = false;

View File

@ -26,8 +26,8 @@ using System.Resources;
[assembly: Guid("e4ceaf5e-ad01-4695-b179-31168eb74c48")]
// Version information
[assembly: AssemblyVersion("2.1.0.317")]
[assembly: AssemblyFileVersion("2.1.0.317")]
[assembly: AssemblyVersion("2.1.0.358")]
[assembly: AssemblyFileVersion("2.1.0.358")]
[assembly: NeutralResourcesLanguageAttribute( "en" )]
[assembly: CLSCompliant(true)]

View File

@ -69,16 +69,6 @@ namespace DisplayMagician.UIForms
public ShortcutForm()
{
InitializeComponent();
_loadingScreen = new ShortcutLoadingForm();
_loadingScreen.Title = "Preparing images...";
_loadingScreen.Description = "Preparing images before showing you the Shortcut information. You will be able to swap your shortcut icon to any image you want, or choose one from a list.";
_loadingScreen.StartPosition = FormStartPosition.CenterParent;
_loadingScreen.Parent = this.ParentForm;
var splashThread = new Thread(new ThreadStart(
() => Application.Run(_loadingScreen)));
splashThread.SetApartmentState(ApartmentState.STA);
splashThread.Start();
// Set the profileAdaptor we need to load images from Profiles
// into the Profiles ImageListView
try
@ -1702,6 +1692,7 @@ namespace DisplayMagician.UIForms
private void ShortcutForm_Load(object sender, EventArgs e)
{
if (_firstShow)
{
// Parse the game bitmaps now the first time as we need them
@ -1711,24 +1702,20 @@ namespace DisplayMagician.UIForms
GameLibraries.GameLibrary.RefreshGameBitmaps();
}
// Close the splash screen
CloseTheSplashScreen();
_firstShow = false;
}
// Load the shortcut info
LoadShortcut();
CloseTheSplashScreen();
}
private void CloseTheSplashScreen()
{
// Close the splash screen
if (_loadingScreen != null && !_loadingScreen.Disposing && !_loadingScreen.IsDisposed)
_loadingScreen.Invoke(new Action(() => _loadingScreen.Close()));
this.TopMost = true;
this.Focus();
this.TopMost = false;
if (Program.AppShortcutLoadingSplashScreen != null && !Program.AppShortcutLoadingSplashScreen.Disposing && !Program.AppShortcutLoadingSplashScreen.IsDisposed)
Program.AppShortcutLoadingSplashScreen.Invoke(new Action(() => Program.AppShortcutLoadingSplashScreen.Close()));
this.Activate();
}
private void rb_standalone_CheckedChanged(object sender, EventArgs e)

View File

@ -10,6 +10,7 @@ using System.Drawing;
using System.Globalization;
using System.IO;
using System.Linq;
using System.Threading;
using System.Windows.Forms;
namespace DisplayMagician.UIForms
@ -266,10 +267,12 @@ namespace DisplayMagician.UIForms
{
this.Cursor = Cursors.WaitCursor;
ShortcutItem si = new ShortcutItem();
ShowShortcutLoadingWindow();
if (_shortcutForm == null)
{
_shortcutForm = new ShortcutForm();
}
_shortcutForm.Owner = this;
//ShortcutRepository.IsValidRefresh()
// Set the Shortcut to as a new shortcut
_shortcutForm.Shortcut = si;
@ -292,6 +295,22 @@ namespace DisplayMagician.UIForms
}
}
private void ShowShortcutLoadingWindow()
{
Program.AppShortcutLoadingSplashScreen = new ShortcutLoadingForm();
Program.AppShortcutLoadingSplashScreen.Title = "Preparing images...";
Program.AppShortcutLoadingSplashScreen.Description = "Preparing images before showing you the Shortcut information. You will be able to swap your shortcut icon to any image you want, or choose one from a list.";
int resultX = this.DesktopLocation.X + ((this.Width - Program.AppShortcutLoadingSplashScreen.Width) / 2);
int resultY = this.DesktopLocation.Y + ((this.Height - Program.AppShortcutLoadingSplashScreen.Height) / 2);
Program.AppShortcutLoadingSplashScreen.WantedLocation = new Point(resultX, resultY);
//_loadingScreen.CenterParent(ownerRect);
//_loadingScreen.StartPosition = FormStartPosition.Manual;
var splashThread = new Thread(new ThreadStart(
() => Application.Run(Program.AppShortcutLoadingSplashScreen)));
splashThread.SetApartmentState(ApartmentState.STA);
splashThread.Start();
}
private void btn_edit_Click(object sender, EventArgs e)
{
if (_selectedShortcut == null)
@ -320,11 +339,12 @@ namespace DisplayMagician.UIForms
_selectedShortcut = GetShortcutFromUUID(shortcutUUID);
this.Cursor = Cursors.WaitCursor;
ShowShortcutLoadingWindow();
if (_shortcutForm == null)
{
_shortcutForm = new ShortcutForm();
}
_shortcutForm.Owner = this;
_shortcutForm.Shortcut = _selectedShortcut;
_shortcutForm.EditingExistingShortcut = true;
//ilv_saved_shortcuts.SuspendLayout();

View File

@ -83,6 +83,7 @@ namespace DisplayMagician.UIForms
this.Text = "DisplayMagician is loading....";
this.UseWaitCursor = true;
this.Load += new System.EventHandler(this.LoadingForm_Load);
this.Shown += new System.EventHandler(this.ShortcutLoadingForm_Shown);
this.ResumeLayout(false);
}

View File

@ -14,11 +14,17 @@ namespace DisplayMagician.UIForms
{
private string _title = "DisplayMagician is loading...";
private string _description = "If you have installed a lot of games over time or have a lot of games installed now, this may take a while!";
private Point _wantedLocation = new Point(0,0);
public ShortcutLoadingForm()
{
InitializeComponent();
this.TopMost = false;
this.StartPosition = FormStartPosition.Manual;
/*_owner = owner;
int resultX = _owner.DisplayRectangle.X + _owner.Width - this.Width;
int resultY = _owner.DisplayRectangle.Y + _owner.Height - this.Height;
_owner.Location = new Point(resultX, resultY);*/
this.TopMost = true;
}
@ -49,10 +55,22 @@ namespace DisplayMagician.UIForms
}
}
public Point WantedLocation
{
set
{
_wantedLocation = value;
}
}
private void LoadingForm_Load(object sender, EventArgs e)
{
DesktopLocation = _wantedLocation;
}
private void ShortcutLoadingForm_Shown(object sender, EventArgs e)
{
DesktopLocation = _wantedLocation;
}
}
}

View File

@ -1,9 +1,11 @@
using System;
using System.Drawing;
using System.Runtime.InteropServices;
using System.Windows.Forms;
namespace DisplayMagician
{
class Utils
static class Utils
{
// 1. Import InteropServices
@ -52,6 +54,34 @@ namespace DisplayMagician
}
}
public static void ShowCentered(this Form frm, Form owner)
{
Rectangle ownerRect = GetOwnerRect(frm, owner);
frm.Location = new Point(ownerRect.Left + (ownerRect.Width - frm.Width) / 2,
ownerRect.Top + (ownerRect.Height - frm.Height) / 2);
frm.Show(owner);
}
public static void CenterParent(this Form frm, Rectangle ownerRect)
{
frm.Location = new Point(ownerRect.Left + (ownerRect.Width - frm.Width) / 2,
ownerRect.Top + (ownerRect.Height - frm.Height) / 2);
}
public static void ShowDialogCentered(this Form frm, Form owner)
{
Rectangle ownerRect = GetOwnerRect(frm, owner);
frm.Location = new Point(ownerRect.Left + (ownerRect.Width - frm.Width) / 2,
ownerRect.Top + (ownerRect.Height - frm.Height) / 2);
frm.ShowDialog(owner);
}
private static Rectangle GetOwnerRect(Form frm, Form owner)
{
return owner != null ? owner.DesktopBounds : Screen.GetWorkingArea(frm);
}
}