我有一个自定义的 WPF Canvas,我想在其上显示一个网格。我通过重写 Canvas 上的 OnRender 方法并使用 DrawingContext 绘图函数来实现这一点。 IsGridVisible、GridWidth、GridHeight 分别是水平和垂直方向上每条网格线之间的像素数。
我还在 Canvas.LayoutTransform 属性上使用 ScaleTransform 来缩放 Canvas 项目,正如人们所期望的那样,网格线粗细乘以 ScaleTransform 缩放因子,如下图所示。有什么方法可以绘制单像素线,而不管当前的 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));
}
}
}
替代文本 http://www.freeimagehosting.net/uploads/f05ad1f602.png http://www.freeimagehosting.net/uploads/f05ad1f602.png
正如对原帖状态的评论。笔粗细应设置为 1.0/缩放。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)