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图片的动态效果,让整个网站更加富有表现力!有时候,我们看到一些比较好看的 ...
随机推荐
- 在WPF中使用AForge.net控制摄像头拍照
原文:在WPF中使用AForge.net控制摄像头拍照 利用AForge.net控制摄像头拍照最方便的方法就是利用PictureBox显示摄像头画面,但在WPF中不能直接使用PictureBox.必须 ...
- CMAKE 生成VS2008静态库工程 与 CMAKE使用,CMakeLists.txt编写总结
cmake -G"Visual Studio 9 2008 Win64" 以上命令得用cd命令切换到顶层CMakeLists.txt的当前目录,才能生效 以下是CMakeLists ...
- BZOJ2023: [Usaco2005 Nov]Ant Counting 数蚂蚁
2023: [Usaco2005 Nov]Ant Counting 数蚂蚁 Time Limit: 4 Sec Memory Limit: 64 MBSubmit: 56 Solved: 16[S ...
- POJ3580---SuperMemo (Splay)
各种操作,区间更新,求最值.翻转.插入.删除.当然是Splay这种神器了. 主要是 revolve这个操作,其实也就是3个区间翻转放到一块, 比如 REVOLVE x y T,T %= (y-x+1) ...
- URAL 1658
题目大意:求出T个最小的满足各个位的和为S1,平方和为S2的数.按顺序输出.数的位数大于100或者不存在这样一个数时,输出:No solution. KB 64bit IO Format:%I ...
- Java小项目--坦克大战(version1.0)
Java小项目--坦克大战<TankWar1.0> 这个小项目主要是练习j2se的基础内容和面向对象的思想.项目实现了基本的简单功能,我方一辆坦克,用上下左右键控制移动方向,按F键为发射炮 ...
- JMeter Building a Database Test Plan
Building a Database Test Plan In this section, you will learn how to create a basic Test Planto test ...
- [RxJS] Logging a Stream with do()
To help understand your stream, you’ll almost always want to log out some the intermediate values to ...
- hdu2767 Proving Equivalences --- 强连通
给一个图,问至少加入�多少条有向边能够使图变成强连通的. 原图是有环的,缩点建图,在该DAG图上我们能够发现,要使该图变成强连通图必须连成环 而加入�最少的边连成环,就是把图上入度为0和出度为0的点连 ...
- Curl命令使用方法
Curl是Linux下一个很强大的http命令行工具,其功能十分强大.1) 读取网页$ curl http://www.linuxidc.com2) 保存网页$ curl http://www.lin ...