维护更新
This commit is contained in:
@@ -1,10 +1,7 @@
|
||||
using System.Runtime.InteropServices;
|
||||
using System.Windows.Automation.Peers;
|
||||
using System.Windows.Automation.Provider;
|
||||
using System.Diagnostics;
|
||||
using System.Runtime.InteropServices;
|
||||
using System.Windows.Input;
|
||||
|
||||
using Melskin.Assists;
|
||||
|
||||
namespace Melskin.Controls;
|
||||
|
||||
/// <summary>
|
||||
@@ -85,7 +82,10 @@ public class MelWindow : Window
|
||||
private const int WM_NCMOUSELEAVE = 0x02A2;
|
||||
private const int WM_NCLBUTTONDOWN = 0x00A1;
|
||||
private const int WM_NCLBUTTONUP = 0x00A2;
|
||||
private const int WM_SYSCOMMAND = 0x0112;
|
||||
private const int HTMAXBUTTON = 9;
|
||||
private const int SC_MAXIMIZE = 0xF030;
|
||||
private const int SC_RESTORE = 0xF120;
|
||||
private const uint TME_LEAVE = 0x00000002;
|
||||
private const uint TME_NONCLIENT = 0x00000010;
|
||||
|
||||
@@ -93,6 +93,10 @@ public class MelWindow : Window
|
||||
|
||||
private bool isMouseOverMaximizeButton = false;
|
||||
private bool isMaximizeButtonPressed = false;
|
||||
/// <summary>
|
||||
/// 记录点击前的窗口状态,用于判断系统是否已通过 Snap Layout 处理了窗口状态变更。
|
||||
/// </summary>
|
||||
private WindowState windowStateBeforeClick;
|
||||
|
||||
private IntPtr HwndSourceHook(IntPtr hwnd, int msg, IntPtr wParam, IntPtr lParam, ref bool handled)
|
||||
{
|
||||
@@ -101,6 +105,8 @@ public class MelWindow : Window
|
||||
case WM_NCHITTEST:
|
||||
if (IsMouseOverMaximizeButton(lParam))
|
||||
{
|
||||
// 返回 HTMAXBUTTON 让 Windows 11 Shell 知道鼠标在最大化按钮上,
|
||||
// 从而触发 Snap Layout 弹出菜单。
|
||||
handled = true;
|
||||
return new IntPtr(HTMAXBUTTON);
|
||||
}
|
||||
@@ -138,6 +144,7 @@ public class MelWindow : Window
|
||||
if (isMouseOverMaximizeButton)
|
||||
{
|
||||
isMaximizeButtonPressed = true;
|
||||
windowStateBeforeClick = this.WindowState;
|
||||
UpdateMaximizeButtonVisualState();
|
||||
handled = true;
|
||||
}
|
||||
@@ -147,15 +154,27 @@ public class MelWindow : Window
|
||||
if (isMaximizeButtonPressed)
|
||||
{
|
||||
isMaximizeButtonPressed = false;
|
||||
isMouseOverMaximizeButton = false;
|
||||
UpdateMaximizeButtonVisualState();
|
||||
|
||||
if (isMouseOverMaximizeButton)
|
||||
// 检查系统是否已经通过 Snap Layout 处理了窗口状态变更
|
||||
// 如果窗口状态未被系统改变,说明用户只是普通点击最大化按钮
|
||||
if (this.WindowState == windowStateBeforeClick)
|
||||
{
|
||||
maximizeRestoreButton?.RaiseEvent(new RoutedEventArgs(Button.ClickEvent));
|
||||
ToggleWindowState();
|
||||
}
|
||||
handled = true;
|
||||
}
|
||||
break;
|
||||
|
||||
case WM_SYSCOMMAND:
|
||||
var command = wParam.ToInt32() & 0xFFF0;
|
||||
if (command == SC_MAXIMIZE || command == SC_RESTORE)
|
||||
{
|
||||
// 让系统正常处理 Snap Layout 发出的最大化/还原命令
|
||||
// 不要阻止这些消息,以确保 Snap Layout 选择的布局能正确应用
|
||||
}
|
||||
break;
|
||||
}
|
||||
|
||||
return IntPtr.Zero;
|
||||
@@ -163,8 +182,10 @@ public class MelWindow : Window
|
||||
|
||||
private bool IsMouseOverMaximizeButton(IntPtr lParam)
|
||||
{
|
||||
var x = (short)(lParam.ToInt32() & 0xFFFF);
|
||||
var y = (short)(lParam.ToInt32() >> 16);
|
||||
// 使用 ToInt64() 确保在 64 位系统和多显示器环境下坐标解析正确
|
||||
var val = lParam.ToInt64();
|
||||
var x = (short)(val & 0xFFFF);
|
||||
var y = (short)((val >> 16) & 0xFFFF);
|
||||
var mousePos = new Point(x, y);
|
||||
|
||||
var windowPos = this.PointFromScreen(mousePos);
|
||||
@@ -172,6 +193,7 @@ public class MelWindow : Window
|
||||
if (maximizeRestoreButton is Visual maximizeButtonVisual)
|
||||
{
|
||||
var bounds = VisualTreeHelper.GetDescendantBounds(maximizeButtonVisual);
|
||||
Debug.WriteLine(bounds.ToString());
|
||||
var buttonTransform = maximizeButtonVisual.TransformToAncestor(this);
|
||||
var buttonRect = buttonTransform.TransformBounds(bounds);
|
||||
|
||||
@@ -300,43 +322,26 @@ public class MelWindow : Window
|
||||
/// <inheritdoc />
|
||||
public override void OnApplyTemplate()
|
||||
{
|
||||
if (minimizeButton != null)
|
||||
{
|
||||
minimizeButton.Click -= MinimizeButtonClickHandler;
|
||||
}
|
||||
base.OnApplyTemplate();
|
||||
minimizeButton?.Click -= MinimizeButtonClickHandler;
|
||||
|
||||
minimizeButton = GetTemplateChild(VbMinimizeButtonName) as Button;
|
||||
|
||||
if (minimizeButton != null)
|
||||
{
|
||||
minimizeButton.Click += MinimizeButtonClickHandler;
|
||||
}
|
||||
minimizeButton?.Click += MinimizeButtonClickHandler;
|
||||
|
||||
if (maximizeRestoreButton != null)
|
||||
{
|
||||
maximizeRestoreButton.Click -= MaximizeRestoreButtonClickHandler;
|
||||
}
|
||||
maximizeRestoreButton?.Click -= MaximizeRestoreButtonClickHandler;
|
||||
|
||||
maximizeRestoreButton = GetTemplateChild(VbMaximizeRestoreButtonName) as Button;
|
||||
|
||||
if (maximizeRestoreButton != null)
|
||||
{
|
||||
maximizeRestoreButton.Click += MaximizeRestoreButtonClickHandler;
|
||||
}
|
||||
maximizeRestoreButton?.Click += MaximizeRestoreButtonClickHandler;
|
||||
|
||||
if (closeButton != null)
|
||||
{
|
||||
closeButton.Click -= CloseButtonClickHandler;
|
||||
}
|
||||
closeButton?.Click -= CloseButtonClickHandler;
|
||||
|
||||
closeButton = GetTemplateChild(VbCloseButtonName) as Button;
|
||||
|
||||
if (closeButton != null)
|
||||
{
|
||||
closeButton.Click += CloseButtonClickHandler;
|
||||
}
|
||||
closeButton?.Click += CloseButtonClickHandler;
|
||||
|
||||
|
||||
base.OnApplyTemplate();
|
||||
}
|
||||
|
||||
private void CloseButtonClickHandler(object sender, RoutedEventArgs args)
|
||||
|
||||
Reference in New Issue
Block a user