自定义窗体标题栏及尺寸调整手柄完成

This commit is contained in:
于智纯
2026-01-06 16:37:54 +08:00
parent 171dcffb96
commit 328455e7ed
4 changed files with 142 additions and 9 deletions

View File

@@ -1,4 +1,6 @@
using Avalonia.Controls;
using Avalonia.Controls;
using Avalonia.Input;
using Avalonia.Interactivity;
namespace MVVMExample.Views
{
@@ -7,6 +9,43 @@ namespace MVVMExample.Views
public MainWindow()
{
InitializeComponent();
//
}
// 处理标题栏拖拽以实现窗口移动
private void TitleBar_PointerPressed(object? sender, PointerPressedEventArgs e)
{
if (e.GetCurrentPoint(this).Properties.IsLeftButtonPressed)
{
BeginMoveDrag(e); // 关键:开始窗口拖拽
}
}
private void Bar_PointerPressed(object? sender, PointerPressedEventArgs e)
{
if (e.GetCurrentPoint(this).Properties.IsLeftButtonPressed)
{
BeginResizeDrag(WindowEdge.SouthEast, e); // 关键:开始窗口拖拽
}
}
// 最小化按钮
private void MinimizeBtn_Click(object? sender, RoutedEventArgs e)
{
WindowState = WindowState.Minimized;
}
// 最大化/还原按钮
private void MaximizeBtn_Click(object? sender, RoutedEventArgs e)
{
WindowState = WindowState == WindowState.Maximized
? WindowState.Normal
: WindowState.Maximized;
// 可选:切换最大化/还原按钮的图标
MaximizeBtn.Content = WindowState == WindowState.Maximized ? "❐" : "□";
}
// 关闭按钮
private void CloseBtn_Click(object? sender, RoutedEventArgs e)
{
Close();
}
}
}