一般漂亮点的软件界面都不会使用系统默认的按钮样式其中异形的图片按钮使用频率比较高 本猿不想每次需要的时候再重新写一遍或者大段的复制粘贴代码所以做了一个自定义的图片按钮控件 扩充一下自己的军火库当做常规武器方便以后使用 做之前也查了不少资料发现写XAML可以实现很多很炫的动画效果 用express工具也可以做出很炫的水晶按钮 可惜本猿是从C++到C#再转到WPF的所以上面样都不是很熟 只能用做C#winform程序的思想来实现了 按钮最多包括态的图片弹起经过按下禁用其中弹起和按下时必须的 初始化图片按钮控件的时候指定到张图片的路径按钮载入图片 控件捕获其内部image控件的鼠标事件改变image的显示图片 鼠标在空间内按下然后弹起则认为是点击事件触发该控件的自定义点击事件 imageButtonxaml xmlns= xmlns:x= >> imageButtonxamlcs using System; using SystemWindowsControls; using SystemWindowsInput; using SystemWindowsMedia; namespace NingTao { /// /// imageButtonxaml 的交互逻辑 /// public partial class imageButton : UserControl { // 设置按钮使能状态 private bool isEnable = true; // 按钮的种状态图片 private ImageSource imageUp = null; private ImageSource imageHover = null; private ImageSource imageDown = null; private ImageSource imageDisable = null; // 按钮的文本属性 private string text = ; private FontFamily textFamily; private double textSize; private Brush textColor; // 是否在当前按钮中按下 private bool isClicking = false; // 点击事件 public event EventHandler click; public imageButton() { InitializeComponent() } #region 属性赋值 // 按钮可用 public bool IsEnable { get { return isEnable; } set { isEnable = value; imageBtnSource = isEnable ? imageUp : imageDisable; } } // 按钮弹起图片 public ImageSource ImageUp { get { return imageUp; } set { imageUp = value; imageBtnSource = imageUp; } } // 按钮划过图片 public ImageSource ImageHover { get { return imageHover; } set { imageHover = value; } } // 按钮按下图片 public ImageSource ImageDown { get { return imageDown; } set { imageDown = value; } } // 按钮禁用图片 public ImageSource ImageDisable { get { return imageDisable; } set { imageDisable = value; } } // 按钮文本 public string Text { get { return text; } set { text = value; labelBtnContent = text; } } // 按钮字体 public FontFamily TextFamily { get { return textFamily; } set { textFamily = value; labelBtnFontFamily = textFamily; } } // 按钮字号 public double TextSize { get { return textSize; } set { textSize = value; labelBtnFontSize = textSize; } } // 文字颜色 public Brush TextColor { get { return textColor; } set { textColor = value; labelBtnForeground = textColor; } } #endregion #region 按钮事件 // 进入 private void imageBtn_MouseEnter(object sender MouseEventArgs e) { if (isEnable) { if (null != imageHover) { imageBtnSource = imageHover; } } } // 按下 private void imageBtn_MouseLeftButtonDown(object sender MouseButtonEventArgs e) { if (isEnable) { isClicking = true; if (null != imageDown) { imageBtnSource = imageDown; } } } // 弹起 private void imageBtn_MouseLeftButtonUp(object sender MouseButtonEventArgs e) { if (isEnable) { // 完成在控件上点击 if (isClicking) { isClicking = false; imageBtnSource = imageUp; // 触发点击事件 if (null != click) click(this null) } } } // 离开 private void imageBtn_MouseLeave(object sender MouseEventArgs e) { if (isEnable) { isClicking = false; imageBtnSource = imageUp; } } #endregion } } 使用方法 xmlns= xmlns:x= xmlns:imageBotton =clrnamespace:NingTao Title=图片按钮演示 Height= Width=> using System;using SystemCollectionsGeneric;using SystemLinq;using SystemText;using SystemWindows;using SystemWindowsControls;using SystemWindowsData;using SystemWindowsDocuments;using SystemWindowsInput;using SystemWindowsMedia;using SystemWindowsMediaImaging;using SystemWindowsNavigation;using SystemWindowsShapes;namespace WpfTest{ /// /// Windowxaml 的交互逻辑 /// public partial class Window : Window { public Window() { InitializeComponent() } private void Grid_Loaded(object sender RoutedEventArgs e) { // 加载按钮图片 try { imgButtonAImageUp = new BitmapImage(new Uri(SystemEnvironmentCurrentDirectory + //skins//apng)) imgButtonAImageHover = new BitmapImage(new Uri(SystemEnvironmentCurrentDirectory + //skins//apng)) imgButtonAImageDown = new BitmapImage(new Uri(SystemEnvironmentCurrentDirectory + //skins//apng)) imgButtonAImageDisable = new BitmapImage(new Uri(SystemEnvironmentCurrentDirectory + //skins//apng)) imgButtonVImageUp = new BitmapImage(new Uri(SystemEnvironmentCurrentDirectory + //skins//vpng)) imgButtonVImageHover = new BitmapImage(new Uri(SystemEnvironmentCurrentDirectory + //skins//vpng)) imgButtonVImageDown = new BitmapImage(new Uri(SystemEnvironmentCurrentDirectory + //skins//vpng)) imgButtonVImageDisable = new BitmapImage(new Uri(SystemEnvironmentCurrentDirectory + //skins//vpng)) } catch { MessageBoxShow(按钮图片加载出错!) } // 按钮文字 imgButtonAText = 禁用按钮; imgButtonVText = 禁用按钮; // 按钮点击事件 imgButtonAclick += new EventHandler(imgButtonA_click) imgButtonVclick += new EventHandler(imgButtonV_click) } // 禁用按钮 void imgButtonA_click(object sender EventArgs e) { imgButtonVIsEnable = !imgButtonVIsEnable; } // 禁用按钮 void imgButtonV_click(object sender EventArgs e) { imgButtonAIsEnable = !imgButtonAIsEnable; } }} |