WPF利用Image实现图片按钮
之前有一篇文章也是采用了Image实现的图片按钮,不过时间太久远了,忘记了地址。好吧,这里我进行了进一步的改进,原来的文章中需要设置4张图片,分别为可用时,鼠标悬浮时,按钮按下时,按钮不可用时的图片,这里我只用了一张图片,利用C#的图片灰度处理自动获得不可用时的图片,利用图片的间距实现悬浮及按下效果。先上效果:(正常 悬浮 按下 不可用)

代码其实比较简单,唯一的难点就是把图片转换成ImageSource,在网上找了很久终于找到了一个,转换代码如下:
///<summary>
///设置正常及不可用时的背景图片
///</summary>
///<param name="i">背景图片</param>
private void getBackImageSource(BitmapSource i)
{
if (i == null) {
EnablebackImage = null;
unEnablebackImage = null;
return;
}
FormatConvertedBitmap b = new FormatConvertedBitmap();
b.BeginInit();
b.Source = i;
b.DestinationFormat = PixelFormats.Gray8;
b.EndInit();
FormatConvertedBitmap b1 = new FormatConvertedBitmap();
b1.BeginInit();
b1.Source = i;
b1.EndInit();
EnablebackImage = b1;
unEnablebackImage = b;
}
前端采用Image作为主体部分,利用Border模仿按钮的边框,TextBlock作为文本显示。注意:代码主体部分利用ViewBox保证控件大小变化时不发生变形。代码如下:
<UserControl x:Class="BaseModel.ImageButton"
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="" d:DesignWidth="" Width="" Height="" MouseLeftButtonDown="UserControl_MouseLeftButtonDown" MouseLeftButtonUp="UserControl_MouseLeftButtonUp" MouseEnter="UserControl_MouseEnter" MouseLeave="UserControl_MouseLeave">
<Viewbox>
<Grid>
<Border Name ="MainBorder" Width="" Height="" BorderBrush="White" BorderThickness="" Background="White" Opacity="0.5" Visibility="Hidden"/>
<Image Name="btnImage" Width="" Height="" Stretch="Fill" Margin="9,2,9,24"/>
<TextBlock Name="btnText" Text="" FontSize="" Width="" IsHitTestVisible="False" TextAlignment="Center" TextWrapping="Wrap" Margin="0,52,0,2"/>
</Grid>
</Viewbox>
</UserControl>
后台主要实现了鼠标悬浮和按下时的效果,以及按钮是否可用切换时图片的选择,代码如下:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes; namespace BaseModel
{
/// <summary>
/// ImageButton.xaml 的交互逻辑
/// </summary>
public partial class ImageButton : UserControl
{
#region 属性 //按钮是否可用
private bool isEnable = true;
//按钮文本
private string text = "";
//按钮文本字体
private FontFamily textFamily = new FontFamily("宋体");
//按钮文本字体大小
private double textSize = ;
//按钮文本字体颜色
private Brush textColor = Brushes.Black;
//正在被按下状态
private bool isClicking = false;
//背景图片
private BitmapSource backImage;
//正常背景资源
private ImageSource EnablebackImage;
//不可用背景资源
private ImageSource unEnablebackImage;
//按下事件
public event EventHandler click;
/// <summary>
/// 设置或获取控件可用状态
/// </summary>
[System.ComponentModel.Browsable(true), System.ComponentModel.Category("Appearance"),System.ComponentModel.Description("设置或获取控件可用状态")]
public bool IsEnable {
get {
return isEnable;
}
set {
this.btnText.IsEnabled = value;
this.btnImage.IsEnabled = value;
isEnable = value;
if (isEnable)
{
btnImage.Source = EnablebackImage;
}
else
{
btnImage.Source = unEnablebackImage;
}
}
}
/// <summary>
/// 设置或获取控件文本
/// </summary>
[System.ComponentModel.Browsable(true), System.ComponentModel.Category("Appearance"), System.ComponentModel.Description("设置或获取控件文本")]
public string Text {
get {
return text;
}
set {
this.btnText.Text = value;
text = value;
}
}
/// <summary>
/// 设置或获取控件文本字体
/// </summary>
[System.ComponentModel.Browsable(true), System.ComponentModel.Category("Appearance"), System.ComponentModel.Description("设置或获取控件文本字体")]
public FontFamily TextFamily
{
get
{
return textFamily;
}
set
{
this.btnText.FontFamily = value;
textFamily = value;
}
}
/// <summary>
/// 设置或获取控件文本字体大小
/// </summary>
[System.ComponentModel.Browsable(true), System.ComponentModel.Category("Appearance"), System.ComponentModel.Description("设置或获取控件文本字体大小")]
public double TextSize
{
get
{
return textSize;
}
set
{
this.btnText.FontSize = value;
textSize = value;
}
}
/// <summary>
/// 设置或获取控件文本字体颜色
/// </summary>
[System.ComponentModel.Browsable(true), System.ComponentModel.Category("Appearance"), System.ComponentModel.Description("设置或获取控件文本字体颜色")]
public Brush TextColor
{
get
{
return textColor;
}
set
{
this.btnText.Foreground = value;
textColor = value;
}
}
/// <summary>
/// 设置或获取控件背景图片
/// </summary>
[System.ComponentModel.Browsable(true), System.ComponentModel.Category("Appearance"), System.ComponentModel.Description("设置或获取控件背景图片")]
public BitmapSource BackImage
{
get
{
return backImage;
}
set
{
backImage = value;
getBackImageSource(value);
if (isEnable)
{
btnImage.Source = EnablebackImage;
}
else
{
btnImage.Source =unEnablebackImage;
}
}
} #endregion public ImageButton()
{
InitializeComponent();
} #region 方法 ///<summary>
///设置正常及不可用时的背景图片
///</summary>
///<param name="i">背景图片</param>
private void getBackImageSource(BitmapSource i)
{
if (i == null) {
EnablebackImage = null;
unEnablebackImage = null;
return;
}
FormatConvertedBitmap b = new FormatConvertedBitmap();
b.BeginInit();
b.Source = i;
b.DestinationFormat = PixelFormats.Gray8;
b.EndInit();
FormatConvertedBitmap b1 = new FormatConvertedBitmap();
b1.BeginInit();
b1.Source = i;
b1.EndInit();
EnablebackImage = b1;
unEnablebackImage = b;
} #endregion #region 事件 private void UserControl_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
{
if (isEnable) {
isClicking = true;
btnImage.Margin = new Thickness(, , , );
}
} private void UserControl_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
{
if (isEnable) {
if (isClicking)
{
isClicking = false;
if (click != null)
{
click(this, null);
}
btnImage.Margin = new Thickness(, , , );
}
}
} private void UserControl_MouseEnter(object sender, MouseEventArgs e)
{
if (isEnable) {
btnImage.Margin = new Thickness(, , , );
}
MainBorder.Visibility = System.Windows.Visibility.Visible;
} private void UserControl_MouseLeave(object sender, MouseEventArgs e)
{
if (isEnable)
{
if (isClicking) {
isClicking = false;
}
btnImage.Margin = new Thickness(, , , );
}
MainBorder.Visibility = System.Windows.Visibility.Hidden;
} #endregion
}
}
好了,有兴趣的同学可以研究一下,蛮简单的。
WPF利用Image实现图片按钮的更多相关文章
- WPF利用radiobutton制作菜单按钮
		
