6

我有一个自定义的 WPF 画布,我想在上面显示一个网格。我通过覆盖 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

4

1 回答 1

1

作为原始帖子状态的评论。笔粗细应设置为 1.0/缩放。

于 2010-07-17T09:19:50.490 回答