之前有一篇文章也是采用了Image實現的圖片按鈕,不過時間太久遠了,忘記了地址。好吧,這里我進行了進一步的改進,原來的文章中需要設置4張圖片,分別為可用時,鼠標懸浮時,按鈕按下時,按鈕不可用時的圖片,這里我只用了一張圖片,利用C#的圖片灰度處理自動獲得不可用時的圖片,利用圖片的間距實現懸浮及按下效果。先上效果:(正常 懸浮 按下 不可用)
代碼其實比較簡單,唯一的難點就是把圖片轉換成ImageSource,在網上找了很久終於找到了一個,轉換代碼如下:
1 ///<summary> 2 ///設置正常及不可用時的背景圖片 3 ///</summary> 4 ///<param name="i">背景圖片</param> 5 private void getBackImageSource(BitmapSource i) 6 { 7 if (i == null) { 8 EnablebackImage = null; 9 unEnablebackImage = null; 10 return; 11 } 12 FormatConvertedBitmap b = new FormatConvertedBitmap(); 13 b.BeginInit(); 14 b.Source = i; 15 b.DestinationFormat = PixelFormats.Gray8; 16 b.EndInit(); 17 FormatConvertedBitmap b1 = new FormatConvertedBitmap(); 18 b1.BeginInit(); 19 b1.Source = i; 20 b1.EndInit(); 21 EnablebackImage = b1; 22 unEnablebackImage = b; 23 }
前端采用Image作為主體部分,利用Border模仿按鈕的邊框,TextBlock作為文本顯示。注意:代碼主體部分利用ViewBox保證控件大小變化時不發生變形。代碼如下:
1 <UserControl x:Class="BaseModel.ImageButton" 2 xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 3 xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 4 xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 5 xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 6 mc:Ignorable="d" 7 d:DesignHeight="74" d:DesignWidth="60" Width="66" Height="76" MouseLeftButtonDown="UserControl_MouseLeftButtonDown" MouseLeftButtonUp="UserControl_MouseLeftButtonUp" MouseEnter="UserControl_MouseEnter" MouseLeave="UserControl_MouseLeave"> 8 <Viewbox> 9 <Grid> 10 <Border Name ="MainBorder" Width="66" Height="76" BorderBrush="White" BorderThickness="2" Background="White" Opacity="0.5" Visibility="Hidden"/> 11 <Image Name="btnImage" Width="48" Height="48" Stretch="Fill" Margin="9,2,9,24"/> 12 <TextBlock Name="btnText" Text="" FontSize="10" Width="66" IsHitTestVisible="False" TextAlignment="Center" TextWrapping="Wrap" Margin="0,52,0,2"/> 13 </Grid> 14 </Viewbox> 15 </UserControl>
后台主要實現了鼠標懸浮和按下時的效果,以及按鈕是否可用切換時圖片的選擇,代碼如下:
1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Text; 5 using System.Windows; 6 using System.Windows.Controls; 7 using System.Windows.Data; 8 using System.Windows.Documents; 9 using System.Windows.Input; 10 using System.Windows.Media; 11 using System.Windows.Media.Imaging; 12 using System.Windows.Navigation; 13 using System.Windows.Shapes; 14 15 namespace BaseModel 16 { 17 /// <summary> 18 /// ImageButton.xaml 的交互邏輯 19 /// </summary> 20 public partial class ImageButton : UserControl 21 { 22 #region 屬性 23 24 //按鈕是否可用 25 private bool isEnable = true; 26 //按鈕文本 27 private string text = ""; 28 //按鈕文本字體 29 private FontFamily textFamily = new FontFamily("宋體"); 30 //按鈕文本字體大小 31 private double textSize = 10; 32 //按鈕文本字體顏色 33 private Brush textColor = Brushes.Black; 34 //正在被按下狀態 35 private bool isClicking = false; 36 //背景圖片 37 private BitmapSource backImage; 38 //正常背景資源 39 private ImageSource EnablebackImage; 40 //不可用背景資源 41 private ImageSource unEnablebackImage; 42 //按下事件 43 public event EventHandler click; 44 /// <summary> 45 /// 設置或獲取控件可用狀態 46 /// </summary> 47 [System.ComponentModel.Browsable(true), System.ComponentModel.Category("Appearance"),System.ComponentModel.Description("設置或獲取控件可用狀態")] 48 public bool IsEnable { 49 get { 50 return isEnable; 51 } 52 set { 53 this.btnText.IsEnabled = value; 54 this.btnImage.IsEnabled = value; 55 isEnable = value; 56 if (isEnable) 57 { 58 btnImage.Source = EnablebackImage; 59 } 60 else 61 { 62 btnImage.Source = unEnablebackImage; 63 } 64 } 65 } 66 /// <summary> 67 /// 設置或獲取控件文本 68 /// </summary> 69 [System.ComponentModel.Browsable(true), System.ComponentModel.Category("Appearance"), System.ComponentModel.Description("設置或獲取控件文本")] 70 public string Text { 71 get { 72 return text; 73 } 74 set { 75 this.btnText.Text = value; 76 text = value; 77 } 78 } 79 /// <summary> 80 /// 設置或獲取控件文本字體 81 /// </summary> 82 [System.ComponentModel.Browsable(true), System.ComponentModel.Category("Appearance"), System.ComponentModel.Description("設置或獲取控件文本字體")] 83 public FontFamily TextFamily 84 { 85 get 86 { 87 return textFamily; 88 } 89 set 90 { 91 this.btnText.FontFamily = value; 92 textFamily = value; 93 } 94 } 95 /// <summary> 96 /// 設置或獲取控件文本字體大小 97 /// </summary> 98 [System.ComponentModel.Browsable(true), System.ComponentModel.Category("Appearance"), System.ComponentModel.Description("設置或獲取控件文本字體大小")] 99 public double TextSize 100 { 101 get 102 { 103 return textSize; 104 } 105 set 106 { 107 this.btnText.FontSize = value; 108 textSize = value; 109 } 110 } 111 /// <summary> 112 /// 設置或獲取控件文本字體顏色 113 /// </summary> 114 [System.ComponentModel.Browsable(true), System.ComponentModel.Category("Appearance"), System.ComponentModel.Description("設置或獲取控件文本字體顏色")] 115 public Brush TextColor 116 { 117 get 118 { 119 return textColor; 120 } 121 set 122 { 123 this.btnText.Foreground = value; 124 textColor = value; 125 } 126 } 127 /// <summary> 128 /// 設置或獲取控件背景圖片 129 /// </summary> 130 [System.ComponentModel.Browsable(true), System.ComponentModel.Category("Appearance"), System.ComponentModel.Description("設置或獲取控件背景圖片")] 131 public BitmapSource BackImage 132 { 133 get 134 { 135 return backImage; 136 } 137 set 138 { 139 backImage = value; 140 getBackImageSource(value); 141 if (isEnable) 142 { 143 btnImage.Source = EnablebackImage; 144 } 145 else 146 { 147 btnImage.Source =unEnablebackImage; 148 } 149 } 150 } 151 152 #endregion 153 154 public ImageButton() 155 { 156 InitializeComponent(); 157 } 158 159 #region 方法 160 161 ///<summary> 162 ///設置正常及不可用時的背景圖片 163 ///</summary> 164 ///<param name="i">背景圖片</param> 165 private void getBackImageSource(BitmapSource i) 166 { 167 if (i == null) { 168 EnablebackImage = null; 169 unEnablebackImage = null; 170 return; 171 } 172 FormatConvertedBitmap b = new FormatConvertedBitmap(); 173 b.BeginInit(); 174 b.Source = i; 175 b.DestinationFormat = PixelFormats.Gray8; 176 b.EndInit(); 177 FormatConvertedBitmap b1 = new FormatConvertedBitmap(); 178 b1.BeginInit(); 179 b1.Source = i; 180 b1.EndInit(); 181 EnablebackImage = b1; 182 unEnablebackImage = b; 183 } 184 185 #endregion 186 187 #region 事件 188 189 private void UserControl_MouseLeftButtonDown(object sender, MouseButtonEventArgs e) 190 { 191 if (isEnable) { 192 isClicking = true; 193 btnImage.Margin = new Thickness(13, 6, 13, 28); 194 } 195 } 196 197 private void UserControl_MouseLeftButtonUp(object sender, MouseButtonEventArgs e) 198 { 199 if (isEnable) { 200 if (isClicking) 201 { 202 isClicking = false; 203 if (click != null) 204 { 205 click(this, null); 206 } 207 btnImage.Margin = new Thickness(9, 2, 9, 24); 208 } 209 } 210 } 211 212 private void UserControl_MouseEnter(object sender, MouseEventArgs e) 213 { 214 if (isEnable) { 215 btnImage.Margin = new Thickness(9, 2, 9, 24); 216 } 217 MainBorder.Visibility = System.Windows.Visibility.Visible; 218 } 219 220 private void UserControl_MouseLeave(object sender, MouseEventArgs e) 221 { 222 if (isEnable) 223 { 224 if (isClicking) { 225 isClicking = false; 226 } 227 btnImage.Margin = new Thickness(9, 2, 9, 24); 228 } 229 MainBorder.Visibility = System.Windows.Visibility.Hidden; 230 } 231 232 #endregion 233 } 234 }
好了,有興趣的同學可以研究一下,蠻簡單的。