Mejorado
This commit is contained in:
parent
f61d8b7883
commit
6a619872f4
44
App.xaml.cs
44
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
|
|||
/// </summary>
|
||||
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;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -6,11 +6,13 @@
|
|||
<Nullable>enable</Nullable>
|
||||
<ImplicitUsings>enable</ImplicitUsings>
|
||||
<UseWPF>true</UseWPF>
|
||||
<UseWindowsForms>True</UseWindowsForms>
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="CommunityToolkit.Mvvm" Version="8.2.2" />
|
||||
<PackageReference Include="Extended.Wpf.Toolkit" Version="4.6.0" />
|
||||
<PackageReference Include="InputSimulator" Version="1.0.4" />
|
||||
<PackageReference Include="Newtonsoft.Json" Version="13.0.3" />
|
||||
</ItemGroup>
|
||||
|
||||
|
|
|
@ -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);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -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">
|
||||
<Window.DataContext>
|
||||
<local:MView />
|
||||
</Window.DataContext>
|
||||
<Grid>
|
||||
<Grid.ColumnDefinitions>
|
||||
<ColumnDefinition Width="*" />
|
||||
<ColumnDefinition Width="2*" />
|
||||
</Grid.ColumnDefinitions>
|
||||
<Grid.RowDefinitions>
|
||||
<RowDefinition Height="*" />
|
||||
<RowDefinition Height="Auto" />
|
||||
</Grid.RowDefinitions>
|
||||
<DataGrid Grid.Column="0" Grid.Row="0" ItemsSource="{Binding Passwords}"
|
||||
SelectedItem="{Binding SelectedPassword}" AutoGenerateColumns="False" CanUserAddRows="False"
|
||||
CanUserDeleteRows="False">
|
||||
<DataGrid Grid.Row="0" ItemsSource="{Binding Passwords}" SelectedItem="{Binding SelectedPassword}"
|
||||
AutoGenerateColumns="False" CanUserAddRows="False" CanUserDeleteRows="False">
|
||||
<DataGrid.Columns>
|
||||
<DataGridTextColumn Header="Usuario" Binding="{Binding Usuario}" Width="*" />
|
||||
<DataGridTextColumn Header="Contraseña" Binding="{Binding Password}" Width="*" />
|
||||
<DataGridTemplateColumn Header="Acciones" Width="Auto">
|
||||
<DataGridTemplateColumn.CellTemplate>
|
||||
<DataTemplate>
|
||||
<Button Content="Eliminar"
|
||||
Command="{Binding DataContext.EliminarCommand, RelativeSource={RelativeSource AncestorType=DataGrid}}"
|
||||
CommandParameter="{Binding}" />
|
||||
<StackPanel Orientation="Horizontal">
|
||||
<Button Content="Eliminar"
|
||||
Command="{Binding DataContext.EliminarCommand, RelativeSource={RelativeSource AncestorType=DataGrid}}"
|
||||
CommandParameter="{Binding}" />
|
||||
<Button Content="Utilizar"
|
||||
Command="{Binding DataContext.UtilizarCommand, RelativeSource={RelativeSource AncestorType=DataGrid}}"
|
||||
CommandParameter="{Binding}" Margin="5,0,0,0" />
|
||||
</StackPanel>
|
||||
</DataTemplate>
|
||||
</DataGridTemplateColumn.CellTemplate>
|
||||
</DataGridTemplateColumn>
|
||||
</DataGrid.Columns>
|
||||
</DataGrid>
|
||||
<StackPanel Grid.Row="1" Grid.ColumnSpan="2" Orientation="Horizontal" HorizontalAlignment="Center">
|
||||
<StackPanel Grid.Row="1" Orientation="Horizontal" HorizontalAlignment="Center" Margin="10,10,10,10">
|
||||
<Button Content="Agregar" Command="{Binding AgregarCommand}" Margin="5" />
|
||||
<Button Content="Cerrar" Command="{Binding CerrarCommand}" Margin="5" />
|
||||
</StackPanel>
|
||||
</Grid>
|
||||
</Window>
|
||||
|
|
|
@ -5,9 +5,43 @@ using System.Collections.ObjectModel;
|
|||
using System.Text.Json;
|
||||
using System.IO;
|
||||
using System.Windows;
|
||||
using WindowsInput;
|
||||
using WindowsInput.Native;
|
||||
using Application = System.Windows.Application;
|
||||
using System.Windows.Input;
|
||||
using MouseButton = System.Windows.Input.MouseButton;
|
||||
using System.Windows;
|
||||
using System.Windows.Controls;
|
||||
using System.Windows.Input;
|
||||
using ListBox = System.Windows.Controls.ListBox;
|
||||
|
||||
namespace EscribePassword
|
||||
{
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// Interaction logic for MainWindow.xaml
|
||||
/// </summary>
|
||||
public partial class MainWindow : Window
|
||||
{
|
||||
public MainWindow()
|
||||
{
|
||||
InitializeComponent();
|
||||
this.Loaded += MainWindow_Loaded;
|
||||
}
|
||||
private void MainWindow_Loaded(object sender, RoutedEventArgs e)
|
||||
{
|
||||
((App)Application.Current).SetInitialWindowPosition();
|
||||
}
|
||||
|
||||
private void Window_MouseDown(object sender, MouseButtonEventArgs e)
|
||||
{
|
||||
if (e.ChangedButton == MouseButton.Left)
|
||||
{
|
||||
this.DragMove();
|
||||
}
|
||||
}
|
||||
}
|
||||
public partial class MView : ObservableObject
|
||||
{
|
||||
[ObservableProperty]
|
||||
|
@ -16,18 +50,12 @@ namespace EscribePassword
|
|||
[ObservableProperty]
|
||||
private Passwords selectedPassword;
|
||||
|
||||
public RelayCommand AgregarCommand { get; }
|
||||
public RelayCommand<Passwords> EliminarCommand { get; }
|
||||
|
||||
public MView()
|
||||
{
|
||||
// Suscribirse al evento de cierre de la aplicación
|
||||
Application.Current.Exit += OnApplicationExit;
|
||||
|
||||
passwords = new ObservableCollection<Passwords>(EstadoPersistente.Instance.Passwords);
|
||||
|
||||
AgregarCommand = new RelayCommand(Agregar);
|
||||
EliminarCommand = new RelayCommand<Passwords>(Eliminar);
|
||||
}
|
||||
|
||||
private void OnApplicationExit(object sender, ExitEventArgs e)
|
||||
|
@ -36,6 +64,7 @@ namespace EscribePassword
|
|||
EstadoPersistente.Instance.GuardarEstado();
|
||||
}
|
||||
|
||||
[RelayCommand]
|
||||
private void Agregar()
|
||||
{
|
||||
Passwords newPassword = new Passwords { Usuario = "NuevoUsuario", Password = "NuevaContraseña" };
|
||||
|
@ -43,6 +72,7 @@ namespace EscribePassword
|
|||
SelectedPassword = newPassword;
|
||||
}
|
||||
|
||||
[RelayCommand]
|
||||
private void Eliminar(Passwords password)
|
||||
{
|
||||
if (password != null)
|
||||
|
@ -51,9 +81,29 @@ namespace EscribePassword
|
|||
SelectedPassword = null;
|
||||
}
|
||||
}
|
||||
|
||||
[RelayCommand]
|
||||
private void Utilizar(Passwords password)
|
||||
{
|
||||
if (password != null)
|
||||
{
|
||||
((App)Application.Current).PasteLogic.RestoreAndSimulatePaste(password);
|
||||
|
||||
// Cerrar la aplicación después de la acción
|
||||
Application.Current.Shutdown();
|
||||
}
|
||||
}
|
||||
|
||||
[RelayCommand]
|
||||
private void Cerrar()
|
||||
{
|
||||
// Cerrar la aplicación después de la acción
|
||||
Application.Current.Shutdown();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
public class EstadoPersistente
|
||||
internal class EstadoPersistente
|
||||
{
|
||||
// Ruta donde se guardará el estado
|
||||
private static readonly string _filePath = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), "estado.json");
|
||||
|
@ -124,13 +174,4 @@ namespace EscribePassword
|
|||
}
|
||||
}
|
||||
}
|
||||
|
||||
public partial class Passwords : ObservableObject
|
||||
{
|
||||
[ObservableProperty]
|
||||
private string usuario;
|
||||
|
||||
[ObservableProperty]
|
||||
private string password;
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue