利用WPF制作一个背景灯光随鼠标闪动效果

  介绍

这期内容当中小编将会给大家带来有关利用WPF制作一个背景灯光随鼠标闪动效果,文章内容丰富且以专业的角度为大家分析和叙述,阅读完这篇文章希望大家可以有所收获。

实现效果如下:

利用WPF制作一个背景灯光随鼠标闪动效果

<强>思路:将容器分割成组合三角形路径,鼠标移动时更新每个三角形的填充颜色。

<强>步骤:

1,窗体xaml

只需放置一个画布。

& lt;帆布x: Name=癱ontainer"宽度=?00”;身高=?00“祝辞& lt;/Canvas>

2,交互逻辑

///& lt; summary>///主窗口。xaml的交互逻辑///& lt;/summary>
  公共部分类主窗口:窗口
  {
  私人点lastMousePosition=new点(0,0);//鼠标位置
  私人int triangleLength=100;//三角形边长
  
  公共主窗口()
  {
  InitializeComponent ();
  这一点。+=MainWindow_Loaded加载;
  CompositionTarget。呈现+=UpdateTriangle;
  this.container。PreviewMouseMove +=UpdateLastMousePosition;
  }
  
  私人空间MainWindow_Loaded(对象发送方,RoutedEventArgs e)
  {//将长方形容易划分成组合三角形
  int horizontalCount=(int) (this.container。ActualWidth/triangleLength);
  int verticalCount=(int) (this.container。ActualHeight/triangleLength);
  for (int i=0;我& lt;horizontalCount;我+ +)
  {
  for (int j=0;j & lt;verticalCount;j + +)
  {
  路径trianglePath2=new路径();
  var g1=new StreamGeometry ();
  使用(StreamGeometryContext上下文=g1.Open ())
  {
  上下文。BeginFigure(新点(我* triangleLength j * triangleLength),真的,真的);
  上下文。画线(新点(我* triangleLength (j + 1) * triangleLength),真的,假);
  上下文。画线(新点((i + 1) * triangleLength (j + 1) * triangleLength),真的,假);
  }
  trianglePath2。数据=https://www.yisu.com/zixun/g1;
  trianglePath2。填补=new SolidColorBrush(颜色。FromArgb (255、247、65));
  this.container.Children.Add (trianglePath2);
  
  路径trianglePath3=new路径();
  var g2=new StreamGeometry ();
  使用(StreamGeometryContext上下文=g2.Open ())
  {
  上下文。BeginFigure(新点(我* triangleLength j * triangleLength),真的,真的);
  上下文。画线(新点((i + 1) * triangleLength j * triangleLength),真的,假);
  上下文。画线(新点((i + 1) * triangleLength (j + 1) * triangleLength),真的,假);
  }
  trianglePath3。数据=g2;
  trianglePath3。填补=new SolidColorBrush(颜色。FromArgb (255、247、65));
  this.container.Children.Add (trianglePath3);
  }
  }
  }
  
  私人空间UpdateTriangle(对象发送方,EventArgs e)
  {//获取子控件
  列表<路径> childList=GetChildObjects <路径> (this.container);
  for (int i=0;我///<总结///获得所有子控件>/// GetChildObjects  (System.Windows。DependencyObject obj)师:System.Windows.FrameworkElement
  {
  System.Windows。DependencyObject孩子=零;
  列表

利用WPF制作一个背景灯光随鼠标闪动效果