Kyungshin-Welder/Machines/ILSOS_KS/Views/PopUP/PopupUpWindows.xaml.cs
2026-01-26 14:51:28 +09:00

137 lines
4.6 KiB
C#

using OpenCvSharp;
using OpenCvSharp.WpfExtensions;
using System;
using System.Collections.Generic;
using System.ComponentModel;
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.Interop;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
namespace ILSOS_KS.Views.PopUP
{
public class BoolToVisibilityConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
=> (value is bool b && b) ? Visibility.Visible : Visibility.Collapsed;
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
=> (value is Visibility v && v == Visibility.Visible);
}
public class InverseBoolToVisibilityConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
=> (value is bool b && b) ? Visibility.Collapsed : Visibility.Visible;
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
=> !(value is Visibility v && v == Visibility.Visible);
}
/// <summary>
/// PopupUpWindows.xaml에 대한 상호 작용 논리
/// </summary>
public partial class PopupUpWindows : System.Windows.Window
{
public PopupUpWindows(PopupWindowViewModel viewModel)
{
InitializeComponent();
DataContext = viewModel;
this.Title = "PopUpWindows";
Loaded += (s, e) =>
{
// 세로 기준 화면 90%, 비율 1:1, 가로 추가 100px
AdjustWindowSize(0.95, 1, 1, 100 + 468);
CenterOnActiveScreen();
};
}
private void AdjustWindowSize(double heightPercent, double aspectWidth, double aspectHeight, double extraWidth = 0)
{
var vm = DataContext as PopupWindowViewModel;
if (vm == null) return;
// 현재 화면 가져오기
var screen = System.Windows.Forms.Screen.FromHandle(new System.Windows.Interop.WindowInteropHelper(this).Handle);
var workingArea = screen.WorkingArea;
// DPI 보정
double dpiX = GetDpiX();
double dpiY = GetDpiY();
// 화면 세로 기준 비율 적용
double targetHeight = workingArea.Height * heightPercent * (96.0 / dpiY);
// 가로/세로 비율 계산
double aspectRatio = aspectWidth / aspectHeight;
// 가로는 비율에 맞춰 계산 + 추가 가로
double targetWidth = targetHeight * aspectRatio + extraWidth;
// 화면 너비보다 크면 화면에 맞춰 조정
double maxWidth = workingArea.Width * (96.0 / dpiX);
if (targetWidth > maxWidth)
{
targetWidth = maxWidth;
targetHeight = targetWidth / aspectRatio; // 비율 유지
}
// 창 크기 적용
this.Width = targetWidth;
this.Height = targetHeight;
}
private void CenterOnActiveScreen()
{
var hwnd = new System.Windows.Interop.WindowInteropHelper(this).Handle;
if (hwnd == IntPtr.Zero) return;
var screen = System.Windows.Forms.Screen.FromHandle(hwnd);
var workingArea = screen.WorkingArea;
double dpiX = GetDpiX();
double dpiY = GetDpiY();
double workAreaLeft = workingArea.Left * 96.0 / dpiX;
double workAreaTop = workingArea.Top * 96.0 / dpiY;
double workAreaWidth = workingArea.Width * 96.0 / dpiX;
double workAreaHeight = workingArea.Height * 96.0 / dpiY;
Left = workAreaLeft + (workAreaWidth - ActualWidth) / 2;
Top = workAreaTop + (workAreaHeight - ActualHeight) / 2;
}
private double GetDpiX()
{
var source = PresentationSource.FromVisual(this);
return source?.CompositionTarget != null
? 96.0 * source.CompositionTarget.TransformToDevice.M11
: 96.0;
}
private double GetDpiY()
{
var source = PresentationSource.FromVisual(this);
return source?.CompositionTarget != null
? 96.0 * source.CompositionTarget.TransformToDevice.M22
: 96.0;
}
}
}