mask window mvvm

This commit is contained in:
huiyadanli 2023-10-03 01:51:18 +08:00
parent 5088be2f44
commit de51fbfd0d
5 changed files with 103 additions and 38 deletions

View File

@ -27,7 +27,7 @@ namespace BetterGenshinImpact.View.Drawable
return _uniqueInstance;
}
public ILogger? Log { get; set; }
//public ILogger? Log { get; set; }
public bool Drawable { get; set; }

View File

@ -1,11 +1,31 @@
<Window x:Class="BetterGenshinImpact.View.MaskWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MaskWindow" Height="600" Width="800"
xmlns:viewModel="clr-namespace:BetterGenshinImpact.ViewModel"
xmlns:b="http://schemas.microsoft.com/xaml/behaviors"
Title="MaskWindow"
AllowsTransparency="True"
WindowStyle="None"
Topmost="True"
DataContext="{Binding RelativeSource={RelativeSource Self}}">
Height="{Binding H,Mode=TwoWay}"
Width="{Binding W, Mode=TwoWay}"
Left="{Binding X, Mode=TwoWay}"
Top="{Binding Y, Mode=TwoWay}"
>
<!--Height="{Binding WindowRect.Height,Mode=TwoWay}"
Width="{Binding WindowRect.Width, Mode=TwoWay}"
Left="{Binding WindowRect.X, Mode=TwoWay}"
Top="{Binding WindowRect.Y, Mode=TwoWay}"-->
<Window.DataContext>
<viewModel:MaskWindowViewModel />
</Window.DataContext>
<b:Interaction.Triggers>
<b:EventTrigger EventName="Loaded">
<b:InvokeCommandAction
CommandParameter="{Binding}"
Command="{Binding LoadedCommand}" />
</b:EventTrigger>
</b:Interaction.Triggers>
<!-- ShowInTaskbar="False" -->
<Window.Background>
<SolidColorBrush Color="#FFB0B0B0"

View File

