WPF画布 - 单像素网格 [英] WPF Canvas - Single Pixel Grid
问题描述
我有一个自定义WPF Canvas,我想在其上显示一个网格。我这样做是通过重写OnRender方法在Canvas,并使用DrawingContext绘图功能。
I have a custom WPF Canvas, upon which I would like to show a grid. I do so by overriding the OnRender method on Canvas, and using the DrawingContext drawing functions. IsGridVisible, GridWidth, GridHeight are the number of pixels between each grid line horizontally and vertically respectively.
我也使用Canvas.LayoutTransform属性上的ScaleTransform来缩放Canvas项目并且如人们所期望的,网格线厚度乘以ScaleTransform缩放因子,如下图所示。是否有任何方法来绘制单个像素行,不管当前的Canvas RenderTransform?
I also use a ScaleTransform on the Canvas.LayoutTransform property to zoom the Canvas items and as one expects, the grid line thicknesses are multiplied by the ScaleTransform scaling factors as shown in the below image. Is there any way to draw single pixel lines, irrespective of the current Canvas RenderTransform?
protected override void OnRender(System.Windows.Media.DrawingContext dc)
{
base.OnRender(dc);
if (IsGridVisible)
{
// Draw GridLines
Pen pen = new Pen(new SolidColorBrush(GridColour), 1);
pen.DashStyle = DashStyles.Dash;
for (double x = 0; x < this.ActualWidth; x += this.GridWidth)
{
dc.DrawLine(pen, new Point(x, 0), new Point(x, this.ActualHeight));
}
for (double y = 0; y < this.ActualHeight; y += this.GridHeight)
{
dc.DrawLine(pen, new Point(0, y), new Point(this.ActualWidth, y));
}
}
}
推荐答案
到原来的状态。钢笔厚度应设置为1.0 /缩放。
As the comments to the original post state. The Pen thickness should be set to 1.0/zoom.
这篇关于WPF画布 - 单像素网格的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!