NOVOTS KMS 词汇表 Glossary    联系我们 Contact Us
查询 Search  
   
按类别浏览 Browse by Category
NOVOTS KMS .: WPF小实例,依鼠标靠近窗体的中心程度而改变背景颜色

WPF小实例,依鼠标靠近窗体的中心程度而改变背景颜色

有部分在这里说明一下,也是书中的说明,觉得有用,为什么改变了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;

}
}
}


这篇文章对你多有用?

相关文章

article 回收站清空了也能把文件找回
首先打开注册表,依次展开“HEKEY_LOCAL_MACHIME/...

(No rating)  1-20-2011    Views: 1027   
article 破解不能使用鼠标右键的网站
   ...

(No rating)  4-23-2012    Views: 930   
article word实用收藏
 

(No rating)  9-18-2013    Views: 752   

用户评语

添加评语
当前还没有评语.


.: .: .: .: .:
[ 登陆 ]
北京护航科技有限公司 2006

Novots Technologies Limited