using System; using System.Threading; using System.Windows; using Application = System.Windows.Application; namespace EscribePassword { public partial class App : Application { private static Mutex _mutex = null; 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; } 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; } } } }