经验首页 前端设计 程序设计 Java相关 移动开发 数据库/运维 软件/图像 大数据/云计算 其他经验
当前位置:技术经验 » 程序设计 » ASP.net » 查看文章
WPF基础:在Canvas上绘制图形
来源:cnblogs  作者:mingupupup  时间:2024/4/17 15:32:09  对本文有异议

Canvas介绍

CanvasWPF(Windows Presentation Foundation)中的一种面板控件,用于在XAML中布置子元素。它提供了绝对定位的能力,允许元素在自由的二维空间中放置。Canvas上的子元素可以通过指定绝对位置(Left和Top属性)来放置,也可以使用附加属性来指定相对于Canvas的位置。Canvas对于需要自由布局的场景非常有用,例如绘图应用程序或需要精确放置UI元素的情况。但是,使用Canvas布局时要注意,它不会自动调整子元素的位置或大小,因此需要手动管理子元素的布局。

image-20240416085443415

在Canvas上绘制矩形

在xaml定义一个Canvas:

  1. <StackPanel>
  2. <hc:Row Margin="0,20,0,0">
  3. <hc:Col Span="8">
  4. <Label Content="画矩形"></Label>
  5. </hc:Col>
  6. <hc:Col Span="8">
  7. <Button Style="{StaticResource ButtonPrimary}" Content="开始"
  8. Click="Button_Click_DrawRect"/>
  9. </hc:Col>
  10. <hc:Col Span="8">
  11. <Button Style="{StaticResource ButtonPrimary}" Content="清空"
  12. Click="Button_Click_Clear"/>
  13. </hc:Col>
  14. </hc:Row>
  15. <Canvas Background="Azure" x:Name="myCanvas1" Height="400">
  16. </Canvas>
  17. </StackPanel>

效果如下所示:

image-20240416085838561

绘制矩形:

  1. System.Windows.Shapes.Rectangle rectangle = new System.Windows.Shapes.Rectangle
  2. {
  3. Width = 100,
  4. Height = 100,
  5. Stroke = System.Windows.Media.Brushes.Blue,
  6. StrokeThickness = 1,
  7. };
  8. Canvas.SetLeft(rectangle, 50);
  9. Canvas.SetTop(rectangle, 50);
  10. myCanvas1.Children.Add(rectangle);

System.Windows.Shapes.Rectangle

System.Windows.Shapes.RectangleWPF(Windows Presentation Foundation)中的一个类,它表示一个矩形图形。

image-20240416093429075

以下是Rectangle类的一些主要属性:

属性名 类型 描述
Width Double 获取或设置元素的宽度。
Height Double 获取或设置元素的建议高度。
Stroke Brush 获取或设置 Brush,用于指定 Shape 边框绘制的方式。
StrokeThickness Double 获取或设置 Shape边框的宽度。
Fill Brush 获取或设置 Brush,它指定形状内部上色的方式。
  1. Canvas.SetLeft(rectangle, 50);
  2. Canvas.SetTop(rectangle, 50);

这两行代码是在设置Rectangle对象在Canvas中的位置。

  1. Canvas.SetLeft(rectangle, 50);:这行代码设置了rectangle对象在Canvas中的左边距。SetLeft是一个静态方法,它接受两个参数:第一个参数是要设置位置的对象,第二个参数是左边距的值。在这个例子中,rectangle对象的左边距被设置为50像素。
  2. Canvas.SetTop(rectangle, 50);:这行代码设置了rectangle对象在Canvas中的上边距。SetTop也是一个静态方法,它的工作方式与SetLeft相同,只是它设置的是上边距而不是左边距。在这个例子中,rectangle对象的上边距被设置为50像素。
  1. myCanvas1.Children.Add(rectangle);

这行代码将矩形添加到Canvas中。myCanvas1是Canvas的名称,Children.Add方法将矩形添加到Canvas的子元素中。

实现效果:

image-20240416094336410

也可以直接在xaml中写:

  1. <Canvas Background="Azure" x:Name="myCanvas1" Height="400">
  2. <Rectangle Width="100" Height="100" Canvas.Left="50" Canvas.Top="50" Stroke="Blue" StrokeThickness="1"/>
  3. </Canvas>

效果与上述相同。

在Canvas上绘制圆

xaml写法:

  1. <Canvas Background="Azure" x:Name="myCanvas1" Height="400">
  2. <Ellipse Width="100" Height="100" Fill="Blue" Canvas.Left="50" Canvas.Top="50"/>
  3. </Canvas>

实现效果:

image-20240416094913617

