极品分享

C# WPF Button 样式的使用(加载图片,设置Button背景图片)

1、XAML:

<Window x:Class="ButtonLoadIMG.MainWindow"  
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"  
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"  
        Title="MainWindow" Height="350" Width="525" ResizeMode="NoResize">  
  
    <Window.Resources>  
        <Style TargetType="Button" x:Key="BtnStyle1" >  
            <Setter Property="Background">  
                <Setter.Value>  
                    <ImageBrush ImageSource="IMG/connect.png" />  
                </Setter.Value>  
            </Setter>  
        </Style>  
  
        <Style TargetType="Button" x:Key="BtnStyle2">  
              
            <Setter Property="Template">  
                <Setter.Value>  
                    <ControlTemplate  TargetType="Button">  
                        <Grid  Background="Transparent">  
                            <ContentPresenter x:Name="cp1" Content="{TemplateBinding Content}" VerticalAlignment="Center" HorizontalAlignment="Center" RecognizesAccessKey="True"/>  
                        </Grid>                          
                          
                        <ControlTemplate.Triggers>  
                            <Trigger Property="IsMouseOver" Value="True">  
                                <Setter TargetName="cp1" Property="Content">  
                                    <Setter.Value>  
                                        <Image Source="IMG/connect.png"/>  
                                    </Setter.Value>  
                                </Setter>  
                            </Trigger>  
  
                            <Trigger Property="IsPressed" Value="True">  
                                <Setter TargetName="cp1" Property="Content">  
                                    <Setter.Value>  
                                        <Image Source="IMG/talking.png"/>  
                                    </Setter.Value>  
                                </Setter>  
                            </Trigger>  
                        </ControlTemplate.Triggers>  
                    </ControlTemplate>  
                </Setter.Value>  
            </Setter>  
        </Style>  
    </Window.Resources>  
      
    <Grid >  
        <Grid.RowDefinitions>  
            <RowDefinition Height="150*" />  
            <RowDefinition Height="150*" />  
        </Grid.RowDefinitions>  
        <Button  Height="100" HorizontalAlignment="Center" Margin="0" Name="btnWPF" VerticalAlignment="Center" Width="100" Style="{StaticResource BtnStyle2}" >  
            <Button.Content>  
                <Image Source="IMG/set.png" />                 
            </Button.Content>  
        </Button>  
  
        <Button Content="ButtonCS" Grid.Row="1" Height="100" HorizontalAlignment="Center" Margin="0" Name="btnCS" VerticalAlignment="Center" Width="100"  MouseEnter="buttonCS_MouseEnter" MouseLeave="buttonCS_MouseLeave" MouseLeftButtonDown="buttonCS_MouseLeftButtonDown" PreviewMouseLeftButtonUp="btnCS_PreviewMouseLeftButtonUp">  
            <Button.Background>  
                <!-- <ImageBrush ImageSource="/IMG/3g05.png" Stretch="None"/> -->  
                <ImageBrush x:Name="imgBtnCS"></ImageBrush>  
            </Button.Background>  
        </Button>  
    </Grid>  
</Window>

2、CS:

using System;  
using System.Windows;  
using System.Windows.Media;  
using System.Windows.Media.Imaging;  
using System.Windows.Input;  
using System.Windows.Controls;  
  
  
namespace ButtonLoadIMG  
{  
    /// <summary>  
    /// MainWindow.xaml 的交互逻辑  
    /// 右击图片->属性->1 复制到输出目录:始终复制, 2 生成操作:内容  
    /// </summary>  
    public partial class MainWindow : Window  
    {  
        public MainWindow()  
        {  
            InitializeComponent();  
            LoadIMG("IMG/person.png");  
  
  
            MouseButtonEventHandler mouseBtnEventHandler = new MouseButtonEventHandler(this.buttonCS_MouseLeftButtonDown);  
            btnCS.AddHandler(System.Windows.Controls.Button.MouseDownEvent, mouseBtnEventHandler, true);  
            //btnCS.AddHandler(System.Windows.Controls.Button.MouseDownEvent, new MouseButtonEventHandler(this.btnCS_MouseLeftButtonUp), true);              
        }  
  
  
        private void LoadIMG(string strIMG)  
        {  
            #region 加载logo图片  
            BitmapImage bitmapImg = new BitmapImage();  
            bitmapImg.BeginInit();  
            bitmapImg.UriSource = new System.Uri(strIMG, UriKind.RelativeOrAbsolute); ;  
            //bitmapImg.DecodePixelWidth = 200;  
            bitmapImg.EndInit();  
            imgBtnCS.Stretch = Stretch.Uniform;  
            imgBtnCS.ImageSource = bitmapImg;  
            #endregion  
        }  
  
  
        private void SetBtnBackgroundIMG(string strIMGPath)  
        {  
            #region 设置图片  
            Image img = new Image();  
            BitmapImage bitmapImg = new BitmapImage();  
            bitmapImg.BeginInit();  
            bitmapImg.UriSource = new System.Uri(strIMGPath, UriKind.RelativeOrAbsolute); ;  
            //bitmapImg.DecodePixelWidth = 200;  
            bitmapImg.EndInit();  
            //imgBtnCS.Stretch = Stretch.Uniform;  
            img.Source = bitmapImg;  
            btnCS.Content = img;  
            #endregion  
        }  
  
  
        private void buttonCS_MouseEnter(object sender, MouseEventArgs e)  
        {  
            SetBtnBackgroundIMG("IMG/group.png");  
        }  
  
  
        private void buttonCS_MouseLeftButtonDown(object sender, System.Windows.Input.MouseButtonEventArgs e)  
        {  
            SetBtnBackgroundIMG("IMG/ChangeGrp.png");  
        }  
  
  
        private void btnCS_PreviewMouseLeftButtonUp(object sender, MouseButtonEventArgs e)  
        {  
            SetBtnBackgroundIMG("IMG/group.png");  
        }  
  
  
        private void buttonCS_MouseLeave(object sender, System.Windows.Input.MouseEventArgs e)  
        {  
            SetBtnBackgroundIMG("IMG/person.png");  
        }  
  
  
        //private void btnCS_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)  
        //{  
        //    //SetBtnBackgroundIMG("IMG/group.png");  
        //}  
          
    }  
}




2017-03-07 0 /
NET学习
/
标签: 

评论回复

回到顶部