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.Configuration;
|
||||||
using System.Data;
|
using System.Data;
|
||||||
using System.Windows;
|
using System.Windows;
|
||||||
|
using Application = System.Windows.Application;
|
||||||
|
|
||||||
namespace EscribePassword
|
namespace EscribePassword
|
||||||
{
|
{
|
||||||
|
@ -9,6 +10,49 @@ namespace EscribePassword
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public partial class App : Application
|
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>
|
<Nullable>enable</Nullable>
|
||||||
<ImplicitUsings>enable</ImplicitUsings>
|
<ImplicitUsings>enable</ImplicitUsings>
|
||||||
<UseWPF>true</UseWPF>
|
<UseWPF>true</UseWPF>
|
||||||
|
<UseWindowsForms>True</UseWindowsForms>
|
||||||
</PropertyGroup>
|
</PropertyGroup>
|
||||||
|
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
<PackageReference Include="CommunityToolkit.Mvvm" Version="8.2.2" />
|
<PackageReference Include="CommunityToolkit.Mvvm" Version="8.2.2" />
|
||||||
<PackageReference Include="Extended.Wpf.Toolkit" Version="4.6.0" />
|
<PackageReference Include="Extended.Wpf.Toolkit" Version="4.6.0" />
|
||||||
|
<PackageReference Include="InputSimulator" Version="1.0.4" />
|
||||||
<PackageReference Include="Newtonsoft.Json" Version="13.0.3" />
|
<PackageReference Include="Newtonsoft.Json" Version="13.0.3" />
|
||||||
</ItemGroup>
|
</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:x="http://schemas.microsoft.com/winfx/2006/xaml"
|
||||||
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
|
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
|
||||||
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
|
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>
|
<Window.DataContext>
|
||||||
<local:MView />
|
<local:MView />
|
||||||
</Window.DataContext>
|
</Window.DataContext>
|
||||||
<Grid>
|
<Grid>
|
||||||
<Grid.ColumnDefinitions>
|
|
||||||
<ColumnDefinition Width="*" />
|
|
||||||
<ColumnDefinition Width="2*" />
|
|
||||||
</Grid.ColumnDefinitions>
|
|
||||||
<Grid.RowDefinitions>
|
<Grid.RowDefinitions>
|
||||||
<RowDefinition Height="*" />
|
<RowDefinition Height="*" />
|
||||||
<RowDefinition Height="Auto" />
|
<RowDefinition Height="Auto" />
|
||||||
</Grid.RowDefinitions>
|
</Grid.RowDefinitions>
|
||||||
<DataGrid Grid.Column="0" Grid.Row="0" ItemsSource="{Binding Passwords}"
|
<DataGrid Grid.Row="0" ItemsSource="{Binding Passwords}" SelectedItem="{Binding SelectedPassword}"
|
||||||
SelectedItem="{Binding SelectedPassword}" AutoGenerateColumns="False" CanUserAddRows="False"
|
AutoGenerateColumns="False" CanUserAddRows="False" CanUserDeleteRows="False">
|
||||||
CanUserDeleteRows="False">
|
|
||||||
<DataGrid.Columns>
|
<DataGrid.Columns>
|
||||||
<DataGridTextColumn Header="Usuario" Binding="{Binding Usuario}" Width="*" />
|
<DataGridTextColumn Header="Usuario" Binding="{Binding Usuario}" Width="*" />
|
||||||
<DataGridTextColumn Header="Contraseña" Binding="{Binding Password}" Width="*" />
|
<DataGridTextColumn Header="Contraseña" Binding="{Binding Password}" Width="*" />
|
||||||
<DataGridTemplateColumn Header="Acciones" Width="Auto">
|
<DataGridTemplateColumn Header="Acciones" Width="Auto">
|
||||||
<DataGridTemplateColumn.CellTemplate>
|
<DataGridTemplateColumn.CellTemplate>
|
||||||
<DataTemplate>
|
<DataTemplate>
|
||||||
|
<StackPanel Orientation="Horizontal">
|
||||||
<Button Content="Eliminar"
|
<Button Content="Eliminar"
|
||||||
Command="{Binding DataContext.EliminarCommand, RelativeSource={RelativeSource AncestorType=DataGrid}}"
|
Command="{Binding DataContext.EliminarCommand, RelativeSource={RelativeSource AncestorType=DataGrid}}"
|
||||||
CommandParameter="{Binding}" />
|
CommandParameter="{Binding}" />
|
||||||
|
<Button Content="Utilizar"
|
||||||
|
Command="{Binding DataContext.UtilizarCommand, RelativeSource={RelativeSource AncestorType=DataGrid}}"
|
||||||
|
CommandParameter="{Binding}" Margin="5,0,0,0" />
|
||||||
|
</StackPanel>
|
||||||
</DataTemplate>
|
</DataTemplate>
|
||||||
</DataGridTemplateColumn.CellTemplate>
|
</DataGridTemplateColumn.CellTemplate>
|
||||||
</DataGridTemplateColumn>
|
</DataGridTemplateColumn>
|
||||||
</DataGrid.Columns>
|
</DataGrid.Columns>
|
||||||
</DataGrid>
|
</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="Agregar" Command="{Binding AgregarCommand}" Margin="5" />
|
||||||
|
<Button Content="Cerrar" Command="{Binding CerrarCommand}" Margin="5" />
|
||||||
</StackPanel>
|
</StackPanel>
|
||||||
</Grid>
|
</Grid>
|
||||||
</Window>
|
</Window>
|
||||||
|
|
|
@ -5,9 +5,43 @@ using System.Collections.ObjectModel;
|
||||||
using System.Text.Json;
|
using System.Text.Json;
|
||||||
using System.IO;
|
using System.IO;
|
||||||
using System.Windows;
|
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
|
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
|
public partial class MView : ObservableObject
|
||||||
{
|
{
|
||||||
[ObservableProperty]
|
[ObservableProperty]
|
||||||
|
@ -16,18 +50,12 @@ namespace EscribePassword
|
||||||
[ObservableProperty]
|
[ObservableProperty]
|
||||||
private Passwords selectedPassword;
|
private Passwords selectedPassword;
|
||||||
|
|
||||||
public RelayCommand AgregarCommand { get; }
|
|
||||||
public RelayCommand<Passwords> EliminarCommand { get; }
|
|
||||||
|
|
||||||
public MView()
|
public MView()
|
||||||
{
|
{
|
||||||
// Suscribirse al evento de cierre de la aplicación
|
// Suscribirse al evento de cierre de la aplicación
|
||||||
Application.Current.Exit += OnApplicationExit;
|
Application.Current.Exit += OnApplicationExit;
|
||||||
|
|
||||||
passwords = new ObservableCollection<Passwords>(EstadoPersistente.Instance.Passwords);
|
passwords = new ObservableCollection<Passwords>(EstadoPersistente.Instance.Passwords);
|
||||||
|
|
||||||
AgregarCommand = new RelayCommand(Agregar);
|
|
||||||
EliminarCommand = new RelayCommand<Passwords>(Eliminar);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private void OnApplicationExit(object sender, ExitEventArgs e)
|
private void OnApplicationExit(object sender, ExitEventArgs e)
|
||||||
|
@ -36,6 +64,7 @@ namespace EscribePassword
|
||||||
EstadoPersistente.Instance.GuardarEstado();
|
EstadoPersistente.Instance.GuardarEstado();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
[RelayCommand]
|
||||||
private void Agregar()
|
private void Agregar()
|
||||||
{
|
{
|
||||||
Passwords newPassword = new Passwords { Usuario = "NuevoUsuario", Password = "NuevaContraseña" };
|
Passwords newPassword = new Passwords { Usuario = "NuevoUsuario", Password = "NuevaContraseña" };
|
||||||
|
@ -43,6 +72,7 @@ namespace EscribePassword
|
||||||
SelectedPassword = newPassword;
|
SelectedPassword = newPassword;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
[RelayCommand]
|
||||||
private void Eliminar(Passwords password)
|
private void Eliminar(Passwords password)
|
||||||
{
|
{
|
||||||
if (password != null)
|
if (password != null)
|
||||||
|
@ -51,9 +81,29 @@ namespace EscribePassword
|
||||||
SelectedPassword = null;
|
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();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public class EstadoPersistente
|
[RelayCommand]
|
||||||
|
private void Cerrar()
|
||||||
|
{
|
||||||
|
// Cerrar la aplicación después de la acción
|
||||||
|
Application.Current.Shutdown();
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
internal class EstadoPersistente
|
||||||
{
|
{
|
||||||
// Ruta donde se guardará el estado
|
// Ruta donde se guardará el estado
|
||||||
private static readonly string _filePath = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), "estado.json");
|
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