Center additional windows depending on the visibility of the main window

This commit is contained in:
Alexander 2019-11-05 21:23:13 +02:00
parent d4c8a01f71
commit cf9953e9f5
4 changed files with 26 additions and 2 deletions

View file

@ -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*/

View file

@ -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();
}

View file

@ -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()

View file

@ -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;
}