From 6a619872f418beca6164c2a75e98b407729f7e1c Mon Sep 17 00:00:00 2001 From: Miguel Date: Sat, 15 Jun 2024 19:33:52 +0200 Subject: [PATCH] Mejorado --- App.xaml.cs | 44 ++++++++++++++++++++++ EscribePassword.csproj | 2 + KeyboardHelper.cs | 85 ++++++++++++++++++++++++++++++++++++++++++ MainWindow.xaml | 26 +++++++------ MainWindow.xaml.cs | 73 ++++++++++++++++++++++++++++-------- 5 files changed, 202 insertions(+), 28 deletions(-) create mode 100644 KeyboardHelper.cs diff --git a/App.xaml.cs b/App.xaml.cs index 16c17c8..1d4c64b 100644 --- a/App.xaml.cs +++ b/App.xaml.cs @@ -1,6 +1,7 @@ using System.Configuration; using System.Data; using System.Windows; +using Application = System.Windows.Application; namespace EscribePassword { @@ -9,6 +10,49 @@ namespace EscribePassword /// public partial class App : Application { + public KeyboardHelper PasteLogic { get; } = new KeyboardHelper(); + + protected override void OnStartup(StartupEventArgs e) + { + base.OnStartup(e); + PasteLogic.SaveCurrentWindow(); + SetInitialWindowPosition(); + } + + public void SetInitialWindowPosition() + { + var screenRect = PasteLogic.GetScreenOfPreviousWindow(); + var mainWindow = Application.Current.MainWindow; + + if (mainWindow != null) + { + + // Obtener la posición del cursor + var cursorPosition = System.Windows.Forms.Cursor.Position; + + // Determinar en qué pantalla está el cursor + var screen = Screen.FromPoint(cursorPosition); + + // Determinar la posición inicial + double newLeft = screenRect.Right; + double newTop = screenRect.Top; + + // Verificar si la ventana se posiciona fuera de la pantalla y ajustarla si es necesario + if (newLeft < screenRect.Left) + { + newLeft = screenRect.Left; + } + + if (newLeft + mainWindow.Width > screen.WorkingArea.Right) + { + newLeft = screen.WorkingArea.Right - mainWindow.Width; + } + + // Aplicar la nueva posición + mainWindow.Left = newLeft; + mainWindow.Top = newTop; + } + } } } diff --git a/EscribePassword.csproj b/EscribePassword.csproj index 0d0a51b..8460c40 100644 --- a/EscribePassword.csproj +++ b/EscribePassword.csproj @@ -6,11 +6,13 @@ enable enable true + True + diff --git a/KeyboardHelper.cs b/KeyboardHelper.cs new file mode 100644 index 0000000..59581aa --- /dev/null +++ b/KeyboardHelper.cs @@ -0,0 +1,85 @@ +using System.Runtime.InteropServices; +using WindowsInput.Native; +using WindowsInput; +using System.Windows; +using EscribePassword; +using CommunityToolkit.Mvvm.ComponentModel; + +namespace EscribePassword +{ + + public partial class Passwords : ObservableObject + { + [ObservableProperty] + private string usuario; + + [ObservableProperty] + private string password; + } + + public class KeyboardHelper + { + [DllImport("user32.dll", SetLastError = true)] + private static extern IntPtr GetForegroundWindow(); + + [DllImport("user32.dll", SetLastError = true)] + private static extern bool SetForegroundWindow(IntPtr hWnd); + + [DllImport("user32.dll")] + private static extern bool GetWindowRect(IntPtr hWnd, ref RECT lpRect); + + [StructLayout(LayoutKind.Sequential)] + private struct RECT + { + public int Left; + public int Top; + public int Right; + public int Bottom; + } + + private IntPtr previousWindow; + + public void SaveCurrentWindow() + { + previousWindow = GetForegroundWindow(); + } + + public Rect GetScreenOfPreviousWindow() + { + if (previousWindow != IntPtr.Zero) + { + var rect = new RECT(); + if (GetWindowRect(previousWindow, ref rect)) + { + return new Rect(rect.Left, rect.Top, rect.Right - rect.Left, rect.Bottom - rect.Top); + } + } + return SystemParameters.WorkArea; + } + + public void RestoreAndSimulatePaste(Passwords password) + { + if (previousWindow != IntPtr.Zero) + { + // Restore focus to the previous window + SetForegroundWindow(previousWindow); + + var sim = new InputSimulator(); + + if (password.Usuario != null && password.Usuario.Length > 0) + { + // Simulate typing the username + sim.Keyboard.TextEntry(password.Usuario); + sim.Keyboard.KeyPress(VirtualKeyCode.RETURN); + sim.Keyboard.KeyPress(VirtualKeyCode.TAB); + } + // Simulate typing the password + sim.Keyboard.TextEntry(password.Password); + sim.Keyboard.KeyPress(VirtualKeyCode.RETURN); + sim.Keyboard.KeyPress(VirtualKeyCode.TAB); + } + } + } +} + + diff --git a/MainWindow.xaml b/MainWindow.xaml index 3f30836..88c7933 100644 --- a/MainWindow.xaml +++ b/MainWindow.xaml @@ -2,38 +2,40 @@ xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" - xmlns:local="clr-namespace:EscribePassword" mc:Ignorable="d" Title="MainWindow" Height="450" Width="800"> + xmlns:local="clr-namespace:EscribePassword" mc:Ignorable="d" Title="MainWindow" Height="450" Width="300" + AllowsTransparency="True" WindowStyle="None" MouseDown="Window_MouseDown"> - - - - - + -