首页 > 代码库 > WPF中使用VisualBrush的实例

WPF中使用VisualBrush的实例

本文实现一个名为“你来我往”的小程序,该程序管理着“张三”和“李四”两位童鞋拥有的现金,一开始,两人均拥有100美元的现金,随着将现金从其中一人转移至另外一人,两人拥有的现金数在不断变化,程序可以跟踪这种变化,并正确显示每人拥有的现金数。每次最多可以转移三张纸币,纸币的金额可以是5美元、10美元或者20美元。

程序运行后的效果如图1所示,我们点击“张三”右边的“5美元”“10美元”“20美元”按钮,再点击“Play”按钮,便完成了现金从“张三”向“李四”流动,流动后,“张三”拥有的现金变为135美元,而“李四”仅剩下65美元,运行后的效果如图2所示。

 

1  初始状态图

 

图2 现金流动后的分布图

在点击“张三”右边的“5美元”“10美元”或者“20美元”按钮时,会将“5美元”“10美元”或者“20美元”按钮复制一份进“张三”与“李四”之间的“待支付区”,而完成这个操作就需要使用到VisualBrush。

 

图3 现金流进待支付区的示意图

点击“Cancel”按钮可以清空“待支付区”中的按钮。

VisualBrush是WPF画刷类Brush的派生类,Brush类是个抽象类,WPF中派生自Brush类的还有:SolidColorBrush(实心画刷)、LinearGradientBrush(线性渐变画刷)、RadialGradientBrush(径向渐变画刷)、ImageBrushDrawingBrush等。VisualBrush使用Visual对象来绘制区域,Visual对象包括ButtonPage等。本例中使用VisualBrush来绘制RectangleFill,最终达到的效果就是将按钮复制进“待支付区”。这部分操作的代码如下所示。

private void AddChildren(Button btn)
{
     VisualBrush vBrush = new VisualBrush(btn);
     Rectangle rect = new Rectangle();
     rect.Width = btn.ActualWidth;
     rect.Height = btn.ActualHeight;
     rect.Fill = vBrush;
     rect.Opacity = 0.8;
     this.StackPanel_Pay.Children.Add(rect);
}
代码中,使用按钮初始化VisualBrush,接着,按照按钮的宽度和高度绘制矩形,并将矩形的Fill属性设置为VisualBrush对象,最后将新绘制的矩形添加进“待支付区”。 好了,就到这里了。

下面附上本例的完整代码。

MainWindow.xaml文件代码

