EscribePassword/App.xaml.cs

62 lines
1.7 KiB
C#
Raw Permalink Normal View History

using System;
using System.Threading;
2024-06-15 06:24:07 -03:00
using System.Windows;
2024-06-15 14:33:52 -03:00
using Application = System.Windows.Application;
2024-06-15 06:24:07 -03:00
namespace EscribePassword
{
public partial class App : Application
{
private static Mutex _mutex = null;
2024-06-15 14:33:52 -03:00
public KeyboardHelper PasteLogic { get; } = new KeyboardHelper();
protected override void OnStartup(StartupEventArgs e)
{
const string appName = "EscribePasswordApp";
bool createdNew;
_mutex = new Mutex(true, appName, out createdNew);
if (!createdNew)
{
// Hay otra instancia en ejecución, cerramos esta instancia
Application.Current.Shutdown();
return;
}
2024-06-15 14:33:52 -03:00
base.OnStartup(e);
PasteLogic.SaveCurrentWindow();
SetInitialWindowPosition();
}
public void SetInitialWindowPosition()
{
var screenRect = PasteLogic.GetScreenOfPreviousWindow();
var mainWindow = Application.Current.MainWindow;
if (mainWindow != null)
{
var cursorPosition = System.Windows.Forms.Cursor.Position;
var screen = Screen.FromPoint(cursorPosition);
double newLeft = screenRect.Right;
double newTop = screenRect.Top;
if (newLeft < screenRect.Left)
{
newLeft = screenRect.Left;
}
if (newLeft + mainWindow.Width > screen.WorkingArea.Right)
{
newLeft = screen.WorkingArea.Right - mainWindow.Width;
}
mainWindow.Left = newLeft;
mainWindow.Top = newTop;
}
}
2024-06-15 06:24:07 -03:00
}
}