GTPCorrgir/VentanaResultado.xaml.cs

118 lines
4.2 KiB
C#
Raw Normal View History

using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Forms;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using Brushes = System.Windows.Media.Brushes;
using Size = System.Windows.Size;
using Application = System.Windows.Application;
using System.Runtime.InteropServices;
using MouseEventArgs = System.Windows.Input.MouseEventArgs;
using FlowDirection = System.Windows.FlowDirection;
namespace GTPCorrgir
{
/// <summary>
/// Interaction logic for VentanaResultado.xaml
/// </summary>
public partial class VentanaResultado : Window
{
[StructLayout(LayoutKind.Sequential)]
public struct POINT
{
public int X;
public int Y;
}
[DllImport("user32.dll")]
public static extern bool GetCursorPos(out POINT lpPoint);
public VentanaResultado(string message)
{
InitializeComponent();
MessageText.Text = message;
// Medir el tamaño del texto
System.Windows.Size textSize = MeasureTextSize(message);
// Ajustar el tamaño del TextBlock
MessageText.Width = textSize.Width; // Establece el ancho real del TextBlock al ancho medido
MessageText.MaxWidth = SystemParameters.PrimaryScreenWidth * 2 / 3; // Límite máximo si necesario
MessageText.MaxHeight = SystemParameters.WorkArea.Height / 2; // Límite máximo de altura
// Configurar el tamaño de la ventana basado en el tamaño del texto
this.Width = MessageText.Width + 20; // Considera los paddings del borde
this.Height = textSize.Height + 20;
// Posiciona la ventana
POINT cursorPoint;
if (GetCursorPos(out cursorPoint))
{
this.Left = cursorPoint.X - this.Width / 2;
this.Top = cursorPoint.Y - this.Height / 2;
}
// Eventos para cerrar y mover la ventana
this.MouseLeftButtonDown += (s, e) => this.Close();
this.MouseMove += MainWindow_MouseMove;
this.MouseLeave += MainWindow_MouseLeave;
}
private void MainWindow_MouseLeave(object sender, MouseEventArgs e)
{
Application.Current.Shutdown(); // Terminar la aplicación
}
private void MainWindow_MouseMove(object sender, MouseEventArgs e)
{
POINT cursorPoint;
if (GetCursorPos(out cursorPoint))
{
this.Left = cursorPoint.X - this.Width / 2;
this.Top = cursorPoint.Y - this.Height / 2;
}
}
private Size MeasureTextSize(string message)
{
// Establece el ancho máximo del texto como dos tercios del ancho de la pantalla
double maxWidth = SystemParameters.PrimaryScreenWidth * 2 / 3;
// Configurando FormattedText con el ancho máximo
var formattedText = new FormattedText(
message,
CultureInfo.CurrentCulture,
FlowDirection.LeftToRight,
new Typeface(MessageText.FontFamily, MessageText.FontStyle, MessageText.FontWeight, MessageText.FontStretch),
MessageText.FontSize,
Brushes.Black, // Asegúrate de que estás usando System.Windows.Media.Brushes
new NumberSubstitution(),
TextFormattingMode.Display);
// Aplicar el ancho máximo para que el texto se ajuste dentro de este límite
formattedText.MaxTextWidth = maxWidth;
// Calcular la altura necesaria para todo el texto dentro del ancho máximo
return new Size(formattedText.Width, formattedText.Height);
}
private void Window_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
{
this.Close(); // Cerrar la ventana al hacer clic
Application.Current.Shutdown(); // Terminar la aplicación
}
}
}