圆角边形式的AllowTransparency替代方案

时间:2012-01-09 15:14:15

标签: c# .net wpf user-interface

我有一个WPF项目,主窗口需要有圆角。我现在可以通过设置AllowTransparency = True来做到这一点。这样圆角附近的白色背景变得透明。

然而,AllowTransparency很慢而且有错误。特别是有known问题,其中AllowTransparency严重破坏,MS拒绝修复它 - 它会影响我的客户。此外,MS建议修补程序最多不一致,所以这也不是一个选项。

似乎我有两个选择:1)简单地关闭AllowTransparency并有一个可怜的丑陋形式或2)找到一个没有AllowTransparency的圆角边缘的解决方法。

StackO是否可以在WPF项目中使用AllowTransparency = False的圆边?感谢。

3 个答案:

答案 0 :(得分:3)

也许这会有帮助吗? http://www.kirupa.com/blend_wpf/custom_wpf_windows.htm(仍然使用AllowTransparency) 或者这个:http://www.codeproject.com/KB/WPF/CustomWPFWindow.aspx

答案 1 :(得分:1)

前段时间我写了一个图书馆:http://archive.msdn.microsoft.com/WPFShell

它公开Window的WindowChrome附加属性,摆脱标准窗口镶边,然后如果您指定不需要玻璃框架,那么您可以指定一个CornerRadius属性来舍入您想要的任何角落。下载中包含一个示例项目,显示了不同属性的工作方式。

该库是通过处理WM_NCCALCSIZE(类似于Office自定义chrome的方法)实现的,而不是使用分层窗口,这就是Window.AllowsTransparency的完成方式。

链接是一个稍微旧版本的库,但我认为从那时起修复的大多数错误都不会影响你,因为它听起来好像你没有使用玻璃。

答案 2 :(得分:0)

如果您只想剪切圆角,可以在窗体中覆盖OnPaint,并创建一个具有所需窗口形状的路径(System.Drawing.Drawing2D.GraphicsPath),并将该路径指定给窗体的Region属性。这有点笨重,但它可能会做你想要的。例如,对于均匀弯曲的角落,你可以做(​​vb,而不是C#,对不起):

Protected Overrides Sub OnPaint(ByVal e As System.Windows.Forms.PaintEventArgs)
        MyBase.OnPaint(e)
        Dim r As Rectangle = Me.ClientRectangle
        Dim w As Integer = 50 'width of curvature
        Dim h As Integer = 50 'heigth of curvature

        Dim gp As New System.Drawing.Drawing2D.GraphicsPath
        gp.StartFigure()
        gp.AddArc(r.Right - w, r.Top, w, h, 270, 90)
        gp.AddArc(r.Right - w, r.Bottom - h, w, h, 0, 90)
        gp.AddArc(r.Left, r.Bottom - h, w, h, 90, 90)
        gp.AddArc(r.Left, r.Top, w, h, 180, 90)
        gp.CloseFigure()

        e.Graphics.DrawPath(Pens.Black, gp)

        Me.Region = New System.Drawing.Region(gp)

    End Sub