本文实例为大家分享了WPF实现背景灯光随鼠标闪动的具体代码,供大家参考,具体内容如下
实现效果如下:
思路:将容器分割成组合三角形Path,鼠标移动时更新每个三角形的填充颜色。
步骤:
1、窗体xaml
只需放置一个Canvas。
复制代码
1<Canvas x:Name="container" Width="400" Height="400"></Canvas>
2、交互逻辑
复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132/// <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实现背景灯光随鼠标闪动效果的全部内容,更多相关WPF实现背景灯光随鼠标闪动效果内容请搜索靠谱客的其他文章。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复