使用WPF创建自定义按钮形状,带有倒角

huangapple go评论77阅读模式
英文:

Creating a custom button shape with inverted corner WPF

问题

我试图在WPF中创建一个自定义按钮,其形状如下所示:

使用WPF创建自定义按钮形状,带有倒角

但是我在底部右侧的倒角上遇到了困难。我已经看到了各种组合矩形和椭圆形状的示例,或者使用自定义路径,但是我还没有看到如何将其转化为按钮的任何示例。

英文:

I am trying to create a custom button in WPF in this shape:

使用WPF创建自定义按钮形状,带有倒角

But am struggling on the inverted CornerRadius in the bottom right. I have seen various examples of combining Rectangle and Ellipse shapes, or by using a custom path, however I haven't seen any examples of how to then turn that into a button.

答案1

得分: 1

将您的设计转化为按钮很简单,只需将其设置为按钮的内容。使用 CombinedGeometry 类,您可以制作各种不同的按钮形状(请参阅 如何:创建组合几何图形)。以下是使用 CombinedGeometry 实现您的设计的示例:

<Button>
    <Grid>
        <Path Fill="White"
              Width="160"
              Height="160">
            <Path.Data>

                <CombinedGeometry GeometryCombineMode="Union">
                    <CombinedGeometry.Geometry1>
                        <CombinedGeometry GeometryCombineMode="Exclude">
                            <CombinedGeometry.Geometry1>
                                <CombinedGeometry GeometryCombineMode="Exclude">
                                    <CombinedGeometry.Geometry1>
                                        <RectangleGeometry Rect="80,0,80,160" />
                                    </CombinedGeometry.Geometry1>
                                    <CombinedGeometry.Geometry2>
                                        <EllipseGeometry RadiusX="40"
                                                            RadiusY="40"
                                                            Center="120,160" />
                                    </CombinedGeometry.Geometry2>
                                </CombinedGeometry>
                            </CombinedGeometry.Geometry1>
                            <CombinedGeometry.Geometry2>
                                <RectangleGeometry Rect="0,120,120,40" />
                            </CombinedGeometry.Geometry2>
                        </CombinedGeometry>
                    </CombinedGeometry.Geometry1>

                    <CombinedGeometry.Geometry2>
                        <CombinedGeometry GeometryCombineMode="Union">
                            <CombinedGeometry.Geometry1>
                                <CombinedGeometry GeometryCombineMode="Union">
                                    <CombinedGeometry.Geometry1>
                                        <RectangleGeometry Rect="0,0,80,80" />
                                    </CombinedGeometry.Geometry1>
                                    <CombinedGeometry.Geometry2>
                                        <EllipseGeometry RadiusX="40"
                                                            RadiusY="40"
                                                            Center="40,80" />
                                    </CombinedGeometry.Geometry2>
                                </CombinedGeometry>
                            </CombinedGeometry.Geometry1>
                            <CombinedGeometry.Geometry2>
                                <RectangleGeometry Rect="40,0,40,120" />
                            </CombinedGeometry.Geometry2>
                        </CombinedGeometry>
                    </CombinedGeometry.Geometry2>
                </CombinedGeometry>

            </Path.Data>
        </Path>

        <TextBlock Text="自定义按钮"
                    HorizontalAlignment="Center"
                    VerticalAlignment="Center"
                    Foreground="Black" />
    </Grid>
</Button>

这可以通过使用更少的几何元素来改进,但它为您提供了一个工作模板。

英文:

Turning your design into a button is simple, you make it the Content of your Button. Using the CombinedGeometry Class, sky's the limit to what you can make (see How to: Create a Combined Geometry). Here's an implementation of your design using CombinedGeometry:

&lt;Button&gt;
&lt;Grid&gt;
&lt;Path Fill=&quot;White&quot;
Width=&quot;160&quot;
Height=&quot;160&quot;&gt;
&lt;Path.Data&gt;
&lt;CombinedGeometry GeometryCombineMode=&quot;Union&quot;&gt;
&lt;CombinedGeometry.Geometry1&gt;
&lt;CombinedGeometry GeometryCombineMode=&quot;Exclude&quot;&gt;
&lt;CombinedGeometry.Geometry1&gt;
&lt;CombinedGeometry GeometryCombineMode=&quot;Exclude&quot;&gt;
&lt;CombinedGeometry.Geometry1&gt;
&lt;RectangleGeometry Rect=&quot;80,0,80,160&quot; /&gt;
&lt;/CombinedGeometry.Geometry1&gt;
&lt;CombinedGeometry.Geometry2&gt;
&lt;EllipseGeometry RadiusX=&quot;40&quot;
RadiusY=&quot;40&quot;
Center=&quot;120,160&quot; /&gt;
&lt;/CombinedGeometry.Geometry2&gt;
&lt;/CombinedGeometry&gt;
&lt;/CombinedGeometry.Geometry1&gt;
&lt;CombinedGeometry.Geometry2&gt;
&lt;RectangleGeometry Rect=&quot;0,120,120,40&quot; /&gt;
&lt;/CombinedGeometry.Geometry2&gt;
&lt;/CombinedGeometry&gt;
&lt;/CombinedGeometry.Geometry1&gt;
&lt;CombinedGeometry.Geometry2&gt;
&lt;CombinedGeometry GeometryCombineMode=&quot;Union&quot;&gt;
&lt;CombinedGeometry.Geometry1&gt;
&lt;CombinedGeometry GeometryCombineMode=&quot;Union&quot;&gt;
&lt;CombinedGeometry.Geometry1&gt;
&lt;RectangleGeometry Rect=&quot;0,0,80,80&quot; /&gt;
&lt;/CombinedGeometry.Geometry1&gt;
&lt;CombinedGeometry.Geometry2&gt;
&lt;EllipseGeometry RadiusX=&quot;40&quot;
RadiusY=&quot;40&quot;
Center=&quot;40,80&quot; /&gt;
&lt;/CombinedGeometry.Geometry2&gt;
&lt;/CombinedGeometry&gt;
&lt;/CombinedGeometry.Geometry1&gt;
&lt;CombinedGeometry.Geometry2&gt;
&lt;RectangleGeometry Rect=&quot;40,0,40,120&quot; /&gt;
&lt;/CombinedGeometry.Geometry2&gt;
&lt;/CombinedGeometry&gt;
&lt;/CombinedGeometry.Geometry2&gt;
&lt;/CombinedGeometry&gt;
&lt;/Path.Data&gt;
&lt;/Path&gt;
&lt;TextBlock Text=&quot;Custom Button&quot;
HorizontalAlignment=&quot;Center&quot;
VerticalAlignment=&quot;Center&quot;
Foreground=&quot;Black&quot; /&gt;
&lt;/Grid&gt;
&lt;/Button&gt;

This could be improved by using less geometry but it gives you a template to work with.

huangapple
  • 本文由 发表于 2023年5月29日 04:39:31
  • 转载请务必保留本文链接:https://go.coder-hub.com/76353506.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定