From cf9953e9f515d7aaf9ba59f7da8bf59a399eaa1e Mon Sep 17 00:00:00 2001 From: Alexander Date: Tue, 5 Nov 2019 21:23:13 +0200 Subject: [PATCH] Center additional windows depending on the visibility of the main window --- Crypto Notepad/EnterKeyForm.cs | 9 +++++++++ Crypto Notepad/MainForm.cs | 10 ++++++++++ Crypto Notepad/MsgBoxCenterParent.cs | 8 ++++++-- Crypto Notepad/Program.cs | 1 + 4 files changed, 26 insertions(+), 2 deletions(-) diff --git a/Crypto Notepad/EnterKeyForm.cs b/Crypto Notepad/EnterKeyForm.cs index 2d825b4..a538294 100644 --- a/Crypto Notepad/EnterKeyForm.cs +++ b/Crypto Notepad/EnterKeyForm.cs @@ -33,6 +33,15 @@ private void EnterKeyForm_Load(object sender, EventArgs e) private void EnterKeyForm_Shown(object sender, EventArgs e) { fileNameLabel.Text = PublicVar.openFileName; + MainForm main = Owner as MainForm; + if (main.Visible) + { + CenterToParent(); + } + else + { + CenterToScreen(); + } } /*Form Events*/ diff --git a/Crypto Notepad/MainForm.cs b/Crypto Notepad/MainForm.cs index c5cabdf..31068db 100644 --- a/Crypto Notepad/MainForm.cs +++ b/Crypto Notepad/MainForm.cs @@ -106,6 +106,7 @@ private void DecryptAES() if (dialogResult == DialogResult.Retry) { DecryptAES(); + PublicVar.messageBoxCenterParent = true; } if (dialogResult == DialogResult.Cancel) { @@ -505,6 +506,7 @@ private void UnlockFile() if (ex is CryptographicException) { TypedPassword.Value = null; + PublicVar.messageBoxCenterParent = true; using (new CenterWinDialog(this)) { DialogResult dialogResult = MessageBox.Show(this, "Invalid key!", PublicVar.appName, MessageBoxButtons.RetryCancel, MessageBoxIcon.Error); @@ -758,6 +760,10 @@ private void MainWindow_FormClosing(object sender, FormClosingEventArgs e) { messageBoxText = "Save file: " + "\"" + PublicVar.openFileName + "\"" + " with a new key? "; } + if (Visible) + { + PublicVar.messageBoxCenterParent = true; + } using (new CenterWinDialog(this)) { DialogResult res = MessageBox.Show(this, messageBoxText, PublicVar.appName, MessageBoxButtons.YesNoCancel, MessageBoxIcon.Question); @@ -906,6 +912,10 @@ private void RichTextBox_DragDrop(object sender, DragEventArgs e) filePath = openFileDialog.FileName; StatusPanelFileInfo(); return; + if (Visible) + { + PublicVar.messageBoxCenterParent = true; + } } DecryptAES(); } diff --git a/Crypto Notepad/MsgBoxCenterParent.cs b/Crypto Notepad/MsgBoxCenterParent.cs index 9a763ff..7bf6fb1 100644 --- a/Crypto Notepad/MsgBoxCenterParent.cs +++ b/Crypto Notepad/MsgBoxCenterParent.cs @@ -3,6 +3,7 @@ using System.Drawing; using System.Windows.Forms; using System.Runtime.InteropServices; +using Crypto_Notepad; class CenterWinDialog : IDisposable { @@ -11,8 +12,11 @@ class CenterWinDialog : IDisposable public CenterWinDialog(Form owner) { - mOwner = owner; - owner.BeginInvoke(new MethodInvoker(FindDialog)); + if (PublicVar.messageBoxCenterParent) + { + mOwner = owner; + owner.BeginInvoke(new MethodInvoker(FindDialog)); + } } private void FindDialog() diff --git a/Crypto Notepad/Program.cs b/Crypto Notepad/Program.cs index 7fc38ce..fa586f9 100644 --- a/Crypto Notepad/Program.cs +++ b/Crypto Notepad/Program.cs @@ -65,6 +65,7 @@ static class PublicVar public static EncryptedString encryptionKey = new EncryptedString(); public static bool keyChanged = false; public static bool okPressed = false; + public static bool messageBoxCenterParent = false; public const string appName = "Crypto Notepad"; public static string openFileName; }