书名:WPF专业编程指南
作者:李应保
出版社:电子工业出版社
出版时间:2010-01
ISBN:9787121100116
一、WPF中的颜色
WPF中的颜色用Color结构表示,这个Color结构位于System.Windows.Media命名空间中。我们知道颜色是用红(Red),绿(Green)和蓝(Blue)三种颜色调配出来的,WPF也支持这种颜色调配方法。
除了R、G、B属性之外,WPF还提供了另一个属性A。这个属性表示颜色的透明度(或叫不透明度),其值也在0和255之间。若A取为0,则表示颜色透明,若A取为255,则表示颜色不透明。还可以使用带有参数的构造函数来创建颜色:
Color lColor = Color.FromRgb(r,g,b);
Color lColor = Color.FromArgb(r,g,b);
在XAML里,上述C#语句可以表示为:
<Color B ="0" G ="0" R ="255"/>
<Color B ="0" G ="0" R ="255" A ="100" />
二、例程
<Window x:Class="Yingbao.Chapter5.TestColorProperty"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="TestColorOpacity" Height="300" Width="600">
<Window.Resources>
<Color B ="100" G ="100" R ="100" A ="100" x:Key="myColor" />
</Window.Resources>
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height ="auto"/>
<RowDefinition Height ="auto"/>
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*"/>
<ColumnDefinition Width="*"/>
<ColumnDefinition Width="*"/>
<ColumnDefinition Width="*"/>
</Grid.ColumnDefinitions>
<Rectangle Name ="dispRect" Stroke ="DarkGreen"
StrokeThickness="5" Fill="Black" Grid.Column ="1"
Grid.Row ="1" Grid.ColumnSpan ="2" Height ="100"
Margin ="20,10,20,10"/>
<StackPanel Grid.Column ="0" Grid.Row ="0"
Orientation ="Horizontal" >
<TextBlock FontSize ="12" Margin ="10,30,5,20"
Padding ="10">红:</TextBlock>
<Slider Orientation ="Vertical" Value="0" Height ="80"
Minimum="0" Maximum="255" TickPlacement="BottomRight"
TickFrequency="10" IsSnapToTickEnabled="True" Margin
="0,0,5,0" AutoToolTipPlacement="BottomRight"
AutoToolTipPrecision="2"
ValueChanged="OnRedSliderValueChange"/>
</StackPanel>
<StackPanel Grid.Column ="1" Grid.Row ="0"
Orientation ="Horizontal" >
<TextBlock FontSize ="12" Margin ="10,30,5,20"
Padding ="10">绿:</TextBlock>
<Slider Orientation ="Vertical" Value="0" Height ="80"
Minimum="0" Maximum="255" TickPlacement="BottomRight"
TickFrequency="10" IsSnapToTickEnabled="True" Margin
="0,0,5,0" AutoToolTipPlacement="BottomRight"
AutoToolTipPrecision="2"
ValueChanged="OnGreenSliderValueChange"/>
</StackPanel>
<StackPanel Grid.Column ="2" Grid.Row ="0" Orientation
="Horizontal" >
<TextBlock FontSize ="12" Margin ="10,30,5,20" Padding ="10">
蓝:</TextBlock>
<Slider Orientation ="Vertical" Height ="80" Minimum="0"
Maximum="255" TickPlacement="BottomRight"
TickFrequency="10" IsSnapToTickEnabled="True" Margin
="0,0,5,0" AutoToolTipPlacement="BottomRight"
AutoToolTipPrecision="2"
ValueChanged="OnBlueSliderValueChange"/>
</StackPanel>
<StackPanel Grid.Column ="3" Grid.Row ="0" Orientation
="Horizontal" >
<TextBlock FontSize ="12" Margin ="10,30,5,20" Padding ="10">
透明度:</TextBlock>
<Slider Orientation ="Vertical" Height ="80" Minimum="0"
Maximum="255" TickPlacement="BottomRight"
TickFrequency="10" IsSnapToTickEnabled="True" Margin
="0,0,5,0" AutoToolTipPlacement="BottomRight"
AutoToolTipPrecision="2"
ValueChanged="OnOpacitySliderValueChange"
Value="255"/>
</StackPanel>
</Grid>
</Window>
上面这段程序使用表格(Grid)排版。
首先创建了2行4列的网格,在第2行的1,2两列,放一个矩形图形。
这个矩形的填充色可以由第一行中的4个滑块控件来调整,这四个滑块控件依次调整红、绿、蓝及透明度的值。
在该程序中滑块的滑动范围从0到255。
TickPlacement设定显示刻度的位置,TickFrequency设置每个刻度的大小。这里TickFrequency设为10,所以在每个滑块的标尺上应有255/10=25个刻度。
AutoToolTipPlacement用来设置显示滑块数值的位置,AutoToolTipPrecision用来设置显示滑块数值的精度,这里设为2,即显示小数点后两位。实际上,我们的ARGB为0~255之间的整数,所以精度对我们来说并不重要。应当注意,我们必须把创建矩形的XAML语句放在创建滑块语句之前,原因是设定滑块值时,需要用到矩形对象,若没有创建矩形对象,C#会产生NullReference异常。处理滑块移动时的C#程序如下:
using System;
using System.Collections.Generic;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
namespace Yingbao.Chapter5
{
public partial class TestColorProperty :System.Windows.Window
{
double lRed = 0;
double lGreen = 0;
double lBlue = 0;
double lOpacity = 255;
public TestColorProperty()
{
InitializeComponent();
}
private void OnRedSliderValueChange(object sender,
RoutedPropertyChangedEventArgs<double> e)
{
lRed = e.NewValue;
Color clr = new Color();
clr.R = Convert.ToByte( lRed );
clr.G = Convert.ToByte( lGreen );
clr.B = Convert.ToByte( lBlue );
clr.A = Convert.ToByte( lOpacity );
dispRect.Fill = new SolidColorBrush(clr);
}
private void OnOpacitySliderValueChange(object sender,
RoutedPropertyChangedEventArgs<double> e)
{
lOpacity = e.NewValue;
Color clr = new Color();
clr.R = Convert.ToByte( lRed );
clr.G = Convert.ToByte( lGreen );
clr.B = Convert.ToByte( lBlue );
clr.A = Convert.ToByte( lOpacity );
dispRect.Fill = new SolidColorBrush(clr);
}
private void OnGreenSliderValueChange(object sender,
RoutedPropertyChangedEventArgs<double> e)
{
lGreen = e.NewValue;
Color clr = new Color();
clr.R = Convert.ToByte(lRed);
clr.G = Convert.ToByte(lGreen);
clr.B = Convert.ToByte(lBlue);
clr.A = Convert.ToByte(lOpacity);
dispRect.Fill = new SolidColorBrush(clr);
}
private void OnBlueSliderValueChange(object sender,
RoutedPropertyChangedEventArgs<double> e)
{
this.lBlue = e.NewValue;
Color clr = new Color();
clr.R = Convert.ToByte(lRed);
clr.G = Convert.ToByte(lGreen);
clr.B = Convert.ToByte(lBlue);
clr.A = Convert.ToByte(lOpacity);
dispRect.Fill = new SolidColorBrush(clr);
}
}
}
-
TextColorProperty类是从Window类中派生出来的,该类定义了四个类型为double的域:lRed、lGreen、lBlue、lOpacity,用来存放红、绿、蓝及透明度值。
然后是四个滑块滑动事项的处理函数,每个函数读出相应于红、绿、蓝及透明度的值,并用这几个值来设定Color中的相应属性,最后创建该颜色的SolidColorBrush,并把该画刷作为矩形的填充画刷。
这段程序的运行结果如图5-1所示。
图5-1 自建调整颜色工具
