Skip to content

Commit

Permalink
Fix CTD when centering window from a system tray or desktop shortcut
Browse files Browse the repository at this point in the history
Fixes #128
  • Loading branch information
terrymacdonald committed Jul 1, 2022
1 parent 47cc5eb commit 7a74456
Show file tree
Hide file tree
Showing 5 changed files with 50 additions and 17 deletions.
4 changes: 2 additions & 2 deletions DisplayMagician/Properties/AssemblyInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,8 @@
[assembly: Guid("e4ceaf5e-ad01-4695-b179-31168eb74c48")]

// Version information
[assembly: AssemblyVersion("2.4.0.88")]
[assembly: AssemblyFileVersion("2.4.0.88")]
[assembly: AssemblyVersion("2.4.0.89")]
[assembly: AssemblyFileVersion("2.4.0.89")]
[assembly: NeutralResourcesLanguageAttribute( "en" )]
[assembly: CLSCompliant(true)]

16 changes: 8 additions & 8 deletions DisplayMagician/UIForms/DisplayProfileForm.cs
Original file line number Diff line number Diff line change
Expand Up @@ -92,18 +92,18 @@ private void Apply_Click(object sender, EventArgs e)

private void RecenterWindow()
{
// Center the MainAppForm
Utils.CenterOnPrimaryScreen(Program.AppMainForm);
//Program.AppMainForm.Activate();

// Bring the window back to the front
Utils.ActivateCenteredOnPrimaryScreen(this);

// Also refresh the right-click menu (if we have a main form loaded)
if (Program.AppMainForm is Form)
{
// Center the MainAppForm
Utils.CenterOnPrimaryScreen(Program.AppMainForm);
// Also refresh the right-click menu (if we have a main form loaded)
Program.AppMainForm.RefreshNotifyIconMenus();

}

// Bring the window back to the front
Utils.ActivateCenteredOnPrimaryScreen(this);

}


Expand Down
5 changes: 5 additions & 0 deletions DisplayMagician/UIForms/MainForm.cs
Original file line number Diff line number Diff line change
Expand Up @@ -225,8 +225,13 @@ public MainForm(Form formToOpen = null)
// Make this window top most if we're not minimised
if (!Program.AppProgramSettings.MinimiseOnStart)
{
if (Program.AppMainForm is Form)
// Center the MainAppForm
Utils.CenterOnPrimaryScreen(Program.AppMainForm);
{
// Center the MainAppForm
Utils.CenterOnPrimaryScreen(Program.AppMainForm);
}
// Bring the window back to the front
Utils.ActivateCenteredOnPrimaryScreen(this);

Expand Down
13 changes: 7 additions & 6 deletions DisplayMagician/UIForms/ShortcutLibraryForm.cs
Original file line number Diff line number Diff line change
Expand Up @@ -514,16 +514,17 @@ private void btn_run_Click(object sender, EventArgs e)
btn_cancel.Visible = false;
btn_cancel.Enabled = false;

// Center the MainAppForm
Utils.CenterOnPrimaryScreen(Program.AppMainForm);
// Bring the window back to the front
Utils.ActivateCenteredOnPrimaryScreen(this);

// Also refresh the right-click menu (if we have a main form loaded)
if (Program.AppMainForm is Form)
{
// Center the MainAppForm
Utils.CenterOnPrimaryScreen(Program.AppMainForm);
// Also refresh the right-click menu (if we have a main form loaded)
Program.AppMainForm.RefreshNotifyIconMenus();

}
// Bring the window back to the front
Utils.ActivateCenteredOnPrimaryScreen(this);


}

Expand Down
29 changes: 28 additions & 1 deletion DisplayMagician/Utils.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using Microsoft.Win32;
using NLog;
using System;
using System.Drawing;
using System.Runtime.InteropServices;
Expand All @@ -12,7 +13,8 @@ static class Utils

/// 2. Declare DownloadsFolder KNOWNFOLDERID
private static Guid FolderDownloads = new Guid("374DE290-123F-4565-9164-39C4925E467B");

private static readonly NLog.Logger logger = NLog.LogManager.GetCurrentClassLogger();

/// 3. Import SHGetKnownFolderPath method
/// <summary>
/// Retrieves the full path of a known folder identified by the folder's KnownFolderID.
Expand Down Expand Up @@ -57,6 +59,11 @@ public static string GetDownloadsPath()

public static void ActivateCenteredOnPrimaryScreen(this Form frm)
{
if (!(frm is Form))
{
logger.Trace($"Utils/ActivateCenteredOnPrimaryScreen: frm passed in is not a Form. Not able to center the form.");
return;
}
CenterOnPrimaryScreen(frm);
frm.Visible = true;
frm.Activate();
Expand All @@ -65,12 +72,22 @@ public static void ActivateCenteredOnPrimaryScreen(this Form frm)

public static void ShowCenteredOnPrimaryScreen(this Form frm)
{
if (!(frm is Form))
{
logger.Trace($"Utils/ShowCenteredOnPrimaryScreen: frm passed in is not a Form. Not able to center the form.");
return;
}
CenterOnPrimaryScreen(frm);
frm.Show();
}

public static void CenterOnPrimaryScreen(this Form frm)
{
if (!(frm is Form))
{
logger.Trace($"Utils/CenterOnPrimaryScreen: frm passed in is not a Form. Not able to center the form.");
return;
}
frm.Top = (Screen.PrimaryScreen.Bounds.Height - frm.Height) / 2;
frm.Left = (Screen.PrimaryScreen.Bounds.Width - frm.Width) / 2;
}
Expand All @@ -79,6 +96,16 @@ public static void CenterOnPrimaryScreen(this Form frm)

public static void ShowDialogCentered(this Form frm, Form owner)
{
if (!(frm is Form))
{
logger.Trace($"Utils/ShowDialogCentered: frm passed in is not a Form. Not able to center the dialog.");
return;
}
if (!(owner is Form))
{
logger.Trace($"Utils/ShowDialogCentered: owner passed in is not a Form. Not able to center the dialog.");
return;
}
Rectangle ownerRect = GetOwnerRect(frm, owner);
frm.Location = new Point(ownerRect.Left + (ownerRect.Width - frm.Width) / 2,
ownerRect.Top + (ownerRect.Height - frm.Height) / 2);
Expand Down

0 comments on commit 7a74456

Please sign in to comment.