更新
This commit is contained in:
@@ -239,16 +239,13 @@ public class TitleBar : System.Windows.Controls.Control
|
||||
remove => RemoveHandler(MinimizeClickedEvent, value);
|
||||
}
|
||||
|
||||
private void CloseWindow()
|
||||
{
|
||||
currentWindow.Close();
|
||||
}
|
||||
private void CloseWindow() { currentWindow.Close(); }
|
||||
|
||||
private T GetTemplateChild<T>(string name) where T : DependencyObject
|
||||
{
|
||||
var element = GetTemplateChild(name);
|
||||
|
||||
if (element is not T tElement)
|
||||
if(element is not T tElement)
|
||||
{
|
||||
throw new InvalidOperationException($"Template part '{name}' is not found or is not of type {typeof(T)}");
|
||||
}
|
||||
@@ -260,7 +257,7 @@ public class TitleBar : System.Windows.Controls.Control
|
||||
{
|
||||
var message = (User32.WM)msg;
|
||||
|
||||
if (message
|
||||
if(message
|
||||
is not (
|
||||
User32.WM.NCHITTEST
|
||||
or User32.WM.NCMOUSELEAVE
|
||||
@@ -271,22 +268,22 @@ public class TitleBar : System.Windows.Controls.Control
|
||||
return IntPtr.Zero;
|
||||
}
|
||||
|
||||
foreach (TitleBarButton button in buttons)
|
||||
foreach(TitleBarButton button in buttons)
|
||||
{
|
||||
if (!button.ReactToHwndHook(message, lParam, out IntPtr returnIntPtr))
|
||||
if(!button.ReactToHwndHook(message, lParam, out IntPtr returnIntPtr))
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
// 修复了有时未正确清除按钮悬停背景,导致多个按钮看起来好像悬停的问题。
|
||||
foreach (TitleBarButton anotherButton in buttons)
|
||||
foreach(TitleBarButton anotherButton in buttons)
|
||||
{
|
||||
if (anotherButton == button)
|
||||
if(anotherButton == button)
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
if (anotherButton.IsHovered && button.IsHovered)
|
||||
if(anotherButton.IsHovered && button.IsHovered)
|
||||
{
|
||||
anotherButton.RemoveHover();
|
||||
}
|
||||
@@ -313,7 +310,7 @@ public class TitleBar : System.Windows.Controls.Control
|
||||
// }
|
||||
//}
|
||||
|
||||
switch (message)
|
||||
switch(message)
|
||||
{
|
||||
case User32.WM.NCHITTEST when CloseWindowByDoubleClickOnIcon && icon.IsMouseOverElement(lParam):
|
||||
// Ideally, clicking on the icon should open the system menu, but when the system menu is opened manually, double-clicking on the icon does not close the window
|
||||
@@ -329,19 +326,19 @@ public class TitleBar : System.Windows.Controls.Control
|
||||
|
||||
private void MaximizeWindow()
|
||||
{
|
||||
if (!CanMaximize)
|
||||
if(!CanMaximize)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
if (MaximizeActionOverride is not null)
|
||||
if(MaximizeActionOverride is not null)
|
||||
{
|
||||
MaximizeActionOverride(this, currentWindow);
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
if (currentWindow.WindowState == WindowState.Normal)
|
||||
if(currentWindow.WindowState == WindowState.Normal)
|
||||
{
|
||||
SetCurrentValue(IsMaximizedProperty, true);
|
||||
currentWindow.SetCurrentValue(Window.WindowStateProperty, WindowState.Maximized);
|
||||
@@ -355,7 +352,7 @@ public class TitleBar : System.Windows.Controls.Control
|
||||
|
||||
private void MinimizeWindow()
|
||||
{
|
||||
if (MinimizeActionOverride is not null)
|
||||
if(MinimizeActionOverride is not null)
|
||||
{
|
||||
MinimizeActionOverride(this, currentWindow);
|
||||
|
||||
@@ -367,18 +364,19 @@ public class TitleBar : System.Windows.Controls.Control
|
||||
|
||||
private void OnParentWindowStateChanged(object sender, EventArgs e)
|
||||
{
|
||||
if (IsMaximized != (currentWindow.WindowState == WindowState.Maximized))
|
||||
if(IsMaximized != (currentWindow.WindowState == WindowState.Maximized))
|
||||
{
|
||||
SetCurrentValue(IsMaximizedProperty, currentWindow.WindowState == WindowState.Maximized);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 设置窗口状态
|
||||
/// </summary>
|
||||
/// <param name="buttonType"></param>
|
||||
private void OnTemplateButtonClick(TitleBarButtonType buttonType)
|
||||
{
|
||||
switch (buttonType)
|
||||
switch(buttonType)
|
||||
{
|
||||
case TitleBarButtonType.Maximize
|
||||
or TitleBarButtonType.Restore:
|
||||
@@ -413,7 +411,7 @@ public class TitleBar : System.Windows.Controls.Control
|
||||
/// </summary>
|
||||
private void OnWindowContentRendered(object sender, EventArgs e)
|
||||
{
|
||||
if (sender is not Window window)
|
||||
if(sender is not Window window)
|
||||
{
|
||||
return;
|
||||
}
|
||||
@@ -433,7 +431,7 @@ public class TitleBar : System.Windows.Controls.Control
|
||||
{
|
||||
var point = PointToScreen(e.GetPosition(this));
|
||||
|
||||
if (dpiScale is null)
|
||||
if(dpiScale is null)
|
||||
{
|
||||
throw new InvalidOperationException("dpiScale is not initialized.");
|
||||
}
|
||||
@@ -445,8 +443,10 @@ public class TitleBar : System.Windows.Controls.Control
|
||||
|
||||
protected virtual void OnLoaded(object sender, RoutedEventArgs e)
|
||||
{
|
||||
currentWindow =
|
||||
System.Windows.Window.GetWindow(this) ?? throw new InvalidOperationException("Window is null");
|
||||
// 添加设计时检查
|
||||
if (DesignerProperties.GetIsInDesignMode(this))
|
||||
return;
|
||||
currentWindow = System.Windows.Window.GetWindow(this) ?? throw new InvalidOperationException("Window is null");
|
||||
currentWindow.StateChanged += OnParentWindowStateChanged;
|
||||
currentWindow.ContentRendered += OnWindowContentRendered;
|
||||
}
|
||||
@@ -466,7 +466,7 @@ public class TitleBar : System.Windows.Controls.Control
|
||||
|
||||
_parentWindow = VisualTreeHelper.GetParent(this);
|
||||
|
||||
while (_parentWindow is not null and not Window)
|
||||
while(_parentWindow is not null and not Window)
|
||||
{
|
||||
_parentWindow = VisualTreeHelper.GetParent(_parentWindow);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user