翼度科技»论坛 编程开发 .net 查看内容

打造真实感十足的速度表盘:WPF实现动态效果与刻度绘制

11

主题

11

帖子

33

积分

新手上路

Rank: 1

积分
33
 
概述:这个WPF项目通过XAML绘制汽车动态速度表盘,实现了0-300的速度刻度,包括数字、指针,并通过定时器模拟速度变化,展示了动态效果。详细实现包括界面设计、刻度绘制、指针角度计算等,通过C#代码与XAML文件结合完成。

  • 新建 WPF 项目: 在 Visual Studio 中创建一个新的 WPF 项目。
  • 设计界面: 使用 XAML 设计速度表的界面。你可以使用 Canvas 控件来绘制表盘、刻度、指针等。确保设置好布局和样式。
  1. <Window x:
  2. <Canvas x:Name="canvas">
  3.    
  4. </Canvas><Canvas x:Name="canvas">
  5.    
  6. </Canvas>xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  7. <Canvas x:Name="canvas">
  8.    
  9. </Canvas><Canvas x:Name="canvas">
  10.    
  11. </Canvas>xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
  12. <Canvas x:Name="canvas">
  13.    
  14. </Canvas><Canvas x:Name="canvas">
  15.    
  16. </Canvas>Title="Speedometer" Height="400" Width="400">
  17. <Canvas x:Name="canvas">
  18.    
  19. </Canvas><Grid>
  20. <Canvas x:Name="canvas">
  21.    
  22. </Canvas><Canvas x:Name="canvas">
  23.    
  24. </Canvas><Canvas>
  25. <Canvas x:Name="canvas">
  26.    
  27. </Canvas><Canvas x:Name="canvas">
  28.    
  29. </Canvas><Canvas x:Name="canvas">
  30.    
  31. </Canvas>
  32. <Canvas x:Name="canvas">
  33.    
  34. </Canvas><Canvas x:Name="canvas">
  35.    
  36. </Canvas></Canvas>
  37. <Canvas x:Name="canvas">
  38.    
  39. </Canvas></Grid>
  40. </Window>
