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;
  }
}

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

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

您可能感兴趣的文章:

相关文章

  • 浅析wpf中datagrid显示列的问题

    浅析wpf中datagrid显示列的问题

    这篇文章主要为大家详细介绍了wpf中datagrid显示列问题的相关知识,文中的示例代码讲解详细,具有一定的借鉴价值,有需要的小伙伴可以学习一下
    2024-04-04
  • C#操作IIS程序池及站点的创建配置实现代码

    C#操作IIS程序池及站点的创建配置实现代码

    最近在做一个WEB程序的安装包;对一些操作IIS进行一个简单的总结;主要包括对IIS进行站点的新建以及新建站点的NET版本的选择,还有针对IIS7程序池的托管模式以及版本的操作
    2013-03-03
  • 图解如何使用C#创建Windows服务

    图解如何使用C#创建Windows服务

    本文主要介绍了图解如何使用C#创建Windows服务,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2023-07-07
  • C#用户控件之温度计设计

    C#用户控件之温度计设计

    这篇文章主要为大家详细介绍了C#用户控件之温度计的设计方法,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2018-03-03
  • C#使用windows服务开启应用程序的方法

    C#使用windows服务开启应用程序的方法

    这篇文章主要介绍了C#使用windows服务开启应用程序的方法,实例分析了C#操作windows服务开启应用程序所遇到的问题及相关解决技巧,具有一定参考借鉴价值,需要的朋友可以参考下
    2015-09-09
  • C#生成唯一不重复订单号

    C#生成唯一不重复订单号

    本文给大家介绍的是使用C#生成唯一不重复订单号的方法,主要用到了lock锁,有需要的小伙伴可以参考下。
    2015-07-07
  • C#使用第三方组件生成二维码汇总

    C#使用第三方组件生成二维码汇总

    本文给大家汇总了几种C#使用第三方组件生成二维码的方法以及示例,非常的简单实用,都是项目中经常需要用到的,希望大家能够喜欢
    2016-12-12
  • C#中Dictionary泛型集合7种常见的用法

    C#中Dictionary泛型集合7种常见的用法

    本文主要介绍了Dictionary集合的7种最基础的用法,包括创建、添加、查找、遍历、删除等方法,程序都是由简入繁,希望能通过阅读简单的示例,给大家一些启发。
    2016-03-03
  • C#实现网络通信共享库NetShare的使用示例

    C#实现网络通信共享库NetShare的使用示例

    本文主要介绍了C#实现网络通信共享库NetShare,网络通信共享库NetShare用于保证客户端与服务器通信数据包的规范和统一,感兴趣的可以了解一下
    2023-11-11
  • 浅谈Visual C#进行图像处理(读取、保存以及对像素的访问)

    浅谈Visual C#进行图像处理(读取、保存以及对像素的访问)

    本文主要介绍利用C#对图像进行读取、保存以及对像素的访问等操作,介绍的比较简单,希望对初学者有所帮助。
    2016-04-04

最新评论