原文:WPF利用radiobutton制作菜单按钮 版权声明:欢迎转载.转载请注明出处,谢谢 https://blog.csdn.net/wzcool273509239/article/details ...
 - WPF控件库:图片按钮的封装
		
需求:很多时候界面上的按钮都需要被贴上图片,一般来说: 1.按钮处于正常状态,按钮具有背景图A 2.鼠标移至按钮上方状态,按钮具有背景图B 3.鼠标点击按钮状态,按钮具有背景图C 4.按钮处于不可用状 ...
 - c# wpf 利用截屏键实现截屏功能
		
原文:c# wpf 利用截屏键实现截屏功能 最近做一个wpf程序需要截图功能,查找资料费了一些曲折,跟大家分享一下. 先是找到了这样一份代码: static class Scr ...
 - 利用bootstrap写图片轮播
		
利用bootstrap写图片轮播 缺点是轮播没有固定样式图片样式会改变外框的大小,所以要再设置 以及左右按钮的style也要从新设置 <div class="carousel slid ...
 - 自己写一个图片按钮(XAML)
		
有时需要用三张图片(正常状态,鼠标移上,鼠标按下)来作为一个按钮的样式,虽然这种做法不好,应该用矢量的方式制作样式,但有的时候还是需要这样做的. 每次都修改按钮的样式来实现这个做法,既麻烦又会生成大段 ...
 - 利用Selenium实现图片文件上传的两种方式介绍
		
