有部分在这里说明一下,也是书中的说明,觉得有用,为什么改变了brush的值就可以改变background的值呢?原来是WPF背后搞得~说是自动调用change方法……
//xaml文件
<Window x:Class="HellowWPF.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" Loaded="Window_Loaded" MouseMove="Window_MouseMove">
</Window>
//xaml.cs文件
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
namespace HellowWPF
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
SolidColorBrush brush = new SolidColorBrush(Colors.Black);
private void BackgroundColor()
{
Title = "改变颜色";
Width=384;
Height=384;
Background = brush;
}
private void Window_Loaded(object sender, RoutedEventArgs e)
{
BackgroundColor();
}
private void Window_MouseMove(object sender, MouseEventArgs e)
{
double width = ActualWidth - 2 * SystemParameters.ResizeFrameVerticalBorderWidth;
double heigh = ActualHeight - 2 * SystemParameters.ResizeFrameHorizontalBorderHeight - SystemParameters.CaptionHeight;
Point ptmouse = e.GetPosition(this);
Point ptcenter = new Point(width / 2, heigh / 2);
Vector vectmouse = ptmouse - ptcenter;
double angle = Math.Atan2(vectmouse.X, vectmouse.Y);
Vector vectEllipse = new Vector(width / 2 * Math.Cos(angle), heigh / 2 * Math.Sin(angle));
byte byLevel = (byte)(255 * (1 - Math.Min(1, vectmouse.Length / vectEllipse.Length)));
Color clr = brush.Color;
clr.R = clr.G = clr.B = byLevel;
brush.Color = clr;
}
}
}