<Window x:Class="WpfVisualBrushExp.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="你来我往" Height="350" Width="525" WindowStyle="ToolWindow">
    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition Height="*"></RowDefinition>
            <RowDefinition Height="*"></RowDefinition>
            <RowDefinition Height="*"></RowDefinition>
            <RowDefinition Height="0.2*"></RowDefinition>
            <RowDefinition Height="*"></RowDefinition>
            <RowDefinition Height="0.2*"></RowDefinition>
            <RowDefinition Height="*"></RowDefinition>
            <RowDefinition Height="*"></RowDefinition>
            <RowDefinition Height="*"></RowDefinition>
        </Grid.RowDefinitions>
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="*"></ColumnDefinition>
            <ColumnDefinition Width="*"></ColumnDefinition>
            <ColumnDefinition Width="*"></ColumnDefinition>
            <ColumnDefinition Width="0.05*"></ColumnDefinition>
            <ColumnDefinition Width="*"></ColumnDefinition>
        </Grid.ColumnDefinitions>
        <TextBlock Text="张三拥有" Grid.Row="1" Grid.Column="0" HorizontalAlignment="Right" Margin="0,10,0,10" />
        <TextBlock x:Name="TextBlock_ZhangsanAmount" Grid.Row="1" Grid.Column="1" HorizontalAlignment="Center" Margin="0,10,0,10" />
        <TextBlock Text="美元" Grid.Row="1" Grid.Column="2" HorizontalAlignment="Left" Margin="0,10,0,10" />
        <Button x:Name="Button_ToLisiFive" Content="5美元" Grid.Row="0" Grid.Column="4" Click="Button_ToLisiFive_Click" />
        <Button x:Name="Button_ToLisiTen" Content="10美元" Grid.Row="1" Grid.Column="4" Click="Button_ToLisiTen_Click" />
        <Button x:Name="Button_ToLisiTwenty" Content="20美元" Grid.Row="2" Grid.Column="4" Click="Button_ToLisiTwenty_Click" />
        
        <TextBlock Grid.Row="3" Grid.Column="0" Grid.ColumnSpan="5" Background="Cyan"/>
 
        <StackPanel x:Name="StackPanel_Pay" Background="White" Grid.Row="4" Grid.Column="0" Grid.ColumnSpan="3" Orientation="Horizontal"/>
 
        <StackPanel Grid.Row="4" Grid.Column="4">
            <Button x:Name="Button_Pay" Content="Pay"  Click="Button_Pay_Click" />
            <Button x:Name="Button_Cancel" Content="Cancel" Click="Button_Cancel_Click" />
        </StackPanel>
                      
        <TextBlock Grid.Row="5" Grid.Column="0" Grid.ColumnSpan="5" Background="Cyan"/>
        
        <TextBlock Text="李四拥有" Grid.Row="7" Grid.Column="0" HorizontalAlignment="Right" Margin="0,10,0,10" />
        <TextBlock x:Name="TextBlock_LisiAmount" Grid.Row="7" Grid.Column="1" HorizontalAlignment="Center" Margin="0,10,0,10" />
        <TextBlock Text="美元" Grid.Row="7" Grid.Column="2" HorizontalAlignment="Left" Margin="0,10,0,10" />
        <Button x:Name="Button_ToZhangsanFive" Content="5美元" Grid.Row="6" Grid.Column="4" Click="Button_ToZhangsanFive_Click" />
        <Button x:Name="Button_ToZhangsanTen" Content="10美元" Grid.Row="7" Grid.Column="4" Click="Button_ToZhangsanTen_Click" />
        <Button x:Name="Button_ToZhangsanTwenty" Content="20美元" Grid.Row="8" Grid.Column="4" Click="Button_ToZhangsanTwenty_Click" />
 
        <TextBlock Grid.Row="0" Grid.Column="3" Grid.RowSpan="9" Background="Cyan"/>
    </Grid>
</Window>

MainWindow.xaml.cs文件代码

//************************************************************  
//  
// VisualBrush示例代码  
//  
// Author:三五月儿  
//   
// Date:2014/08/26 
//  
// http://blog.csdn.net/yl2isoft  
//  
//************************************************************  
using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;
using System.Windows.Shapes;
 
namespace WpfVisualBrushExp
{
    /// <summary>
    /// MainWindow.xaml 的交互逻辑
    /// </summary>
    public partial class MainWindow : Window
    {
        bool isZhangsanPaying = false;
        bool isLisiPaying = false;
        List<int> zhangsanPayAmountList = new List<int>();
        List<int> lisiPayAmountList = new List<int>();
 
        public MainWindow()
        {
            InitializeComponent();
            this.TextBlock_ZhangsanAmount.Text = "100";
            this.TextBlock_LisiAmount.Text = "100";
        }
 
        #region 张三
        private void Button_ToLisiFive_Click(object sender, RoutedEventArgs e)
        {
            if (ReturnByMaxCount()
                || ReturnByLisiIsPaying())
            {
                return;
            }
            AddChildren(this.Button_ToLisiFive);
            zhangsanPayAmountList.Add(5);
            isZhangsanPaying = true;
        }
 
        private void Button_ToLisiTen_Click(object sender, RoutedEventArgs e)
        {
            if (ReturnByMaxCount()
                || ReturnByLisiIsPaying())
            {
                return;
            }
            AddChildren(this.Button_ToLisiTen);
            zhangsanPayAmountList.Add(10);
            isZhangsanPaying = true;
        }
 