@ -1,4 +1,7 @@
using BetterGenshinImpact.Core.Recognition.OpenCv;
using BetterGenshinImpact.GameTask;
using BetterGenshinImpact.Helpers;
using BetterGenshinImpact.View.Drawable;
using Microsoft.Extensions.Logging;
using System;
using System.Collections.Concurrent;
@ -12,8 +15,8 @@ using System.Windows.Documents;
using System.Windows.Media;
using System.Windows.Shapes;
using System.Windows.Threading;
using BetterGenshinImpact.GameTask;
using BetterGenshinImpact.View.Drawable;
using Vanara.PInvoke;
using FontFamily = System.Windows.Media.FontFamily;
namespace BetterGenshinImpact.View
{
@ -26,15 +29,23 @@ namespace BetterGenshinImpact.View
{
private static MaskWindow? _maskWindow;
public ILogger<MaskWindow>? Logger { get; set; }
private static readonly Typeface MyTypeface = new FontFamily("微软雅黑").GetTypefaces().First();
public static MaskWindow Instance(ILogger<MaskWindow>? logger = null)
public static MaskWindow Instance(IntPtr? hWnd = null)
{
_maskWindow ??= new MaskWindow();
_maskWindow.Logger ??= logger;
VisionContext.Instance().Log ??= logger;
if (hWnd != null)
{
User32.GetWindowRect(hWnd.Value, out var rect);
double scale = DpiHelper.ScaleY;
_maskWindow.Left = (int)Math.Ceiling(rect.X * 1d / scale);
_maskWindow.Top = (int)Math.Ceiling(rect.Y * 1d / scale);
_maskWindow.Width = (int)Math.Ceiling(rect.Width * 1d / scale);
_maskWindow.Height = (int)Math.Ceiling(rect.Height * 1d / scale);
Debug.WriteLine($"原神窗口大小:{rect.Width} x {rect.Height}");
Debug.WriteLine($"原神窗口大小(计算DPI缩放后){_maskWindow.Width} x {_maskWindow.Height}");
}
return _maskWindow;
}
@ -93,7 +104,6 @@ namespace BetterGenshinImpact.View
catch (Exception e)
{
Debug.WriteLine(e);
Logger?.LogError(e, "绘制识别结果时发生错误");
}
base.OnRender(drawingContext);
@ -105,7 +115,6 @@ namespace BetterGenshinImpact.View
public void AddAreaSettingsControl(string name)
{
Logger?.LogInformation("添加设置控件");
var control = new ContentControl();
control.Width = 100;
control.Height = 100;

View File

@ -4,11 +4,13 @@ using BetterGenshinImpact.View;
using BetterGenshinImpact.View.Test;
using CommunityToolkit.Mvvm.ComponentModel;
using CommunityToolkit.Mvvm.Input;
using CommunityToolkit.Mvvm.Messaging;
using Fischless.WindowCapture;
using Microsoft.Extensions.Logging;
using System;
using System.Diagnostics;
using System.Windows;
using CommunityToolkit.Mvvm.Messaging.Messages;
using Vanara.PInvoke;
namespace BetterGenshinImpact.ViewModel
@ -30,7 +32,7 @@ namespace BetterGenshinImpact.ViewModel
{
//TestMask();
//TestRect();
Debug.WriteLine(DpiHelper.ScaleY);
//Debug.WriteLine(DpiHelper.ScaleY);
}
[RelayCommand]
@ -96,34 +98,11 @@ namespace BetterGenshinImpact.ViewModel
private void ShowMaskWindow(IntPtr hWnd)
{
User32.GetWindowRect(hWnd, out var rect);
//var x = rect.X;
//var y = rect.Y;
//var w = rect.Width;
//var h = rect.Height;
var x = (int)Math.Ceiling(rect.X / DpiHelper.ScaleY);
var y = (int)Math.Ceiling(rect.Y / DpiHelper.ScaleY);
var w = (int)Math.Ceiling(rect.Width / DpiHelper.ScaleY);
var h = (int)Math.Ceiling(rect.Height / DpiHelper.ScaleY);
Debug.WriteLine($"原神窗口大小:{rect.Width} x {rect.Height}");
Debug.WriteLine($"原神窗口大小(计算DPI缩放后){w} x {h}");
//var x = 0;
//var y = 0;
//var w = 1200;
//var h = 800;
_maskWindow ??= MaskWindow.Instance();
////window.Owner = this;
_maskWindow.Left = x;
_maskWindow.Top = y;
_maskWindow.Width = w;
_maskWindow.Height = h;
_maskWindow.Logger = App.GetLogger<MaskWindow>();
_maskWindow.Show();
WeakReferenceMessenger.Default.Send(new PropertyChangedMessage<IntPtr>(this, "hWnd", hWnd, hWnd));
_logger.LogInformation("- 遮罩窗口启动成功");
}
}
}

View File

@ -0,0 +1,57 @@
using BetterGenshinImpact.GameTask;
using BetterGenshinImpact.Helpers;
using BetterGenshinImpact.View;
using BetterGenshinImpact.View.Test;
using CommunityToolkit.Mvvm.ComponentModel;
using CommunityToolkit.Mvvm.Input;
using Fischless.WindowCapture;
using Microsoft.Extensions.Logging;
using System;
using System.Diagnostics;
using System.Windows;
using CommunityToolkit.Mvvm.Messaging;
using CommunityToolkit.Mvvm.Messaging.Messages;
using Vanara.PInvoke;
using System.Windows.Threading;
namespace BetterGenshinImpact.ViewModel
{
public partial class MaskWindowViewModel : ObservableRecipient
{
[ObservableProperty] private string[] _modeNames = WindowCaptureFactory.ModeNames();
[ObservableProperty] private Rect _windowRect;
[ObservableProperty] private int _x;
[ObservableProperty] private int _y;
[ObservableProperty] private int _w;
[ObservableProperty] private int _h;
private ILogger<MaskWindowViewModel>? _logger;
public MaskWindowViewModel()
{
WeakReferenceMessenger.Default.Register<PropertyChangedMessage<IntPtr>>(this, (sender, msg) =>
{
User32.GetWindowRect(msg.NewValue, out var rect);
//_windowRect.X = (int)Math.Ceiling(rect.X / DpiHelper.ScaleY);
//_windowRect.Y = (int)Math.Ceiling(rect.Y / DpiHelper.ScaleY);
//_windowRect.Width = (int)Math.Ceiling(rect.Width / DpiHelper.ScaleY);
//_windowRect.Height = (int)Math.Ceiling(rect.Height / DpiHelper.ScaleY);
_x = (int)Math.Ceiling(rect.X / DpiHelper.ScaleY);
_y = (int)Math.Ceiling(rect.Y / DpiHelper.ScaleY);
_w = (int)Math.Ceiling(rect.Width / DpiHelper.ScaleY);
_h = (int)Math.Ceiling(rect.Height / DpiHelper.ScaleY);
Debug.WriteLine($"原神窗口大小:{rect.Width} x {rect.Height}");
Debug.WriteLine($"原神窗口大小(计算DPI缩放后){_w} x {_h}");
});
}
[RelayCommand]
private void OnLoaded()
{
_logger = App.GetLogger<MaskWindowViewModel>();
_logger.LogInformation("遮罩窗口启OnLoaded");
}
}
}