TransparentWithShapes.axaml代码
<Window xmlns="https://github.com/avaloniaui" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" Width="210" Height="170" x:Class="AvaloniaUI.TransparentWithShapes" Title="TransparentWithShapes" SystemDecorations="None" TransparencyLevelHint="Transparent" Background="Transparent"> <Grid x:Name="root"> <!-- 这里的 Path 既画边框/填充,也会用于 Clip --> <Path x:Name="shapePath" Stroke="DarkGray" StrokeThickness="1" Stretch="None"> <Path.Fill> <LinearGradientBrush StartPoint="20%,0%" EndPoint="80%,100%"> <GradientStop Color="White" Offset="0"/> <GradientStop Color="White" Offset="0.45"/> <GradientStop Color="LightBlue" Offset="0.9"/> <GradientStop Color="Gray" Offset="1"/> </LinearGradientBrush> </Path.Fill> <Path.Data> <PathGeometry> <PathFigure StartPoint="20,0" IsClosed="True"> <LineSegment Point="140,0"/> <ArcSegment Point="160,20" Size="20,20" SweepDirection="Clockwise"/> <LineSegment Point="160,60"/> <ArcSegment Point="140,80" Size="20,20" SweepDirection="Clockwise"/> <LineSegment Point="70,80"/> <LineSegment Point="70,130"/> <LineSegment Point="40,80"/> <LineSegment Point="20,80"/> <ArcSegment Point="0,60" Size="20,20" SweepDirection="Clockwise"/> <LineSegment Point="0,20"/> <ArcSegment Point="20,0" Size="20,20" SweepDirection="Clockwise"/> </PathFigure> </PathGeometry> </Path.Data> <Path.RenderTransform> <ScaleTransform ScaleX="1.3" ScaleY="1.3"/> </Path.RenderTransform> </Path> <StackPanel Margin="5"> <Button HorizontalAlignment="Right" Margin="0,5,10,0" Click="cmdClose_Click">x</Button> <TextBlock TextWrapping="Wrap" FontSize="15" HorizontalAlignment="Center" PointerPressed="window_PointerPressed"> This is a balloon-shaped window. </TextBlock> </StackPanel> </Grid> </Window>
TransparentWithShapes.axaml.cs代码
using Avalonia;
using Avalonia.Controls;
using Avalonia.Input;
using Avalonia.Interactivity;
using Avalonia.Markup.Xaml;
using Avalonia.Media;
namespace AvaloniaUI;
public partial class TransparentWithShapes : Window
{
public TransparentWithShapes()
{
InitializeComponent();
ApplyClip();
}
private void ApplyClip()
{
if (shapePath.Data is not Geometry geometry)
return;
Geometry clip;
clip = Geometry.Parse(geometry.ToString() ?? "");
if (shapePath.RenderTransform != null)
{
clip.Transform = new MatrixTransform(
shapePath.RenderTransform.Value
);
}
root.Clip = clip;
}
private void cmdClose_Click(object? sender, RoutedEventArgs e)
{
Close();
}
private void window_PointerPressed(object? sender, PointerPressedEventArgs e)
{
if (e.GetCurrentPoint(this).Properties.IsLeftButtonPressed)
BeginMoveDrag(e);
}
}
运行效果

浙公网安备 33010602011771号