        private void Button_ToLisiTwenty_Click(object sender, RoutedEventArgs e)
        {
            if (ReturnByMaxCount() 
                || ReturnByLisiIsPaying())
            {
                return;
            }
            AddChildren(this.Button_ToLisiTwenty);
            zhangsanPayAmountList.Add(20);
            isZhangsanPaying = true;
        }
        #endregion //张三
 
        #region 李四
        private void Button_ToZhangsanFive_Click(object sender, RoutedEventArgs e)
        {
            if (ReturnByMaxCount()
                || ReturnByZhangsanIsPaying())
            {
                return;
            }
            AddChildren(this.Button_ToZhangsanFive);
            lisiPayAmountList.Add(5);
            isLisiPaying = true;
        }
 
        private void Button_ToZhangsanTen_Click(object sender, RoutedEventArgs e)
        {
            if (ReturnByMaxCount()
                || ReturnByZhangsanIsPaying())
            {
                return;
            }
            AddChildren(this.Button_ToZhangsanTen);
            lisiPayAmountList.Add(10);
            isLisiPaying = true;
        }
 
        private void Button_ToZhangsanTwenty_Click(object sender, RoutedEventArgs e)
        {
            if (ReturnByMaxCount()
                || ReturnByZhangsanIsPaying())
            {
                return;
            }
            AddChildren(this.Button_ToZhangsanTwenty);
            lisiPayAmountList.Add(20);
            isLisiPaying = true;
        }
        #endregion //李四
 
        private bool ReturnByMaxCount()
        {
            if (this.StackPanel_Pay.Children.Count >= 3)
            {
                MessageBox.Show("一次最多只能支付3张美元。");
                return true;
            }
            return false;
        }
 
        private bool ReturnByZhangsanIsPaying()
        {
            if (isZhangsanPaying == true)
            {
                MessageBox.Show("张三正在支付中...");
                return true;
            }
            return false;
        }
 
        private bool ReturnByLisiIsPaying()
        {
            if (isLisiPaying == true)
            {
                MessageBox.Show("李四正在支付中...");
                return true;
            }
            return false;
        }
 
        private void AddChildren(Button btn)
        {
            VisualBrush vBrush = new VisualBrush(btn);
            Rectangle rect = new Rectangle();
            rect.Width = btn.ActualWidth;
            rect.Height = btn.ActualHeight;
            rect.Fill = vBrush;
            rect.Opacity = 0.8;
            this.StackPanel_Pay.Children.Add(rect);
        }
 
        private void Button_Pay_Click(object sender, RoutedEventArgs e)
        {
            if (isZhangsanPaying)
            {
                int zhangsanAmount = Convert.ToInt32(this.TextBlock_ZhangsanAmount.Text) - zhangsanPayAmountList.Sum(it => it);
                int lisiAmount = Convert.ToInt32(this.TextBlock_LisiAmount.Text) + zhangsanPayAmountList.Sum(it => it);
                this.TextBlock_ZhangsanAmount.Text = zhangsanAmount.ToString();
                this.TextBlock_LisiAmount.Text = lisiAmount.ToString();
                zhangsanPayAmountList.Clear();
                isZhangsanPaying = false;
            }
            else
            {
                int lisiAmount = Convert.ToInt32(this.TextBlock_LisiAmount.Text) - lisiPayAmountList.Sum(it => it);
                int zhangsanAmount = Convert.ToInt32(this.TextBlock_ZhangsanAmount.Text) + lisiPayAmountList.Sum(it => it);
                this.TextBlock_LisiAmount.Text = lisiAmount.ToString();
                this.TextBlock_ZhangsanAmount.Text = zhangsanAmount.ToString();
                lisiPayAmountList.Clear();
                isLisiPaying = false;
            }
            this.StackPanel_Pay.Children.Clear();
        }
 
        private void Button_Cancel_Click(object sender, RoutedEventArgs e)
        {
            this.StackPanel_Pay.Children.Clear();
            zhangsanPayAmountList.Clear();
            lisiPayAmountList.Clear();          
            isZhangsanPaying = false;
            isLisiPaying = false;
        }
    }
}

 


 

 

 


WPF中使用VisualBrush的实例