WPF實(shí)現(xiàn)背景燈光隨鼠標(biāo)閃動(dòng)效果
本文實(shí)例為大家分享了WPF實(shí)現(xiàn)背景燈光隨鼠標(biāo)閃動(dòng)的具體代碼,供大家參考,具體內(nèi)容如下
實(shí)現(xiàn)效果如下:
思路:將容器分割成組合三角形Path,鼠標(biāo)移動(dòng)時(shí)更新每個(gè)三角形的填充顏色。
步驟:
1、窗體xaml
只需放置一個(gè)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);//鼠標(biāo)位置 private int triangleLength = 100;//三角形邊長(zhǎng) public MainWindow() { InitializeComponent(); this.Loaded += MainWindow_Loaded; CompositionTarget.Rendering += UpdateTriangle; this.container.PreviewMouseMove += UpdateLastMousePosition; } private void MainWindow_Loaded(object sender, RoutedEventArgs e) { //將長(zhǎng)方形容易劃分成組合三角形 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> /// 截取兩個(gè)指定字符中間的字符串 /// </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; } }
說明:當(dāng)組合三角形過多時(shí),會(huì)有明顯卡頓,需要優(yōu)化色彩更新方法。
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
C#基于Linq和反射實(shí)現(xiàn)數(shù)據(jù)持久化框架Xml4DB詳解
在本篇文章里小編給大家整理的是關(guān)于C#基于Linq和反射實(shí)現(xiàn)數(shù)據(jù)持久化框架Xml4DB相關(guān)知識(shí)點(diǎn),有需要的朋友們學(xué)習(xí)下。2019-08-08C#實(shí)現(xiàn)TreeView節(jié)點(diǎn)拖拽的方法
這篇文章主要介紹了C#實(shí)現(xiàn)TreeView節(jié)點(diǎn)拖拽的方法,涉及C#針對(duì)TreeView節(jié)點(diǎn)的動(dòng)態(tài)添加及移除技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下2015-09-09C#動(dòng)態(tài)調(diào)整數(shù)組大小的方法
這篇文章主要介紹了C#動(dòng)態(tài)調(diào)整數(shù)組大小的方法,涉及C#中靜態(tài)方法CreateInstance的使用技巧,非常具有實(shí)用價(jià)值,需要的朋友可以參考下2015-04-04C#操作NPOI實(shí)現(xiàn)Excel數(shù)據(jù)導(dǎo)入導(dǎo)出
這篇文章主要為大家詳細(xì)介紹了C#如何操作NPOI實(shí)現(xiàn)Excel數(shù)據(jù)導(dǎo)入導(dǎo)出功能,文中的示例代碼講解詳細(xì),感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下2023-02-02c#靜態(tài)方法和非靜態(tài)方法詳細(xì)介紹
這篇文章主要介紹了c#靜態(tài)方法和非靜態(tài)方法,需要的朋友可以參考下2014-02-02winform壁紙工具為圖片添加當(dāng)前月的日歷信息
使用用winform做了一個(gè)設(shè)置壁紙小工具,為圖片添加當(dāng)月的日歷并設(shè)為壁紙,可以手動(dòng)/定時(shí)設(shè)置壁紙,最主要的特點(diǎn)是在圖片上生成當(dāng)前月的日歷信息,感興趣的你可以參考下2013-03-03C#語法糖(Csharp Syntactic sugar)大匯總
首先需要聲明的是“語法糖”這個(gè)詞絕非貶義詞,它可以給我?guī)矸奖?,是一種便捷的寫法,編譯器會(huì)幫我們做轉(zhuǎn)換;而且可以提高開發(fā)編碼的效率,在性能上也不會(huì)帶來?yè)p失。這讓java開發(fā)人員羨慕不已,呵呵。2010-06-06