在实现UI自动化测试过程中,有一类需求是实现图片上传,这种需求根据开发的实现方式,UI的实现方式也会不同. 一.直接利用Selenium实现 这种方式是最简单的一种实现方式,但是依赖于开发的实现. 当 ...
 - WPF利用HelixToolKit后台导入3D模型
		
原文:WPF利用HelixToolKit后台导入3D模型 版权声明:本文为博主原创文章,未经博主允许不得转载. https://blog.csdn.net/m0_37591671/article/de ...
 - 利用Photoshop修改图片以达到投稿要求
		
摘自:http://www.dxy.cn/bbs/thread/8602152#8602152 利用Photoshop修改图片以达到投稿要求 软件版本为Photoshop CS V8.0.1(中文版) ...
 - [开源]基于WPF实现的Gif图片分割器,提取GIf图片中的每一帧
		
不知不觉又半个月没有更新博客了,今天终于抽出点时间,来分享一下前段时间的成果. 在网上,我们经常看到各种各样的图片,尤其是GIF图片的动态效果,让整个网站更加富有表现力!有时候,我们看到一些比较好看的 ...
 
随机推荐
- windows设备驱动安装指南
			
高观点下的设备驱动安装(overview) 一.windows是怎样安装设备的? 第一步:新设备的识别 在给一个新设备安装驱动之前,总线或集线器(hub)驱动会为连接到PC上的设备分配一个硬件ID(h ...
 - 在Visual Studio 2013中编译libssh2项目
			
 一. 下载需要的外部包,并解压,下面给出的链接如果无法访问,就google搜索下载一下: •下载openssl •下载zlib 二.修改libssh2项目配置: 1.C/C++->Gene ...
 - centos Minicom通信终端
			
minicom是linux下的串口通信软件,他使用完全使用键盘操作.它虽然没有windows下的超级终端好用,但是它也是一种串口通信的方法.一.minicom安装在超级终端中输入:sally@sall ...
 - js设计模式系列之(一)请节约你的请求-代理模式
			
What’s the proxy pattern? 代理模式其实就是将违反单一性原则的类给抽离出来,尽量满足开放和封闭的原则. 相当于一个类的行为只是一种,但是你可以给这个类添加额外的行为.比如: 一 ...
 - Gwt ListBox选中自动触发事件
			
以前用TreeView显示,需求更改 需要做一个ListBox控件显示数据,和HTML中的<Select>标签一样 编辑时候自动触发选中的数据子类: 1.只要自动触发了rootListBo ...
 - NoSQL 数据库的使用场景
			
摘要:对比传统关系型数据库,NoSQL有着更为复杂的分类——键值.面向文档.列存储.图数据库.这里就带你一览NoSQL各种类型的适用场景及一些知名公司的方案选择. 在过去几年,关系型数据库一直是数据持 ...
 - NVL函数(NVL,NVL2,NULLIF,COALESCE)
			
NVL 语法:NVL( expr1, expr2) 功能:如果expr1为NULL,则NVL函数返回expr2的值,否则返回expr1的值,如果两个参数的都为NULL ,则返回NULL. 注意事项:e ...
 - Ubuntu中设置静态IP和DNS(转载)
			
原文地址:http://blog.sina.com.cn/s/blog_669421480102v3bb.html VMware 中使用网络,对虚拟机设置静态IP:在Ubuntu中设置静态IP共两步: ...
 - 用sp_change_users_login消除Sql Server的孤立用户
			
异常详细信息: System.Data.SqlClient.SqlException: 拒绝了对对象 'zwj_EnterpriseActivities' (数据库 'Ntours',架构 'dbo' ...
 - (转)[老老实实学WCF] 第四篇 初探通信--ChannelFactory
			
第四篇 初探通信--ChannelFactory 通过前几篇的学习,我们简单了解了WCF的服务端-客户端模型,可以建立一个简单的WCF通信程序,并且可以把我们的服务寄宿在IIS中了.我们不禁感叹WCF ...