cs写法:

  1. System.Windows.Shapes.Ellipse ellipse = new System.Windows.Shapes.Ellipse
  2. {
  3. Width = 100,
  4. Height = 100,
  5. Fill = System.Windows.Media.Brushes.Blue
  6. };
  7. Canvas.SetLeft(ellipse, 50);
  8. Canvas.SetTop(ellipse, 50);
  9. myCanvas1.Children.Add(ellipse);

实现效果与上述相同。

在Canvas上绘制折线

xaml写法:

  1. <Canvas Background="Azure" x:Name="myCanvas1" Height="400">
  2. <Polyline Points="10,10 50,50 100,20 150,70" Stroke="Blue" StrokeThickness="2"/>
  3. </Canvas>

实现效果:

image-20240416095915008

cs写法:

  1. // 创建Polyline对象
  2. Polyline polyline = new Polyline();
  3. polyline.Points = new PointCollection()
  4. {
  5. new System.Windows.Point(10, 10),
  6. new System.Windows.Point(50, 50),
  7. new System.Windows.Point(100, 20),
  8. new System.Windows.Point(150, 70)
  9. };
  10. polyline.Stroke = System.Windows.Media.Brushes.Blue;
  11. polyline.StrokeThickness = 2;
  12. myCanvas1.Children.Add(polyline);

实现效果与上述相同。

在Canvas上绘制多边形

xaml写法:

  1. <Canvas Background="Azure" x:Name="myCanvas1" Height="400">
  2. <Polygon Points="350,200 250,100 300,250 " Fill="Red" Stroke="Blue" StrokeThickness="2"/>
  3. </Canvas>

实现效果:

image-20240416101250384

cs写法:

  1. // 创建Polygon对象
  2. Polygon polygon = new Polygon();
  3. polygon.Points = new PointCollection()
  4. {
  5. new System.Windows.Point(350, 200),
  6. new System.Windows.Point(250, 100),
  7. new System.Windows.Point(300, 250)
  8. };
  9. polygon.Fill = System.Windows.Media.Brushes.Red;
  10. polygon.Stroke = System.Windows.Media.Brushes.Blue;
  11. polygon.StrokeThickness = 2;
  12. myCanvas1.Children.Add(polygon);

实现效果与上述相同。

在Canvas上绘制自定义路径

xaml写法:

  1. <Canvas Background="Azure" x:Name="myCanvas1" Height="400">
  2. <Path Stroke="Blue" StrokeThickness="2">
  3. <Path.Data>
  4. <PathGeometry>
  5. <PathFigure StartPoint="10,10">
  6. <LineSegment Point="50,50"/>
  7. <LineSegment Point="100,20"/>
  8. <LineSegment Point="150,70"/>
  9. </PathFigure>
  10. </PathGeometry>
  11. </Path.Data>
  12. </Path>
  13. </Canvas>

实现效果:

image-20240416101923692

cs写法:

  1. // 创建Path对象
  2. Path path = new Path();
  3. path.Stroke = System.Windows.Media.Brushes.Blue;
  4. path.StrokeThickness = 2;
  5. // 创建PathGeometry对象
  6. PathGeometry pathGeometry = new PathGeometry();
  7. // 创建PathFigure对象
  8. PathFigure pathFigure = new PathFigure();
  9. pathFigure.StartPoint = new System.Windows.Point(10, 10);
  10. // 创建LineSegment对象并添加到PathFigure
  11. pathFigure.Segments.Add(new LineSegment(new System.Windows.Point(50, 50), true));
  12. pathFigure.Segments.Add(new LineSegment(new System.Windows.Point(100, 20), true));
  13. pathFigure.Segments.Add(new LineSegment(new System.Windows.Point(150, 70), true));
  14. // 将PathFigure添加到PathGeometry
  15. pathGeometry.Figures.Add(pathFigure);
  16. // 设置Path的Data属性为PathGeometry对象
  17. path.Data = pathGeometry;
  18. // 将path添加到myCanvas1中
  19. myCanvas1.Children.Add(path);

实现效果与上述相同。

原文链接:https://www.cnblogs.com/mingupupu/p/18137691

 友情链接:直通硅谷  点职佳  北美留学生论坛

本站QQ群:前端 618073944 | Java 606181507 | Python 626812652 | C/C++ 612253063 | 微信 634508462 | 苹果 692586424 | C#/.net 182808419 | PHP 305140648 | 运维 608723728

W3xue 的所有内容仅供测试,对任何法律问题及风险不承担任何责任。通过使用本站内容随之而来的风险与本站无关。
关于我们  |  意见建议  |  捐助我们  |  报错有奖  |  广告合作、友情链接(目前9元/月)请联系QQ:27243702 沸活量
皖ICP备17017327号-2 皖公网安备34020702000426号