WPF canvas、基本图形、path几个示例

1 Canvas和基本形状

<Page
  xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
  <Grid>
        <Canvas Margin="0,0,0,0" Background="White">
            <Rectangle Fill="Orange" 
                Stroke="SeaGreen" 
                StrokeThickness="3"
                Width="250" 
                Height="220" 
                Canvas.Left="150" Canvas.Top="100"/>
            <Ellipse Fill="LimeGreen" 
                Stroke="Green" 
                StrokeThickness="3"
                Width="250" Height="100"
                Panel.ZIndex="1" 
                Canvas.Left="55" Canvas.Top="25"/>
        </Canvas>
    </Grid>
</Page>

 通过 Canvas 元素,可以根据 x 和 y 绝对坐标定位内容。可以在唯一的位置绘制元素;或者,如果多个元素占用了相同的坐标,则这些元素在标记中的出现顺序可决定它们的绘制顺序。

Canvas.Left,Canvas.Top,定义了形状在Canvas中的位置;Stroke,边框颜色;StrokeThickness,边框像素宽度;

WPF中各种颜色名如下;

2 路径和几何元素 

<Page
  xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
  <Grid>
    <Canvas>
        <Path Stroke="SeaGreen" StrokeThickness="5">
            <Path.Data>
                <EllipseGeometry Center="100,100" RadiusX="70" 

RadiusY="30"></EllipseGeometry>
            </Path.Data>
        </Path>
    </Canvas>
    </Grid>
</Page>

 可以在路径中使用几何元素,如EllipseGeometry;

3 线条几何图形

LineGeometry 类:表示线条的几何图形;

<Page
  xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
  <Grid ShowGridLines="True" HorizontalAlignment="Left">
        <Canvas Height="200" Width="200">
        <Path Stroke="SeaGreen" StrokeThickness="5">
            <Path.Data>
                <LineGeometry StartPoint="2,2" EndPoint="50,100"></LineGeometry>
            </Path.Data>
        </Path>
        </Canvas>
   </Grid>
</Page>

4 路径中画贝塞尔曲线 

<Page
  xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
  <Grid>  
    <Path  Stroke="Green" StrokeThickness="3">
            <Path.Data>
                <PathGeometry>
                    <PathFigure StartPoint="10,10">
                        <BezierSegment Point1="220,40" Point2="300,430" Point3="600,330"  

IsSmoothJoin="True"/>
                    </PathFigure>
                </PathGeometry>
            </Path.Data>
    </Path>
  </Grid>
</Page>

 也可以在路径中画贝塞尔曲线;贝塞尔曲线是由控制点定义的;改变控制点坐标,图形如下;

5 利用路径中的点绘制形状 

<Page
  xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
  <Grid>  
        <Path Stroke="Red" Fill="DarkGreen" Data="M 100,100 A 50,30 0 0 0 100,200 A 65 50 0 

0 1 100,300">

        </Path>
  </Grid>
</Page>

 如下代码;
<Path x:Name="path_data" Stroke="#FFE23838" StrokeThickness="1" Data="M 100,0 A 50,100 0 0 0 100,200"></Path>
含义如下;
    M 起始点 (100,0)  A 尺寸(X50,Y100半径)  圆弧旋转角度值(0) 优势弧的标记(否,弧角度小于180)  正负角度标记(0 逆时针画圆)   结束点(100,200)

6 绘制问号和齿轮 

问号:
M 0 0 l 0 4.4 l 10.6 0 a 1.9 1.5 0 0 1 1.9 1.5
l 0 4 a 2 2.3 0 0 1 -2 2.3 l -2.7 0 
a 4.4 5.5 0 0 0 -4.4 5.5 l 0 7.7 l 4.6 0 l 0 -7.0
a 1.8 1.9 0 0 1 1.8 -1.9 l 2.6 0
a 4.5 4.5 0 0 0 4.5 -4.5 l 0 -6.8
a 5.7 5.3 0 0 0 -5.7 -5.3z
m 3.6 28.1 l4.7 0 l 0 4.7 l -4.7 0z

