带圆角的 WPF 无边框应用程序

发布于 2024-10-17 23:31:13 字数 583 浏览 1 评论 0原文

我创建了一个无边框的 WPF 应用程序,并且有一个我想作为背景的图像。我在“画图”中创建了图像,因此无法选择使角透明。我想让应用程序具有圆角,但我没有通过 XAML 获得预期结果。我尝试添加边框以获得所需的效果,但是当我运行应用程序时,图像仍然位于边框前面。这是我的代码。我做错了什么?

<Border BorderBrush="Black" CornerRadius="15,15,15,15" BorderThickness="1,1,1,1" Width="242" Height="426">
    <Grid>
        <Image Height="425" Name="image1" Stretch="Fill" Width="240" Source="/Test;component/Test.png" />
        <Grid Height="334" HorizontalAlignment="Left" Margin="24,39,0,0" Name="grid1" VerticalAlignment="Top" Width="198" />
    </Grid>
</Border>

I have created a WPF app that is borderless and have an image that I want to be my background. I created my image in Paint, so I did not have an option to make the corners transparent. I would like to make the application have rounded corners, but I am not getting the expect results with XAML. I have tried adding a border to get the desired effect but when I run the application, the image is still in front of the border. Here is my code. What am I doing wrong?

<Border BorderBrush="Black" CornerRadius="15,15,15,15" BorderThickness="1,1,1,1" Width="242" Height="426">
    <Grid>
        <Image Height="425" Name="image1" Stretch="Fill" Width="240" Source="/Test;component/Test.png" />
        <Grid Height="334" HorizontalAlignment="Left" Margin="24,39,0,0" Name="grid1" VerticalAlignment="Top" Width="198" />
    </Grid>
</Border>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

偷得浮生 2024-10-24 23:31:13

窗口中的这些设置将使其透明:

WindowStyle="None"
AllowsTransparency="True"
Background="Transparent"

然后将边框的背景设置为图像:

  <Border BorderBrush="Transparent" BorderThickness="1" CornerRadius="15">
            <Border.Background>
                <ImageBrush>
                    <ImageBrush.ImageSource>
                <BitmapImage UriSource="/Test;component/Test.png" />
                    </ImageBrush.ImageSource>
                </ImageBrush>
            </Border.Background>
        </Border>

these settings in the window will make it transparent:

WindowStyle="None"
AllowsTransparency="True"
Background="Transparent"

and then just set the background of the Border to be the image:

  <Border BorderBrush="Transparent" BorderThickness="1" CornerRadius="15">
            <Border.Background>
                <ImageBrush>
                    <ImageBrush.ImageSource>
                <BitmapImage UriSource="/Test;component/Test.png" />
                    </ImageBrush.ImageSource>
                </ImageBrush>
            </Border.Background>
        </Border>
◇流星雨 2024-10-24 23:31:13

我认为您需要将 ClipToBounds="True" 添加到图像中。

I think you need to add ClipToBounds="True" to the image.

生来就爱笑 2024-10-24 23:31:13

这是一个特殊的边框,ClipToBounds 可以完美地工作。如果您将埃雷兹的答案中的“Border”更改为“ClippingBorder”,那么它应该可以工作。

''' <Remarks>
'''     As a side effect ClippingBorder will surpress any databinding or animation of 
'''         its childs UIElement.Clip property until the child is removed from ClippingBorder
''' </Remarks>

Public Class ClippingBorder
Inherits Border
Protected Overrides Sub OnRender(ByVal dc As DrawingContext)
    OnApplyChildClip()
    MyBase.OnRender(dc)
End Sub

Public Overrides Property Child() As UIElement
    Get
        Return MyBase.Child
    End Get
    Set(ByVal value As UIElement)
        If Me.Child IsNot value Then
            If Me.Child IsNot Nothing Then
                ' Restore original clipping
                Me.Child.SetValue(UIElement.ClipProperty, _oldClip)
            End If

            If value IsNot Nothing Then
                _oldClip = value.ReadLocalValue(UIElement.ClipProperty)
            Else
                ' If we dont set it to null we could leak a Geometry object
                _oldClip = Nothing
            End If
            MyBase.Child = value
        End If
    End Set
End Property

Protected Overridable Sub OnApplyChildClip()
    Dim _child As UIElement = Me.Child
    If _child IsNot Nothing Then
        _clipRect.RadiusX = InlineAssignHelper(_clipRect.RadiusY, Math.Max(0.0, Me.CornerRadius.TopLeft - (Me.BorderThickness.Left * 0.5)))
        _clipRect.Rect = New Rect(_child.RenderSize)
        _child.Clip = _clipRect
    End If
End Sub

Private _clipRect As New RectangleGeometry()
Private _oldClip As Object
Private Shared Function InlineAssignHelper(Of T)(ByRef target As T, ByVal value As T) As T
    target = value
    Return value
End Function

End Class

您可以使用 http://www.developerfusion.com/tools/convert/ vb-to-csharp/ 如果您更喜欢 C#。

Here is a special border where ClipToBounds works perfectly. If you change in Erez's answer the "Border" to "ClippingBorder" then it should work.

''' <Remarks>
'''     As a side effect ClippingBorder will surpress any databinding or animation of 
'''         its childs UIElement.Clip property until the child is removed from ClippingBorder
''' </Remarks>

Public Class ClippingBorder
Inherits Border
Protected Overrides Sub OnRender(ByVal dc As DrawingContext)
    OnApplyChildClip()
    MyBase.OnRender(dc)
End Sub

Public Overrides Property Child() As UIElement
    Get
        Return MyBase.Child
    End Get
    Set(ByVal value As UIElement)
        If Me.Child IsNot value Then
            If Me.Child IsNot Nothing Then
                ' Restore original clipping
                Me.Child.SetValue(UIElement.ClipProperty, _oldClip)
            End If

            If value IsNot Nothing Then
                _oldClip = value.ReadLocalValue(UIElement.ClipProperty)
            Else
                ' If we dont set it to null we could leak a Geometry object
                _oldClip = Nothing
            End If
            MyBase.Child = value
        End If
    End Set
End Property

Protected Overridable Sub OnApplyChildClip()
    Dim _child As UIElement = Me.Child
    If _child IsNot Nothing Then
        _clipRect.RadiusX = InlineAssignHelper(_clipRect.RadiusY, Math.Max(0.0, Me.CornerRadius.TopLeft - (Me.BorderThickness.Left * 0.5)))
        _clipRect.Rect = New Rect(_child.RenderSize)
        _child.Clip = _clipRect
    End If
End Sub

Private _clipRect As New RectangleGeometry()
Private _oldClip As Object
Private Shared Function InlineAssignHelper(Of T)(ByRef target As T, ByVal value As T) As T
    target = value
    Return value
End Function

End Class

And you can use http://www.developerfusion.com/tools/convert/vb-to-csharp/ if you like C# better.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文