multiple instances of mask windows #481

This commit is contained in:
辉鸭蛋 2024-07-13 10:17:07 +08:00
parent ed20c8bb1d
commit 35354ede17
4 changed files with 61 additions and 70 deletions

View File

@ -47,9 +47,6 @@ public partial class App : Application
Directory.CreateDirectory(logFolder);
var logFile = Path.Combine(logFolder, "better-genshin-impact.log");
var maskWindow = new MaskWindow();
services.AddSingleton(maskWindow);
var richTextBox = new RichTextBoxImpl();
services.AddSingleton<IRichTextBox>(richTextBox);

View File

@ -105,7 +105,6 @@ namespace BetterGenshinImpact.GameTask
// 初始化任务上下文(一定要在初始化触发器前完成)
TaskContext.Instance().Init(hWnd);
PrintSystemInfo();
// 初始化触发器(一定要在任务上下文初始化完毕后使用)
_triggers = GameTaskManager.LoadTriggers();
@ -154,21 +153,6 @@ namespace BetterGenshinImpact.GameTask
}
}
private void PrintSystemInfo()
{
var systemInfo = TaskContext.Instance().SystemInfo;
var width = systemInfo.GameScreenSize.Width;
var height = systemInfo.GameScreenSize.Height;
var dpiScale = TaskContext.Instance().DpiScale;
_logger.LogInformation("截图器已启动,游戏大小{Width}x{Height},素材缩放{Scale}DPI缩放{Dpi}",
width, height, systemInfo.AssetScale.ToString("F"), dpiScale);
if (width * 9 != height * 16)
{
_logger.LogWarning("当前游戏分辨率不是16:9部分功能可能无法正常使用");
}
}
public void Stop()
{
_timer.Stop();
@ -285,11 +269,11 @@ namespace BetterGenshinImpact.GameTask
Debug.WriteLine("游戏窗口不在前台, 不再进行截屏");
}
var pName = SystemControl.GetActiveProcessName();
if (pName != "BetterGI" && pName != "YuanShen" && pName != "GenshinImpact" && pName != "Genshin Impact Cloud Game")
{
maskWindow.Invoke(() => { maskWindow.Hide(); });
}
// var pName = SystemControl.GetActiveProcessName();
// if (pName != "BetterGI" && pName != "YuanShen" && pName != "GenshinImpact" && pName != "Genshin Impact Cloud Game")
// {
// maskWindow.Invoke(() => { maskWindow.Hide(); });
// }
_prevGameActive = active;
@ -313,16 +297,16 @@ namespace BetterGenshinImpact.GameTask
}
else
{
if (!_prevGameActive)
{
maskWindow.Invoke(() =>
{
if (!maskWindow.IsClosed)
{
maskWindow.Show();
}
});
}
// if (!_prevGameActive)
// {
// maskWindow.Invoke(() =>
// {
// if (maskWindow.IsExist())
// {
// maskWindow.Show();
// }
// });
// }
_prevGameActive = active;
// 移动游戏窗口的时候同步遮罩窗口的位置,此时不进行捕获
@ -426,7 +410,7 @@ namespace BetterGenshinImpact.GameTask
_gameRect = new RECT(currentRect);
double scale = TaskContext.Instance().DpiScale;
TaskContext.Instance().SystemInfo.CaptureAreaRect = currentRect;
MaskWindow.Instance().RefreshPosition(currentRect, scale);
MaskWindow.Instance().RefreshPosition();
return true;
}

View File

