WPF实现背景灯光随鼠标闪动效果

 更新时间:2020年08月28日 15:08:22   作者:RunnerDNA  
这篇文章主要为大家详细介绍了WPF实现背景灯光随鼠标闪动效果,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下

本文实例为大家分享了WPF实现背景灯光随鼠标闪动的具体代码,供大家参考,具体内容如下

实现效果如下:

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

步骤:

1、窗体xaml

只需放置一个Canvas。

<Canvas x:Name="container" Width="400" Height="400"></Canvas>

2、交互逻辑

/// <summary>
/// MainWindow.xaml 的交互逻辑
/// </summary>
 public partial class MainWindow : Window
 {
  private Point lastMousePosition = new Point(0, 0);//鼠标位置
  private int triangleLength = 100;//三角形边长
 
  public MainWindow()
  {
   InitializeComponent();
   this.Loaded += MainWindow_Loaded;
   CompositionTarget.Rendering += UpdateTriangle;
   this.container.PreviewMouseMove += UpdateLastMousePosition;
  }
 
  private void MainWindow_Loaded(object sender, RoutedEventArgs e)
  {
   //将长方形容易划分成组合三角形
   int horizontalCount = (int)(this.container.ActualWidth / triangleLength);
   int verticalCount = (int)(this.container.ActualHeight / triangleLength);
   for (int i = 0; i < horizontalCount; i++)
   {
    for (int j = 0; j < verticalCount; j++)
    {
     Path trianglePath1 = new Path();
     var g1 = new StreamGeometry();
     using (StreamGeometryContext context = g1.Open())
     {
      context.BeginFigure(new Point(i * triangleLength, j * triangleLength), true, true);
      context.LineTo(new Point(i * triangleLength, (j + 1) * triangleLength), true, false);
      context.LineTo(new Point((i + 1) * triangleLength, (j + 1) * triangleLength), true, false);
     }
     trianglePath1.Data = g1;
     trianglePath1.Fill = new SolidColorBrush(Color.FromArgb(255, 247, 18, 65));
     this.container.Children.Add(trianglePath1);
 
     Path trianglePath2 = new Path();
     var g2 = new StreamGeometry();
     using (StreamGeometryContext context = g2.Open())
     {
      context.BeginFigure(new Point(i * triangleLength, j * triangleLength), true, true);
      context.LineTo(new Point((i + 1) * triangleLength, j * triangleLength), true, false);
      context.LineTo(new Point((i + 1) * triangleLength, (j + 1) * triangleLength), true, false);
     }
     trianglePath2.Data = g2;
     trianglePath2.Fill = new SolidColorBrush(Color.FromArgb(255, 247, 18, 65));
     this.container.Children.Add(trianglePath2);
    }
   }
  }
 
  private void UpdateTriangle(object sender, EventArgs e)
  {
   //获取子控件
   List<Path> childList = GetChildObjects<Path>(this.container);
   for (int i = 0; i < childList.Count; i++)
   {
    for (int j = 1; j < childList.Count; j++)
    {
     string si = childList[i].Data.ToString();
     string si1 = MidStrEx(si, "M", "L");
     string si2 = MidStrEx(si, "L", " ");
     string si3 = MidStrEx(si, " ", "z");
     string sj = childList[j].Data.ToString();
     string sj1 = MidStrEx(sj, "M", "L");
     string sj2 = MidStrEx(sj, "L", " ");
     string sj3 = MidStrEx(sj, " ", "z");
     //左右三角形判断
     if (si1 == sj1 && si3 == sj3)
     {
      double x = childList[i].Data.Bounds.X + (1 - Math.Pow(2, 0.5) / 2) * triangleLength - lastMousePosition.X;
      double y = childList[i].Data.Bounds.Y + (1 - Math.Pow(2, 0.5) / 2) * triangleLength - lastMousePosition.Y;
      double rRadio = 1 - Math.Pow(x * x + y * y, 0.5) / Math.Pow(this.container.ActualWidth * this.container.ActualWidth + this.container.ActualHeight * this.container.ActualHeight, 0.5);
      childList[j].Fill = new SolidColorBrush(Color.FromArgb((byte)(255 * rRadio), 247, 18, 65));
      x = childList[j].Data.Bounds.TopRight.X - (1 - Math.Pow(2, 0.5) / 2) * triangleLength - lastMousePosition.X;
      rRadio = 1 - Math.Pow(x * x + y * y, 0.5) / Math.Pow(this.container.ActualWidth * this.container.ActualWidth + this.container.ActualHeight * this.container.ActualHeight, 0.5);
      childList[i].Fill = new SolidColorBrush(Color.FromArgb((byte)(255 * rRadio), 247, 18, 65));
      break;
     }
    }
   }
  }
 
  private void UpdateLastMousePosition(object sender, MouseEventArgs e)
  {
   lastMousePosition = e.GetPosition(this.container);
  }
 
  /// <summary>
  /// 获得所有子控件
  /// </summary>
  private List<T> GetChildObjects<T>(System.Windows.DependencyObject obj) where T : System.Windows.FrameworkElement
  {
   System.Windows.DependencyObject child = null;
   List<T> childList = new List<T>();
   for (int i = 0; i < VisualTreeHelper.GetChildrenCount(obj); i++)
   {
    child = VisualTreeHelper.GetChild(obj, i);
    if (child is T)
    {
     childList.Add((T)child);
    }
    childList.AddRange(GetChildObjects<T>(child));
   }
   return childList;
  }
 
  /// <summary>
  /// 截取两个指定字符中间的字符串
  /// </summary>
  public static string MidStrEx(string sourse, string startstr, string endstr)
  {
   string result = string.Empty;
   int startindex, endindex;
   try
   {
    startindex = sourse.IndexOf(startstr);
    if (startindex == -1)
     return result;
    string tmpstr = sourse.Substring(startindex + startstr.Length);
    endindex = tmpstr.IndexOf(endstr);
    if (endindex == -1)
     return result;
    result = tmpstr.Remove(endindex);
   }
   catch (Exception ex)
   {
   }
   return result;
  }
}

