自定义窗体标题栏及尺寸调整手柄完成
This commit is contained in:
@@ -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:vm="using:MVVMExample.ViewModels"
|
||||
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
|
||||
@@ -6,15 +6,39 @@
|
||||
mc:Ignorable="d" d:DesignWidth="800" d:DesignHeight="450"
|
||||
x:Class="MVVMExample.Views.MainWindow"
|
||||
x:DataType="vm:MainWindowViewModel"
|
||||
SystemDecorations="None"
|
||||
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>
|
||||
<!-- 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/>
|
||||
</Design.DataContext>
|
||||
|
||||
<TextBlock Text="{Binding Greeting}" HorizontalAlignment="Center" VerticalAlignment="Center"/>
|
||||
|
||||
<Grid>
|
||||
<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>
|
||||
|
||||
@@ -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();
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user