复制代码

  • 绘制表盘和刻度: 在 Canvas 中使用 Ellipse 绘制表盘,使用 Line 绘制刻度。同时,添加数字标签。
  1. [/code][list=1]
  2. [*][b]实现动态效果[/b]: 在代码文件中,使用定时器或者动画来实现指针的动态变化效果。在 MainWindow.xaml.cs 文件中添加以下代码:
  3. [/list][code]using System;
  4. using System.Windows;
  5. using System.Windows.Media;
  6. using System.Windows.Shapes;
  7. using System.Windows.Threading;
  8. namespace YourNamespace
  9. {
  10. <Canvas x:Name="canvas">
  11.    
  12. </Canvas>public partial class MainWindow : Window
  13. <Canvas x:Name="canvas">
  14.    
  15. </Canvas>{
  16. <Canvas x:Name="canvas">
  17.    
  18. </Canvas><Canvas x:Name="canvas">
  19.    
  20. </Canvas>private double currentSpeed = 0;
  21. <Canvas x:Name="canvas">
  22.    
  23. </Canvas><Canvas x:Name="canvas">
  24.    
  25. </Canvas>private const double MaxSpeed = 300;
  26. <Canvas x:Name="canvas">
  27.    
  28. </Canvas><Canvas x:Name="canvas">
  29.    
  30. </Canvas>private readonly Line speedPointer;
  31. <Canvas x:Name="canvas">
  32.    
  33. </Canvas><Canvas x:Name="canvas">
  34.    
  35. </Canvas>public MainWindow()
  36. <Canvas x:Name="canvas">
  37.    
  38. </Canvas><Canvas x:Name="canvas">
  39.    
  40. </Canvas>{
  41. <Canvas x:Name="canvas">
  42.    
  43. </Canvas><Canvas x:Name="canvas">
  44.    
  45. </Canvas><Canvas x:Name="canvas">
  46.    
  47. </Canvas>InitializeComponent();
  48. <Canvas x:Name="canvas">
  49.    
  50. </Canvas><Canvas x:Name="canvas">
  51.    
  52. </Canvas><Canvas x:Name="canvas">
  53.    
  54. </Canvas>
  55. <Canvas x:Name="canvas">
  56.    
  57. </Canvas><Canvas x:Name="canvas">
  58.    
  59. </Canvas><Canvas x:Name="canvas">
  60.    
  61. </Canvas>// 初始化指针
  62. <Canvas x:Name="canvas">
  63.    
  64. </Canvas><Canvas x:Name="canvas">
  65.    
  66. </Canvas><Canvas x:Name="canvas">
  67.    
  68. </Canvas>speedPointer = new Line
  69. <Canvas x:Name="canvas">
  70.    
  71. </Canvas><Canvas x:Name="canvas">
  72.    
  73. </Canvas><Canvas x:Name="canvas">
  74.    
  75. </Canvas>{
  76. <Canvas x:Name="canvas">
  77.    
  78. </Canvas><Canvas x:Name="canvas">
  79.    
  80. </Canvas><Canvas x:Name="canvas">
  81.    
  82. </Canvas><Canvas x:Name="canvas">
  83.    
  84. </Canvas>X1 = 200,
  85. <Canvas x:Name="canvas">
  86.    
  87. </Canvas><Canvas x:Name="canvas">
  88.    
  89. </Canvas><Canvas x:Name="canvas">
  90.    
  91. </Canvas><Canvas x:Name="canvas">
  92.    
  93. </Canvas>Y1 = 200,
  94. <Canvas x:Name="canvas">
  95.    
  96. </Canvas><Canvas x:Name="canvas">
  97.    
  98. </Canvas><Canvas x:Name="canvas">
  99.    
  100. </Canvas><Canvas x:Name="canvas">
  101.    
  102. </Canvas>Stroke = Brushes.Red,
  103. <Canvas x:Name="canvas">
  104.    
  105. </Canvas><Canvas x:Name="canvas">
  106.    
  107. </Canvas><Canvas x:Name="canvas">
  108.    
  109. </Canvas><Canvas x:Name="canvas">
  110.    
  111. </Canvas>StrokeThickness = 3
  112. <Canvas x:Name="canvas">
  113.    
  114. </Canvas><Canvas x:Name="canvas">
  115.    
  116. </Canvas><Canvas x:Name="canvas">
  117.    
  118. </Canvas>};
  119. <Canvas x:Name="canvas">
  120.    
  121. </Canvas><Canvas x:Name="canvas">
  122.    
  123. </Canvas><Canvas x:Name="canvas">
  124.    
  125. </Canvas>canvas.Children.Add(speedPointer);
  126. <Canvas x:Name="canvas">
  127.    
  128. </Canvas><Canvas x:Name="canvas">
  129.    
  130. </Canvas><Canvas x:Name="canvas">
  131.    
  132. </Canvas>// 使用定时器更新速度
  133. <Canvas x:Name="canvas">
  134.    
  135. </Canvas><Canvas x:Name="canvas">
  136.    
  137. </Canvas><Canvas x:Name="canvas">
  138.    
  139. </Canvas>var timer = new DispatcherTimer { Interval = TimeSpan.FromMilliseconds(100) };
  140. <Canvas x:Name="canvas">
  141.    
  142. </Canvas><Canvas x:Name="canvas">
  143.    
  144. </Canvas><Canvas x:Name="canvas">
  145.    
  146. </Canvas>timer.Tick += Timer_Tick;
  147. <Canvas x:Name="canvas">
  148.    
  149. </Canvas><Canvas x:Name="canvas">
  150.    
  151. </Canvas><Canvas x:Name="canvas">
  152.    
  153. </Canvas>timer.Start();
  154. <Canvas x:Name="canvas">
  155.    
  156. </Canvas><Canvas x:Name="canvas">
  157.    
  158. </Canvas>}
  159. <Canvas x:Name="canvas">
  160.    
  161. </Canvas><Canvas x:Name="canvas">
  162.    
  163. </Canvas>private void Timer_Tick(object sender, EventArgs e)
  164. <Canvas x:Name="canvas">
  165.    
  166. </Canvas><Canvas x:Name="canvas">
  167.    
  168. </Canvas>{
  169. <Canvas x:Name="canvas">
  170.    
  171. </Canvas><Canvas x:Name="canvas">
  172.    
  173. </Canvas><Canvas x:Name="canvas">
  174.    
  175. </Canvas>// 模拟速度变化
  176. <Canvas x:Name="canvas">
  177.    
  178. </Canvas><Canvas x:Name="canvas">
  179.    
  180. </Canvas><Canvas x:Name="canvas">
  181.    
  182. </Canvas>currentSpeed = currentSpeed < MaxSpeed ? currentSpeed + 5 : 0;
  183. <Canvas x:Name="canvas">
  184.    
  185. </Canvas><Canvas x:Name="canvas">
  186.    
  187. </Canvas><Canvas x:Name="canvas">
  188.    
  189. </Canvas>// 更新指针角度
  190. <Canvas x:Name="canvas">
  191.    
  192. </Canvas><Canvas x:Name="canvas">
  193.    
  194. </Canvas><Canvas x:Name="canvas">
  195.    
  196. </Canvas>UpdateSpeedometer();
  197. <Canvas x:Name="canvas">
  198.    
  199. </Canvas><Canvas x:Name="canvas">
  200.    
  201. </Canvas>}
  202. <Canvas x:Name="canvas">
  203.    
  204. </Canvas><Canvas x:Name="canvas">
  205.    
  206. </Canvas>private void UpdateSpeedometer()
  207. <Canvas x:Name="canvas">
  208.    
  209. </Canvas><Canvas x:Name="canvas">
  210.    
  211. </Canvas>{
  212. <Canvas x:Name="canvas">
  213.    
  214. </Canvas><Canvas x:Name="canvas">
  215.    
  216. </Canvas><Canvas x:Name="canvas">
  217.    
  218. </Canvas>// 计算指针角度
  219. <Canvas x:Name="canvas">
  220.    
  221. </Canvas><Canvas x:Name="canvas">
  222.    
  223. </Canvas><Canvas x:Name="canvas">
  224.    
  225. </Canvas>double angle = currentSpeed / MaxSpeed * 270 - 135;
  226. <Canvas x:Name="canvas">
  227.    
  228. </Canvas><Canvas x:Name="canvas">
  229.    
  230. </Canvas><Canvas x:Name="canvas">
  231.    
  232. </Canvas>// 使用 RotateTransform 旋转指针
  233. <Canvas x:Name="canvas">
  234.    
  235. </Canvas><Canvas x:Name="canvas">
  236.    
  237. </Canvas><Canvas x:Name="canvas">
  238.    
  239. </Canvas>var rotateTransform = new RotateTransform(angle);
  240. <Canvas x:Name="canvas">
  241.    
  242. </Canvas><Canvas x:Name="canvas">
  243.    
  244. </Canvas><Canvas x:Name="canvas">
  245.    
  246. </Canvas>speedPointer.RenderTransform = rotateTransform;
  247. <Canvas x:Name="canvas">
  248.    
  249. </Canvas><Canvas x:Name="canvas">
  250.    
  251. </Canvas>}
  252. <Canvas x:Name="canvas">
  253.    
  254. </Canvas>}
  255. }
复制代码
这个例子中,我们使用了一个定时器(DispatcherTimer)来模拟速度的变化,并在定时器的 Tick 事件中更新指针的角度。UpdateSpeedometer 方法根据当前速度计算出指针的角度,并使用 RotateTransform 进行旋转。
确保在 MainWindow.xaml 文件中的 Canvas 中添加了名称为 canvas 的属性:
  1. <Canvas x:Name="canvas">
  2.    
  3. </Canvas>
复制代码
运行效果如:
 
这是一个基本的实例,你可以根据需要进一步优化和扩展,例如添加动画效果、改进界面设计等。
 
源代码获取:https://pan.baidu.com/s/1J4_nbFklHbpqsgfwAfTiIw?pwd=6666
 



来源:https://www.cnblogs.com/hanbing81868164/p/18076722
免责声明:由于采集信息均来自互联网,如果侵犯了您的权益,请联系我们【E-Mail:cb@itdo.tech】 我们会及时删除侵权内容,谢谢合作!

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有账号?立即注册

x

举报 回复 使用道具