1. 程式人生 > 程式設計 >WPF實現背景燈光隨滑鼠閃動效果

WPF實現背景燈光隨滑鼠閃動效果

本文例項為大家分享了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),false);
      context.LineTo(new Point((i + 1) * triangleLength,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,true);
      context.LineTo(new Point((i + 1) * triangleLength,false);
     }
     trianglePath2.Data = g2;
     trianglePath2.Fill = new SolidColorBrush(Color.FromArgb(255,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,"L");
     string sj2 = MidStrEx(sj," ");
     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),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);
      childList[i].Fill = new SolidColorBrush(Color.FromArgb((byte)(255 * rRadio),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;
  }
}

說明:當組合三角形過多時,會有明顯示卡頓,需要優化色彩更新方法。

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支援我們。