【问题标题】:How to create a circle with a hole in a circle in WPF?如何在 WPF 中创建一个带孔的圆?
【发布时间】:2013-08-20 06:39:19
【问题描述】:

我创建了一个用户控件,它是一个环,通过叠加 2 个圆圈,小圆圈为空白,第二个圆圈在最小的圆圈后面是彩色的。

在我的 WPF 应用程序中,我想放置几个环,但小圆圈确实隐藏了其他环。我想看穿它,并在其他戒指后面捕捉鼠标事件,否则它不是真正的戒指。有可能吗?

http://social.msdn.microsoft.com/forums/en-US/wpf/thread/551201d1-c5b3-4e17-ae63-625cfbb8bcc4 的回答所指出的,我为小椭圆尝试了 OpacityMask,但仍然看不到孔后面的环:

<UserControl x:Class="MyUserControls.MyRing"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
             mc:Ignorable="d" 
             d:DesignHeight="150" d:DesignWidth="150" SizeChanged="UserControl_SizeChanged">
    <Grid Height="150" Name="Grid" Width="150" MouseMove="ellipse1_MouseMove">
        <Ellipse Fill="Red" Height="150" Width="150" HorizontalAlignment="Left" Margin="0,0,0,0" Name="ellipse1" Stroke="Black" VerticalAlignment="Top"  >
            <Ellipse.OpacityMask>
                <RadialGradientBrush>
                    <GradientStop Color="#FFB94444" Offset="0.496"/>
                    <GradientStop Color="#00FFFFFF" Offset="0.491"/>
                </RadialGradientBrush>
            </Ellipse.OpacityMask>
        </Ellipse>
        <Ellipse Fill="White" Height="100" Width="100" Margin="25,25,25,0" Name="ellipse2" Stroke="Black" VerticalAlignment="Top" />       
    </Grid>
</UserControl>

【问题讨论】:

    标签: wpf user-controls wpf-controls


    【解决方案1】:

    看起来您已经找到了答案(几年前),但对于其他想要这样做的人,您可能需要查看 CompositeGeometry:

    http://msdn.microsoft.com/en-us/library/ms751808.aspx#combindgeometriessection

    如:

    <Path Fill="Red" Stroke="Black">
        <Path.Data>
            <CombinedGeometry GeometryCombineMode="Xor">
                <CombinedGeometry.Geometry1>
                    <EllipseGeometry RadiusX="75" RadiusY="75" Center="75,75" />
                </CombinedGeometry.Geometry1>
                <CombinedGeometry.Geometry2>
                    <EllipseGeometry RadiusX="50" RadiusY="50" Center="75,75" />
                </CombinedGeometry.Geometry2>
            </CombinedGeometry>
        </Path.Data>
    </Path>
    

    那么IsHitTestVisible="False" 应该在需要时防止鼠标干扰。

    【讨论】:

    • 是否可以在其父路径中拉伸和居中?
    • 这是一个不错的答案。
    【解决方案2】:

    您可以在 UserControl 中创建一个具有透明背景和 StrokeThickness 的圆圈。

    <UserControl x:Class="WpfApplication1.UserControl1"
                 xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                 xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
                 xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
                 xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
                 mc:Ignorable="d" 
                 d:DesignHeight="300" d:DesignWidth="300">
        <Grid>
            <Ellipse Width="50" Height="50" Stroke="Blue" StrokeThickness="10" Fill="Transparent"></Ellipse>
        </Grid>
    </UserControl>
    

    编辑: 您可以为描边设置渐变画笔,如下所示。您可以根据需要将LinearGradientBrush 替换为任何其他类型的画笔。

    <Ellipse Width="50" Height="50" StrokeThickness="10" Fill="Transparent">
        <Ellipse.Stroke>
            <LinearGradientBrush>
                <GradientStop Offset="0" Color="Red"/>
                <GradientStop Offset="1" Color="Green"/>
            </LinearGradientBrush>
        </Ellipse.Stroke>
    </Ellipse>
    

    【讨论】:

    • 我试过问题是它不再是一个环,因为我可以看到后面的大圆圈:) 我想要第二个圆圈对其应用一些渐变,所以大边框不能做到这一点我可以预见。
    【解决方案3】:

    看看这个How to create a doughnut with a transparent center?

    下面有路径和按钮的代码:

    <Grid x:Name="LayoutRoot" Width="109" Height="109">
    
        <Button Content="Below" Width="100" Height="100" />
    
        <Path Fill="#FF1F96D8" Stroke="#FF000000"
            Width="109" HorizontalAlignment="Left" Stretch="None"
            Data="M54.5,32.5 C41.245167,32.5 30.5,43.021309 30.5,56         
              30.5,68.978691 41.245167,79.5 54.5,79.5 C67.754837,79.5         
              78.5,68.978691 78.5,56 C78.5,43.021309 67.754837,32.5        
              54.5,32.5 z M54.5,0.5 C84.32338,0.5 108.5,24.676624         
              108.5,54.5 C108.5,84.32338 84.32338,108.5 54.5,108.5         
              24.676624,108.5 0.5,84.32338 0.5,54.5 0.5,24.676624         
              24.676624,0.5 54.5,0.5 z" Height="109" />
    
    
    </Grid>
    

    【讨论】:

    • 究竟是什么不工作我试过用它下面的按钮运行发布的代码(路径示例)。
    • 好的,答案很好,但是有没有办法使用圆而不是路径,因为我需要使用路径自动调整大小并不容易。所以将尝试下面的其他解决方案。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-02-10
    • 2015-08-08
    • 1970-01-01
    • 2010-09-12
    相关资源
    最近更新 更多