@ -19,6 +19,8 @@ using System.Windows.Media;
using System.Windows.Threading;
using Vanara.PInvoke;
using FontFamily = System.Windows.Media.FontFamily;
using BetterGenshinImpact.ViewModel;
using Microsoft.Extensions.Logging;
namespace BetterGenshinImpact.View;
@ -36,6 +38,8 @@ public partial class MaskWindow : Window
private IRichTextBox? _richTextBox;
private readonly ILogger<MaskWindow> _logger = App.GetLogger<MaskWindow>();
static MaskWindow()
{
if (Application.Current.TryFindResource("TextThemeFontFamily") is FontFamily fontFamily)
@ -60,41 +64,27 @@ public partial class MaskWindow : Window
return _maskWindow;
}
public bool IsClosed { get; private set; }
protected override void OnClosed(EventArgs e)
public bool IsExist()
{
base.OnClosed(e);
IsClosed = true;
return _maskWindow != null && PresentationSource.FromVisual(_maskWindow) != null;
}
public void RefreshPosition(IntPtr hWnd)
public void RefreshPosition()
{
var currentRect = SystemControl.GetCaptureRect(hWnd);
double dpiScale = DpiHelper.ScaleY;
RefreshPosition(currentRect, dpiScale);
}
nint targetHWnd = TaskContext.Instance().GameHandle;
_ = User32.GetClientRect(targetHWnd, out RECT targetRect);
float x = DpiHelper.GetScale(targetHWnd).X;
_ = User32.SetWindowPos(_hWnd, IntPtr.Zero, 0, 0, (int)(targetRect.Width * x), (int)(targetRect.Height * x), User32.SetWindowPosFlags.SWP_SHOWWINDOW);
public void RefreshPosition(RECT currentRect, double dpiScale)
{
// TODO如果窗口被关闭则需要调用
// _richTextBox.RichTextBox = null!;
// TODO重写下面代码适应BGI
//nint targetHWnd = TaskContext.Instance().GameHandle;
//_ = User32.GetClientRect(targetHWnd, out RECT targetRect);
//float x = DpiHelper.GetScale(targetHWnd).X;
//_ = User32.SetWindowPos(_hWnd, IntPtr.Zero, 0, 0, (int)(targetRect.Width * x), (int)(targetRect.Height * x), User32.SetWindowPosFlags.SWP_SHOWWINDOW);
//Invoke(() =>
//{
// Canvas.SetTop(LogTextBoxWrapper, Height - LogTextBoxWrapper.Height - 65);
// Canvas.SetTop(StatusWrapper, Height - LogTextBoxWrapper.Height - 90);
//});
Invoke(() =>
{
Canvas.SetTop(LogTextBoxWrapper, Height - LogTextBoxWrapper.Height - 65);
Canvas.SetTop(StatusWrapper, Height - LogTextBoxWrapper.Height - 90);
});
// 重新计算控件位置
// shit code 预定了
//WeakReferenceMessenger.Default.Send(new PropertyChangedMessage<object>(this, "RefreshSettings", new object(), "重新计算控件位置"));
WeakReferenceMessenger.Default.Send(new PropertyChangedMessage<object>(this, "RefreshSettings", new object(), "重新计算控件位置"));
}
public MaskWindow()
@ -120,13 +110,25 @@ public partial class MaskWindow : Window
_hWnd = new WindowInteropHelper(this).Handle;
nint targetHWnd = TaskContext.Instance().GameHandle;
_ = User32.GetClientRect(_hWnd, out RECT rect);
_ = User32.SetParent(_hWnd, targetHWnd);
_ = User32.GetClientRect(targetHWnd, out RECT targetRect);
float x = DpiHelper.GetScale(targetHWnd).X;
_ = User32.SetWindowPos(_hWnd, IntPtr.Zero, 0, 0, (int)(targetRect.Width * x), (int)(targetRect.Height * x), User32.SetWindowPosFlags.SWP_SHOWWINDOW);
RefreshPosition();
PrintSystemInfo();
}
private void PrintSystemInfo()
{
var systemInfo = TaskContext.Instance().SystemInfo;
var width = systemInfo.GameScreenSize.Width;
var height = systemInfo.GameScreenSize.Height;
var dpiScale = TaskContext.Instance().DpiScale;
_logger.LogInformation("遮罩窗口已启动,游戏大小{Width}x{Height},素材缩放{Scale}DPI缩放{Dpi}",
width, height, systemInfo.AssetScale.ToString("F"), dpiScale);
if (width * 9 != height * 16)
{
_logger.LogWarning("当前游戏分辨率不是16:9部分功能可能无法正常使用");
}
}
protected override void OnSourceInitialized(EventArgs e)

View File

@ -202,8 +202,8 @@ public partial class HomePageViewModel : ObservableObject, INavigationAware, IVi
_taskDispatcher.UiTaskStartTickEvent -= OnUiTaskStartTick;
_taskDispatcher.UiTaskStopTickEvent += OnUiTaskStopTick;
_taskDispatcher.UiTaskStartTickEvent += OnUiTaskStartTick;
_maskWindow = MaskWindow.Instance();
_maskWindow.RefreshPosition(hWnd);
_maskWindow ??= new MaskWindow();
_maskWindow.Show();
_mouseKeyMonitor.Subscribe(hWnd);
TaskDispatcherEnabled = true;
}
@ -222,7 +222,15 @@ public partial class HomePageViewModel : ObservableObject, INavigationAware, IVi
if (TaskDispatcherEnabled)
{
_taskDispatcher.Stop();
_maskWindow?.Hide();
if (_maskWindow != null && _maskWindow.IsExist())
{
_maskWindow?.Hide();
}
else
{
_maskWindow?.Close();
_maskWindow = null;
}
TaskDispatcherEnabled = false;
_mouseKeyMonitor.Unsubscribe();
}