如何在WPF中创建一个圆上有孔的圆?

发布于 2024-10-14 08:39:40 字数 1785 浏览 2 评论 0原文

我创建了一个用户控件,它是一个由两个圆圈叠加而成的环,其中小圆圈是空白的,最小圆圈后面的第二个圆圈是彩色的。

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

我尝试使用 OpacityMask 作为小椭圆,正如 http://social.msdn.microsoft.com/forums/en-US/wpf/thread/551201d1-c5b3-4e17-ae63-625cfbb8bcc4 但仍然看不到孔后面的环:

<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>

< img src="https://i.sstatic.net/D2jYY.png" alt="在此处输入图像描述">

I have created a UserControl which is a ring by superposing 2 circles the small circle being blank and the second behind the smallest being colored.

In my WPF app, I want to put several rings but the small circle does hide other rings. I'd like to see through it and also capture mouse event for ring behind other rings otherwise it's not real rings. Is it possible ?

I tried OpacityMask for small ellipse as pointed by answer to http://social.msdn.microsoft.com/forums/en-US/wpf/thread/551201d1-c5b3-4e17-ae63-625cfbb8bcc4 but still can't see ring behind hole:

<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>

enter image description here

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

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

发布评论

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

评论(3

離殇 2024-10-21 08:39:40

看起来您已经找到了答案(几年前),但对于其他想要这样做的人,您可能需要查看 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” 应该在需要时防止鼠标干扰。

It looks like you already found your answer (a few years ago), but for anyone else looking to do this, you may want to check out CompositeGeometry:

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

Such as:

<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>

Then IsHitTestVisible="False" should prevent mouse interference, when needed.

牵强ㄟ 2024-10-21 08:39:40

您可以在 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>

You could just create a circle with a transparent background and a StrokeThickness in the UserControl.

<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>

EDIT:
You can set a gradient brush to the stroke as below. You could replace the LinearGradientBrush with any other type of brush as you wish.

<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>
花期渐远 2024-10-21 08:39:40

查看如何创建中心透明的甜甜圈?

其下带有路径和按钮的代码:

<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>

Check out this How to create a doughnut with a transparent center?.

Code with path and button under it:

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