gpt4 book ai didi

c# - WPF 在 Canvas 面板背景上绘制彩色网格线

转载 作者:太空狗 更新时间:2023-10-29 23:07:56 27 4
gpt4 key购买 nike

类似于方格纸的样子,我想在 Canvas 的背景上绘制不同颜色的直线,形成一个网格。以下代码仅适用于绘制红线。我还想画一些蓝线和灰线。这意味着我还需要两组线,到目前为止,我还无法解决绘制多组附加颜色的新线的问题。

<Window x:Class="GridTest.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="700" Width="1000">

<ScrollViewer HorizontalScrollBarVisibility="Visible">
<Canvas Width="10000" Height="10000">
<Canvas.Background>
<DrawingBrush Stretch="None" TileMode="Tile"
Viewport="0,0 100,100" ViewportUnits="Absolute">
<DrawingBrush.Drawing>
<GeometryDrawing>
<GeometryDrawing.Geometry>
<GeometryGroup>
<LineGeometry StartPoint="0,0" EndPoint="0,100"/>
<LineGeometry StartPoint="0,0" EndPoint="100,0"/>
</GeometryGroup>
</GeometryDrawing.Geometry>
<GeometryDrawing.Pen>
<Pen Thickness="1" Brush="Red"/>
</GeometryDrawing.Pen>
</GeometryDrawing>
</DrawingBrush.Drawing>
</DrawingBrush>
</Canvas.Background>
</Canvas>
</ScrollViewer>

</Window>

重申一下...此代码正在执行我想要的单色操作。但我还想添加不同颜色的线条。

最佳答案

您可以向根 Canvas 添加多个 Canvas ,每个 Canvas 都有自己的背景笔:

<ScrollViewer HorizontalScrollBarVisibility="Visible">
<Canvas Width="10000" Height="10000">
<Canvas Width="10000" Height="10000">
<Canvas.Background>
<DrawingBrush Stretch="None" TileMode="Tile"
Viewport="0,0 10,10" ViewportUnits="Absolute">
<DrawingBrush.Drawing>
<GeometryDrawing>
<GeometryDrawing.Geometry>
<GeometryGroup>
<LineGeometry StartPoint="0,0" EndPoint="0,10"/>
<LineGeometry StartPoint="0,0" EndPoint="10,0"/>
</GeometryGroup>
</GeometryDrawing.Geometry>
<GeometryDrawing.Pen>
<Pen Thickness="1" Brush="DarkGray"/>
</GeometryDrawing.Pen>
</GeometryDrawing>
</DrawingBrush.Drawing>
</DrawingBrush>
</Canvas.Background>
</Canvas>
<Canvas Width="10000" Height="10000">
<Canvas.Background>
<DrawingBrush Stretch="None" TileMode="Tile"
Viewport="0,0 50,50" ViewportUnits="Absolute">
<DrawingBrush.Drawing>
<GeometryDrawing>
<GeometryDrawing.Geometry>
<GeometryGroup>
<LineGeometry StartPoint="0,0" EndPoint="0,50"/>
<LineGeometry StartPoint="0,0" EndPoint="50,0"/>
</GeometryGroup>
</GeometryDrawing.Geometry>
<GeometryDrawing.Pen>
<Pen Thickness="1" Brush="Blue"/>
</GeometryDrawing.Pen>
</GeometryDrawing>
</DrawingBrush.Drawing>
</DrawingBrush>
</Canvas.Background>
</Canvas>
<Canvas Width="10000" Height="10000">
<Canvas.Background>
<DrawingBrush Stretch="None" TileMode="Tile"
Viewport="0,0 100,100" ViewportUnits="Absolute">
<DrawingBrush.Drawing>
<GeometryDrawing>
<GeometryDrawing.Geometry>
<GeometryGroup>
<LineGeometry StartPoint="0,0" EndPoint="0,100"/>
<LineGeometry StartPoint="0,0" EndPoint="100,0"/>
</GeometryGroup>
</GeometryDrawing.Geometry>
<GeometryDrawing.Pen>
<Pen Thickness="1" Brush="Red"/>
</GeometryDrawing.Pen>
</GeometryDrawing>
</DrawingBrush.Drawing>
</DrawingBrush>
</Canvas.Background>
</Canvas>
</Canvas>
</ScrollViewer>

或者,您可以使用 VisualBrush , 它从任何 UIElement 中构造一个画笔。因此,您可以使用内部包含 RectanglesGrid 或任何类似方法制作 Canvas 背景:

<Canvas> 
<Canvas.Background>
<VisualBrush>
<VisualBrush.Visual>
<Grid>
<Rectangle Width="10000" Height="10000">
<Rectangle.Fill>
<DrawingBrush Stretch="None" TileMode="Tile"
Viewport="0,0 10,10" ViewportUnits="Absolute">
<DrawingBrush.Drawing>
<GeometryDrawing>
<GeometryDrawing.Geometry>
<GeometryGroup>
<LineGeometry StartPoint="0,0" EndPoint="0,10"/>
<LineGeometry StartPoint="0,0" EndPoint="10,0"/>
</GeometryGroup>
</GeometryDrawing.Geometry>
<GeometryDrawing.Pen>
<Pen Thickness="1" Brush="DarkGray"/>
</GeometryDrawing.Pen>
</GeometryDrawing>
</DrawingBrush.Drawing>
</DrawingBrush>
</Rectangle.Fill>
</Rectangle>
<Rectangle Width="10000" Height="10000">
<Rectangle.Fill>
<DrawingBrush Stretch="None" TileMode="Tile"
Viewport="0,0 50,50" ViewportUnits="Absolute">
<DrawingBrush.Drawing>
<GeometryDrawing>
<GeometryDrawing.Geometry>
<GeometryGroup>
<LineGeometry StartPoint="0,0" EndPoint="0,50"/>
<LineGeometry StartPoint="0,0" EndPoint="50,0"/>
</GeometryGroup>
</GeometryDrawing.Geometry>
<GeometryDrawing.Pen>
<Pen Thickness="1" Brush="Blue"/>
</GeometryDrawing.Pen>
</GeometryDrawing>
</DrawingBrush.Drawing>
</DrawingBrush>
</Rectangle.Fill>
</Rectangle>
<Rectangle Width="10000" Height="10000">
<Rectangle.Fill>
<DrawingBrush Stretch="None" TileMode="Tile"
Viewport="0,0 100,100" ViewportUnits="Absolute">
<DrawingBrush.Drawing>
<GeometryDrawing>
<GeometryDrawing.Geometry>
<GeometryGroup>
<LineGeometry StartPoint="0,0" EndPoint="0,100"/>
<LineGeometry StartPoint="0,0" EndPoint="100,0"/>
</GeometryGroup>
</GeometryDrawing.Geometry>
<GeometryDrawing.Pen>
<Pen Thickness="1" Brush="Red"/>
</GeometryDrawing.Pen>
</GeometryDrawing>
</DrawingBrush.Drawing>
</DrawingBrush>
</Rectangle.Fill>
</Rectangle>
</Grid>
</VisualBrush.Visual>
</VisualBrush>
</Canvas.Background>
</Canvas>

关于c# - WPF 在 Canvas 面板背景上绘制彩色网格线,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20748651/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com