日期:2014-05-18  浏览次数:21033 次

如何用wpf实现不规则按钮?
如题,我想用wpf实现不规则按钮,根据png图片的形状来决定按钮的形状,图片以外的区域鼠标点击无效。
请高手帮帮忙!

------解决方案--------------------
用blend工具,做起来就非常容易,代码都不用写
------解决方案--------------------
把图像转成path就可以了。。。
XML code
        <Style x:Key="ButtonStyle1" TargetType="{x:Type Button}">
            <Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate TargetType="{x:Type Button}">
                        <Grid x:Name="grid" RenderTransformOrigin="0.5,0.5">
                            <Grid.RenderTransform>
                                <TransformGroup>
                                    <ScaleTransform/>
                                    <SkewTransform/>
                                    <RotateTransform/>
                                    <TranslateTransform/>
                                </TransformGroup>
                            </Grid.RenderTransform>
                            <Path x:Name="形状_1" Data="F1 M 37.9719,53.7023C 38.1659,56.8911 38.7423,60.258 40.0892,62.5621C 41.442,65.0446 43.366,66.1075 46.2597,67.8793L 25.636,98.1879C 16.765,93.4041 10.0179,87.3757 5.58875,79.5789C 0.958984,71.603 -0.776428,63.095 0,53.7024L 37.9719,53.7023 Z M 64.572,38.4599C 61.4838,37.0403 58.0189,35.9774 55.1245,35.9774C 52.2362,35.9775 50.3068,37.0403 47.2245,38.4599L 27.7537,7.62128C 36.4301,2.65863 45.489,6.10352e-005 55.1245,6.10352e-005C 64.7599,6.10352e-005 73.8249,2.65863 82.4958,7.62128L 64.572,38.4599 Z M 55.895,44.6616C 61.1013,44.6616 65.1486,48.3853 65.1486,53.1696C 65.1484,57.9568 61.1013,61.6777 55.895,61.6777C 50.6894,61.6777 46.6421,57.9568 46.6421,53.1697C 46.6421,48.3853 50.6894,44.6616 55.895,44.6616 Z M 111.796,53.7024C 112.567,63.095 110.832,71.603 106.202,79.5789C 101.773,87.3758 95.0253,93.4041 86.1608,98.1879L 65.5311,67.8793C 68.4248,66.1075 70.3542,65.0446 71.7017,62.5622C 73.0486,60.2581 73.631,56.8911 73.8251,53.7025L 111.796,53.7024 Z " Fill="Red" Stretch="Fill" Stroke="#FFFDFF00">
                                <Path.Effect>
                                    <DropShadowEffect ShadowDepth="0" BlurRadius="12"/>
                                </Path.Effect>
                            </Path>
                            <ContentPresenter x:Name="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="grid">
                                    <Setter.Value>
                                        <TransformGroup>
                                            <ScaleTransform ScaleX="1.05" ScaleY="1.05"/>
                                            <SkewTransform/>
                                            <RotateTransform/>
                                            <TranslateTransform/>
                                        </TransformGroup>
                                    </Setter.Value>
                                &