自定义窗体标题栏及尺寸调整手柄完成
This commit is contained in:
54
MVVMExample/Helper.cs
Normal file
54
MVVMExample/Helper.cs
Normal file
@@ -0,0 +1,54 @@
|
|||||||
|
using Avalonia.Controls;
|
||||||
|
using Avalonia.Input;
|
||||||
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using System.Linq;
|
||||||
|
using System.Text;
|
||||||
|
using System.Threading.Tasks;
|
||||||
|
|
||||||
|
namespace MVVMExample
|
||||||
|
{
|
||||||
|
public static partial class Helper
|
||||||
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// 绑定自定义窗口标题栏与尺寸调整句柄
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="window">要进行绑定的窗口</param>
|
||||||
|
/// <param name="handle">窗口位置拖动句柄</param>
|
||||||
|
/// <param name="slider">窗口尺寸调整句柄</param>
|
||||||
|
/// <param name="maxBut">最大化按钮</param>
|
||||||
|
/// <param name="minBut">最小化按钮</param>
|
||||||
|
/// <param name="closeBut">关闭按钮</param>
|
||||||
|
public static void BindWindowEdge(this Window window, InputElement? handle = null, InputElement? slider = null, Button? maxBut = null, Button? minBut = null, Button? closeBut = null)
|
||||||
|
{
|
||||||
|
// 隐藏窗口默认标题栏与尺寸调整句柄
|
||||||
|
window.SystemDecorations = SystemDecorations.None;
|
||||||
|
|
||||||
|
if (handle != null)
|
||||||
|
handle.PointerPressed += (s, e) =>
|
||||||
|
{
|
||||||
|
if (e.GetCurrentPoint(window).Properties.IsLeftButtonPressed) window.BeginMoveDrag(e); // 关键:开始窗口拖拽
|
||||||
|
};
|
||||||
|
if (slider != null)
|
||||||
|
slider.PointerPressed += (s, e) =>
|
||||||
|
{
|
||||||
|
if (e.GetCurrentPoint(window).Properties.IsLeftButtonPressed) window.BeginResizeDrag(WindowEdge.SouthEast, e);
|
||||||
|
};
|
||||||
|
if (maxBut != null)
|
||||||
|
maxBut.Click += (s, e) =>
|
||||||
|
{
|
||||||
|
window.WindowState = window.WindowState == WindowState.Maximized ? WindowState.Normal : WindowState.Maximized;
|
||||||
|
};
|
||||||
|
if (minBut != null)
|
||||||
|
minBut.Click += (s, e) =>
|
||||||
|
{
|
||||||
|
window.WindowState = WindowState.Minimized;
|
||||||
|
};
|
||||||
|
if (closeBut != null)
|
||||||
|
closeBut.Click += (s, e) =>
|
||||||
|
{
|
||||||
|
window.Close();
|
||||||
|
};
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
16
MVVMExample/Themes/DefaultStyles.axaml
Normal file
16
MVVMExample/Themes/DefaultStyles.axaml
Normal file
@@ -0,0 +1,16 @@
|
|||||||
|
<Styles xmlns="https://github.com/avaloniaui"
|
||||||
|
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
|
||||||
|
<Design.PreviewWith>
|
||||||
|
<Border Padding="20">
|
||||||
|
<!-- Add Controls for Previewer Here -->
|
||||||
|
</Border>
|
||||||
|
</Design.PreviewWith>
|
||||||
|
|
||||||
|
<!-- Add Styles Here -->
|
||||||
|
<Style Selector="Button.maxButton">
|
||||||
|
|
||||||
|
</Style>
|
||||||
|
<Style Selector="Button.maxButton:pointerover">
|
||||||
|
|
||||||
|
</Style>
|
||||||
|
</Styles>
|
||||||
@@ -1,4 +1,4 @@
|
|||||||
<Window xmlns="https://github.com/avaloniaui"
|
<Window xmlns="https://github.com/avaloniaui"
|
||||||
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
|
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
|
||||||
xmlns:vm="using:MVVMExample.ViewModels"
|
xmlns:vm="using:MVVMExample.ViewModels"
|
||||||
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
|
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
|
||||||
@@ -6,15 +6,39 @@
|
|||||||
mc:Ignorable="d" d:DesignWidth="800" d:DesignHeight="450"
|
mc:Ignorable="d" d:DesignWidth="800" d:DesignHeight="450"
|
||||||
x:Class="MVVMExample.Views.MainWindow"
|
x:Class="MVVMExample.Views.MainWindow"
|
||||||
x:DataType="vm:MainWindowViewModel"
|
x:DataType="vm:MainWindowViewModel"
|
||||||
|
SystemDecorations="None"
|
||||||
Icon="/Assets/avalonia-logo.ico"
|
Icon="/Assets/avalonia-logo.ico"
|
||||||
Title="MVVMExample">
|
Title="MVVMExample"
|
||||||
|
CanResize="True">
|
||||||
|
<Window.Styles>
|
||||||
|
<Style Selector="Window">
|
||||||
|
<Setter Property="Background" Value="#0F000000"/>
|
||||||
|
</Style>
|
||||||
|
</Window.Styles>
|
||||||
<Design.DataContext>
|
<Design.DataContext>
|
||||||
<!-- This only sets the DataContext for the previewer in an IDE,
|
|
||||||
to set the actual DataContext for runtime, set the DataContext property in code (look at App.axaml.cs) -->
|
|
||||||
<vm:MainWindowViewModel/>
|
<vm:MainWindowViewModel/>
|
||||||
</Design.DataContext>
|
</Design.DataContext>
|
||||||
|
<Grid>
|
||||||
<TextBlock Text="{Binding Greeting}" HorizontalAlignment="Center" VerticalAlignment="Center"/>
|
<Grid.RowDefinitions>
|
||||||
|
<RowDefinition Height="Auto"/> <!-- 标题栏行 -->
|
||||||
|
<RowDefinition Height="*"/> <!-- 内容行 -->
|
||||||
|
<RowDefinition Height="40"/>
|
||||||
|
</Grid.RowDefinitions>
|
||||||
|
<!-- 自定义标题栏 -->
|
||||||
|
<Border Grid.Row="0" Height="40" Background="#007ACC" PointerPressed="TitleBar_PointerPressed"> <!-- 绑定拖拽事件 -->
|
||||||
|
<Grid Margin="10,0">
|
||||||
|
<TextBlock VerticalAlignment="Center" Foreground="White" Text="{Binding Path=Title, RelativeSource={RelativeSource AncestorType=Window}}"/>
|
||||||
|
<StackPanel HorizontalAlignment="Right" Orientation="Horizontal" Spacing="8">
|
||||||
|
<Button x:Name="MinimizeBtn" Content="─" Width="40" Click="MinimizeBtn_Click"/>
|
||||||
|
<Button x:Name="MaximizeBtn" Content="□" Width="40" Click="MaximizeBtn_Click"/>
|
||||||
|
<Button x:Name="CloseBtn" Content="✕" Width="40" Background="Red" Click="CloseBtn_Click"/>
|
||||||
|
</StackPanel>
|
||||||
|
</Grid>
|
||||||
|
</Border>
|
||||||
|
<!-- 主内容区 -->
|
||||||
|
<Border Grid.Row="1" Background="White">
|
||||||
|
<TextBlock Text="你的应用内容在这里" HorizontalAlignment="Center" VerticalAlignment="Center"/>
|
||||||
|
</Border>
|
||||||
|
<Border Grid.Row="2" Width="40" Height="40" Background="Red" HorizontalAlignment="Right" PointerPressed="Bar_PointerPressed"/>
|
||||||
|
</Grid>
|
||||||
</Window>
|
</Window>
|
||||||
|
|||||||
@@ -1,4 +1,6 @@
|
|||||||
using Avalonia.Controls;
|
using Avalonia.Controls;
|
||||||
|
using Avalonia.Input;
|
||||||
|
using Avalonia.Interactivity;
|
||||||
|
|
||||||
namespace MVVMExample.Views
|
namespace MVVMExample.Views
|
||||||
{
|
{
|
||||||
@@ -7,6 +9,43 @@ namespace MVVMExample.Views
|
|||||||
public MainWindow()
|
public MainWindow()
|
||||||
{
|
{
|
||||||
InitializeComponent();
|
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();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Reference in New Issue
Block a user