说明:当组合三角形过多时,会有明显卡顿,需要优化色彩更新方法。

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。

您可能感兴趣的文章:

相关文章

  • c#实现从字符串数组中把数字的元素找出来

    c#实现从字符串数组中把数字的元素找出来

    下面小编就为大家分享一篇c#实现从字符串数组中把数字的元素找出来的方法,希望对大家有所帮助。一起跟随小编过来看看吧
    2017-12-12
  • c# 解决IIS写Excel的权限问题

    c# 解决IIS写Excel的权限问题

    使用以上方法必须对dcom进行配置,给用户使用office的权限
    2012-10-10
  • C#中数据类型的转换介绍

    C#中数据类型的转换介绍

    大家好,本篇文章主要讲的是C#中数据类型的转换介绍,感兴趣的同学赶快来看一看吧,对你有帮助的话记得收藏一下,方便下次浏览
    2022-01-01
  • c#实现摄像头拍照功能示例

    c#实现摄像头拍照功能示例

    这篇文章主要介绍了c#实现摄像头拍照功能示例,需要的朋友可以参考下
    2014-04-04
  • C# Timer控件学习之使用Timer解决按钮幂等性问题

    C# Timer控件学习之使用Timer解决按钮幂等性问题

    Timer控件又称定时器控件或计时器控件,该控件的主要作用是按一定的时间间隔周期性地触发一个名为Tick的事件,因此在该事件的代码中可以放置一些需要每隔一段时间重复执行的程序段,这篇文章主要介绍了关于C#使用Timer解决按钮幂等性问题的相关资料,需要的朋友可以参考下
    2022-10-10
  • C#中LINQ的Select与SelectMany函数使用

    C#中LINQ的Select与SelectMany函数使用

    这篇文章主要介绍了C#中LINQ的Select与SelectMany函数使用,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教
    2022-08-08
  • Unity实现OCR文字识别功能

    Unity实现OCR文字识别功能

    这篇文章主要介绍了通过Unity接入百度AI接口,实现OCR文字识别功能,文中的实现步骤讲解详细,对我们学习或工作有一定的参考价值,需要的可以了解一下
    2022-01-01
  • C#实现过滤sql特殊字符的方法集合

    C#实现过滤sql特殊字符的方法集合

    这篇文章主要介绍了C#实现过滤sql特殊字符的方法,以实例形式分析总结了C#针对SQL危险字符的几种常用的过滤技巧,非常具有实用价值,需要的朋友可以参考下
    2015-11-11
  • C# 嵌入dll 的方法

    C# 嵌入dll 的方法

    这篇文章主要介绍了C# 嵌入dll 的方法,本文图文并茂给大家及时的非常详细,具有一定的参考借鉴价值,需要的朋友可以参考下
    2018-07-07
  • 设计模式速记

    设计模式速记

    本文主要介绍了设计模式:创建型模式;结构型模式;行为型模式三大类。具有很好的参考价值,相信有助于大家记忆与学习,下面跟着小编一起来看下吧
    2017-02-02

最新评论