WPF 重写按钮变成音乐播放器按钮
2016-08-02 20:51
302 查看
在使用天天动听音乐播放器的时候,为什么别人的播放暂停按钮可以做的那么漂亮,于是想用WPF来实现下,WPF中可以重写按钮的样式,在winform中就不知道怎么搞了(可能可以来个usercontrol画个圆画两个横线设置些事件然后…)。废话不多说,直接上代码:
1.这里用到了资源字典,先建立一个资源文件,名字为ButtonStyle.xaml,代码如下:
说明下上述代码:建立两个button样式,名字为buttonPause何buttonPlay,都设置了当鼠标移动到按钮上去了之后按钮背景设改变的触发器。原理就是先画个圆和Canvas,然后在Canvas上画两条直线或者三角形,将圆的背景设置成按钮的背景效果如下所示:
鼠标没放上去的播放按钮状态
鼠标放上去的播放按钮状态
鼠标没放上去的暂停按钮状态
鼠标放上去的暂停按钮状态
2.使用资源文件,使用之前要在APP.XAML中加入:
加入之后就可以直接在MainWindow中使用了:
在后台 代码中点击按钮切换样式:
1.这里用到了资源字典,先建立一个资源文件,名字为ButtonStyle.xaml,代码如下:
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"> <Style TargetType="Button" x:Key="buttonPause"> <Setter Property="Background" Value="White"></Setter> <Setter Property="Template"> <Setter.Value> <ControlTemplate TargetType="Button"> <Grid> <Ellipse Height="80" Width="80" Fill="{TemplateBinding Background}" Stroke="Yellow" StrokeThickness="2"> </Ellipse> <Canvas Height="80" Width="80" Opacity="1"> <Line X1="25" Y1="20" X2="25" Y2="60" Stroke="Red" StrokeThickness="5"></Line> <Line X1="55" Y1="15" X2="55" Y2="65" Stroke="Red" StrokeThickness="5"></Line> </Canvas> </Grid> <ControlTemplate.Triggers> <Trigger Property="IsMouseOver" Value="true"> <Setter Property="Background" Value="#FF507FE6"></Setter> </Trigger> <Trigger Property="IsMouseOver" Value="false"> <Setter Property="Background" Value="White"></Setter> </Trigger> </ControlTemplate.Triggers> </ControlTemplate> </Setter.Value> </Setter> </Style> <Style TargetType="Button" x:Key="buttonPlay"> <Setter Property="Background" Value="White"></Setter> <Setter Property="Template"> <Setter.Value> <ControlTemplate TargetType="Button"> <Grid> <Ellipse Height="80" Width="80" Fill="{TemplateBinding Background}" Stroke="Yellow" StrokeThickness="2"> </Ellipse> <Canvas Height="80" Width="80" Opacity="1"> <Line X1="25" Y1="20" X2="25" Y2="60" Stroke="Red" StrokeThickness="3"></Line> <Line X1="25" Y1="20" X2="60" Y2="40" Stroke="Red" StrokeThickness="3"></Line> <Line X1="25" Y1="60" X2="60" Y2="40" Stroke="Red" StrokeThickness="3"></Line> </Canvas> </Grid> <ControlTemplate.Triggers> <Trigger Property="IsMouseOver" Value="true"> <Setter Property="Background" Value="#FF507FE6"></Setter> </Trigger> <Trigger Property="IsMouseOver" Value="false"> <Setter Property="Background" Value="White"></Setter> </Trigger> </ControlTemplate.Triggers> </ControlTemplate> </Setter.Value> </Setter> </Style> </ResourceDictionary>
说明下上述代码:建立两个button样式,名字为buttonPause何buttonPlay,都设置了当鼠标移动到按钮上去了之后按钮背景设改变的触发器。原理就是先画个圆和Canvas,然后在Canvas上画两条直线或者三角形,将圆的背景设置成按钮的背景效果如下所示:
鼠标没放上去的播放按钮状态
鼠标放上去的播放按钮状态
鼠标没放上去的暂停按钮状态
鼠标放上去的暂停按钮状态
2.使用资源文件,使用之前要在APP.XAML中加入:
<Application x:Class="重写button.App" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" StartupUri="MainWindow.xaml"> <Application.Resources> <ResourceDictionary> <ResourceDictionary.MergedDictionaries> <ResourceDictionary Source="ButtonStyle.xaml" ></ResourceDictionary> </ResourceDictionary.MergedDictionaries> </ResourceDictionary> </Application.Resources> </Application>
加入之后就可以直接在MainWindow中使用了:
<Grid> <Button Name="btn" Click="btn_Click" Style="{StaticResource buttonPlay}" ToolTip="播放"> </Button> </Grid>
在后台 代码中点击按钮切换样式:
private void btn_Click(object sender, RoutedEventArgs e) { Button b = (Button)sender; string toolTip = b.ToolTip.ToString(); if (toolTip == "播放") { b.SetValue(Button.StyleProperty, Application.Current.Resources["buttonPause"]); b.ToolTip = "暂停"; } else { b.SetValue(Button.StyleProperty,Application.Current.Resources["buttonPlay"]); b.ToolTip = "播放"; } }
相关文章推荐
- wpf中把按钮变成圆角
- 重写按钮的方法,改变其图片的大小
- WPF实用指南一:在WPF窗体的边框中添加搜索框和按钮
- WPF中隐藏窗口最小化、最大化和关闭按钮
- WPF后台自定义文字带背景的选择状态按钮
- WPF中让窗体不显示最大化,最小化,关闭按钮
- 禁用WPF窗体的最大化按钮
- WPF重写Button样式
- WPF 按钮样式
- 【WPF】TextBox样式重写注意事项
- 利用Custom Ribbon XML 重写Office内置按钮
- C# WPF button样式清空以及关闭 最小化 按钮重置
- 从SVN上面Checkout出项目不可运行,Run按钮变成灰色
- WPF程序员自定义控库系列(三)——冷却按钮
- WPF PopupNonTopmost重写
- [WPF疑难]如何禁用窗口上的关闭按钮
- C# 重写 winform 关闭按钮
- WPF利用代码触发按钮点击操作
- IOS——重写按钮(开关功能)
- (转)WPF中让窗体不显示最大化,最小化,关闭按钮