WPF复杂形状按钮

Posted 天王星天

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了WPF复杂形状按钮相关的知识,希望对你有一定的参考价值。

技术分享

 

方法很简单,将图片转换为<path>就可以了(需要用到Photoshop)

不过一般情况下制作按钮都不会用到这种方法,通常只要用image填充一张图片或者把路径转成按钮控件就可以了。

之所以这样做,是见到一位坛友提问说:如何根据PNG图片的形状来决定按钮的形状,大概意思可能就是只有在PNG

图像形状范围内触发器才有效,不过他说的我也没怎么听明白,呵呵。。。自己试着做了一下。目前就想到了这个方法,也许还有更好的方法!

 

点击打开下载链接

 

技术分享
Style x:Key="ButtonStyle1" TargetType="{x:Type Button}">
            <Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate TargetType="{x:Type Button}">
                        <Grid>
                            <Path x:Name="形状_1" Data="M 32.9626,1.52588e-005C 64.8366,-1.05113 86.9761,13.3906 90.2887,40.0549C 121.366,22.922 167.447,36.5059 190.609,52.9298C 196.819,60.0817 203.031,67.2358 209.24,74.3877C 218.587,80.7901 229.685,81.6765 236.47,90.1236C 238.66,92.8495 248.286,130.997 249.369,135.901C 258.248,176.097 225.629,230.492 199.208,228.885C 210.609,245.719 247.47,243.846 260.834,260.357C 258.446,264.648 256.057,268.941 253.668,273.232C 254.624,273.232 255.579,273.232 256.535,273.232C 266.144,278.475 277.516,274.548 286.631,271.801C 286.631,278 286.631,284.2 286.631,290.398C 270.758,292.489 258.952,283.729 243.636,286.107C 231.045,288.061 212.93,298.373 196.342,291.829C 187.923,288.507 176.727,274.685 167.679,268.94C 169.112,257.974 170.545,247.004 171.978,236.038C 171.501,236.038 171.023,236.038 170.545,236.038C 158.23,240.139 147.846,241.445 150.481,257.496C 153.347,257.973 156.214,258.45 159.08,258.927C 157.574,270.027 155.171,292.206 163.38,301.843C 167.201,303.75 171.023,305.658 174.845,307.565C 178.188,315.194 181.533,322.825 184.877,330.453C 196.377,340.356 212.387,340.294 222.139,351.911C 148.1,351.911 74.0389,351.911 0,351.911C 2.38837,349.05 4.77742,346.189 7.16574,343.328C 26.8224,334.338 23.7874,310.676 37.262,296.12C 45.8601,298.028 54.4607,299.935 63.0588,301.843C 63.5364,300.412 64.0143,298.981 64.4919,297.551C 62.1036,285.154 59.7145,272.754 57.3262,260.357C 56.8485,260.357 56.3707,260.357 55.893,260.357C 55.893,267.986 55.893,275.617 55.893,283.246C 55.4153,283.246 54.9375,283.246 54.4599,283.246C 54.4599,282.292 54.4599,281.338 54.4599,280.385C 48.716,268.591 52.2378,254.85 53.0267,240.33C 42.9823,238.522 30.6873,236.171 25.7968,228.885C 20.3403,219.725 16.4283,166.867 14.3315,155.928C 25.3332,155.55 33.2694,151.907 37.262,144.484C 42.9325,134.876 41.5233,110.573 44.4278,98.7068C 43.4724,99.1836 42.5168,99.6605 41.5615,100.137C 36.665,114.747 29.8112,126.853 25.7968,144.484C 25.3191,144.484 24.8413,144.484 24.3636,144.484C 20.4211,118.72 25.9583,98.0166 34.3957,81.5404C 21.5746,91.6128 18.1252,106.513 10.0321,121.595C 9.55441,121.595 9.0766,121.595 8.59889,121.595C 8.59889,121.119 8.59889,120.642 8.59889,120.165C 18.9555,78.4881 36.1168,33.5594 88.8555,41.4855C 82.3319,4.01784 40.9382,1.40187 8.59889,12.8748C 9.55426,11.4444 10.5099,10.0137 11.4652,8.58327C 18.6303,5.72243 25.7975,2.86086 32.9626,1.52588e-005 Z " Stretch="Fill" RenderTransformOrigin="0.5,0.5">
                                <Path.RenderTransform>
                                    <TransformGroup>
                                        <ScaleTransform/>
                                        <SkewTransform/>
                                        <RotateTransform/>
                                        <TranslateTransform/>
                                    </TransformGroup>
                                </Path.RenderTransform>
                                <Path.Effect>
                                    <DropShadowEffect ShadowDepth="0" BlurRadius="22" Color="#FF646464"/>
                                </Path.Effect>
                                <Path.Fill>
                                    <ImageBrush ImageSource="图层 1.png"/>
                                </Path.Fill>
                            </Path>
                            <ContentPresenter HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}" RecognizesAccessKey="True" SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}" VerticalAlignment="{TemplateBinding VerticalContentAlignment}" Content=""/>
                        </Grid>
                        <ControlTemplate.Triggers>
                            <Trigger Property="IsFocused" Value="True"/>
                            <Trigger Property="IsDefaulted" Value="True"/>
                            <Trigger Property="IsMouseOver" Value="True">
                                <Setter Property="RenderTransform" TargetName="形状_1">
                                    <Setter.Value>
                                        <TransformGroup>
                                            <ScaleTransform ScaleX="1.01" ScaleY="1.01"/>
                                            <SkewTransform/>
                                            <RotateTransform/>
                                            <TranslateTransform/>
                                        </TransformGroup>
                                    </Setter.Value>
                                </Setter>
                            </Trigger>
                            <Trigger Property="IsPressed" Value="True">
                                <Setter Property="Effect" TargetName="形状_1">
                                    <Setter.Value>
                                        <DropShadowEffect BlurRadius="22" Color="Red" ShadowDepth="0"/>
                                    </Setter.Value>
                                </Setter>
                            </Trigger>
                            <Trigger Property="IsEnabled" Value="False"/>
                        </ControlTemplate.Triggers>
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
        </Style>
View Code

 

转载:http://blog.csdn.net/cmis7645/article/details/7592372

以上是关于WPF复杂形状按钮的主要内容,如果未能解决你的问题,请参考以下文章

HTML代码片段

WPF学习第三十九章 理解形状

wpf C#中线条起点和终点的特殊形状

WPF学习第四十三章 路径和几何图形

如何在 WPF 中停止方法(通过按钮单击触发)

如何使用 WPF 中的 TextShape 类绘制自定义形状?