齿轮:
M 15 7.5 a 7.5 7.5 0 0 0 0 15 a 7.5 7.5 0 0 0 0 -15
M 19 0.6 A 15 15 0 0 0 11.2 0.6 L 12.2 3.9 A 11.5 11.5 0 0 0 7.0 6.8 L 4.5 4.3
A 15 15 0 0 0 0.5 11.4 L 3.8 12.2 A 11.5 11.5 0 0 0 3.9 18 L 0.7 19
A 15 15 0 0 0 4.4 25.5 L 6.9 23 A 11.5 11.5 0 0 0 12 26.1 L 11 29.5
A 15 15 0 0 0 18.8 29.4 L 17.8 26.1 A 11.5 11.5 0 0 0 23 23.2 L25.4 25.7
A 15 15 0 0 0 29.5 18.7L 26.1 17.8 A 11.5 11.5 0 0 0 26 11.9L 29.4 11
A 15 15 0 0 0 25.5 4.5 L 23.2 6.9 A 11.5 11.5 0 0 0 18 3.9 Z

 

路径的作用不止是绘制形状;可以跟事件结合;我以前做过,在窗体上绘制手的形状,鼠标点在手形范围之内,不触发任何事件,点在手形范围之外,触发窗体事件;

  • 1
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
### 回答1: 这里是火箭图形的xaml代码:<Ellipse x:Name="rocket" Width="100" Height="100" Fill="Black" /> <Path x:Name="fire" Data="M20,25 L20,45 L50,45 L50,25 Z" Fill="Red" /> ### 回答2: <Window x:Class="RocketWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Title="Rocket Window" Height="450" Width="800"> <Grid> <Canvas> <Path Stroke="Black" StrokeThickness="2" Fill="Gray"> <Path.Data> <PathGeometry> <PathFigure StartPoint="400,400"> <BezierSegment Point1="300,340" Point2="395,290" Point3="390,230"/> </PathFigure> <PathFigure StartPoint="390,230"> <BezierSegment Point1="390,220" Point2="400,150" Point3="400,130"/> </PathFigure> <PathFigure StartPoint="400,130"> <BezierSegment Point1="400,100" Point2="420,110" Point3="410,80"/> </PathFigure> <PathFigure StartPoint="410,80" IsClosed="True"> <LineSegment Point="390,70"/> <LineSegment Point="400,45"/> <LineSegment Point="390,50"/> <LineSegment Point="400,15"/> <LineSegment Point="410,50"/> <LineSegment Point="400,50"/> <LineSegment Point="410,70"/> <LineSegment Point="390,80"/> <LineSegment Point="410,100"/> </PathFigure> </PathGeometry> </Path.Data> </Path> </Canvas> </Grid> </Window> ### 回答3: 当然,以下是一个使用WPF创建火箭图形的XAML代码示例: ```xaml <Window x:Class="RocketGraphics.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Title="Rocket Graphics" Height="500" Width="500"> <Grid> <Canvas> <Path Fill="#FFD81B60"> <Path.Data> <PathGeometry> <PathGeometry.Figures> <PathFigure StartPoint="100,200"> <PolyLineSegment Points="150,100 200,100 250,200 200,400 150,400 100,200"/> </PathFigure> <PathFigure StartPoint="100,200"> <LineSegment Point="15,350"/> <LineSegment Point="100,250"/> </PathFigure> <PathFigure StartPoint="150,100" IsClosed="True"> <LineSegment Point="200,15"/> <LineSegment Point="250,100"/> </PathFigure> </PathGeometry.Figures> </PathGeometry> </Path.Data> </Path> <Path Fill="#FFFF0000"> <Path.Data> <PathGeometry> <PathGeometry.Figures> <PathFigure StartPoint="165,115"> <PolyLineSegment Points="190,30 215,115"/> </PathFigure> </PathGeometry.Figures> </PathGeometry> </Path.Data> </Path> </Canvas> </Grid> </Window> ``` 这段代码会显示一个简单的火箭图形,火箭由三个`Path`元素组成。第一个`Path`元素绘制了火箭的主体部分,使用红色作为填充色(`Fill="#FFD81B60"`)。第二个`Path`元素绘制了火箭的底部火焰部分,使用红色作为填充色(`Fill="#FFFF0000"`)。通过调整坐标可以调整火箭的形状和位置。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值