别名问题

发布于 2024-09-29 13:25:57 字数 1719 浏览 3 评论 0原文

下面的代码在画布上绘制两条垂直线。这些线在屏幕上看起来粗细不同,尽管它们在代码中是相同的。我正在寻找一种方法,使它们看起来像画布周围的边框一样清晰。设置 Path.SnapsToDevicePixels 没有任何效果。该代码是一个人为的示例,通常绘制这些线条的画布可以嵌套在可视化树的更深处。

感谢您的帮助 康斯坦丁


<Window x:Class="wpfapp.MyWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
  <Grid>
    <Border BorderBrush="Black"
            BorderThickness="1"
            Margin="10">
      <Canvas x:Name="Canvas"
              SizeChanged="OnCanvasSizeChanged" />
    </Border>
  </Grid>
</Window>

using System.Windows;
using System.Windows.Media;
using System.Windows.Shapes;

namespace wpfapp
{
    public partial class MyWindow : Window
    {
        public MyWindow()
        {
            InitializeComponent();
        }

        private void OnCanvasSizeChanged(object sender, SizeChangedEventArgs e)
        {
            StreamGeometry g = new StreamGeometry();
            double h = this.Canvas.ActualHeight;

            using (StreamGeometryContext c = g.Open())
            {
                c.BeginFigure(new Point(7, 0), false, false);
                c.LineTo(new Point(7, h), true, false);

                c.BeginFigure(new Point(14, 0), false, false);
                c.LineTo(new Point(14, h), true, false);
            }
            g.Freeze();

            Path p = new Path();

            p.Data = g;
            p.SnapsToDevicePixels = true;
            p.Stroke = new SolidColorBrush(Colors.Black);
            p.StrokeThickness = 1;

            this.Canvas.Children.Clear();
            this.Canvas.Children.Add(p);
        }
    }
}

the code below draws two vertical lines on a canvas. these lines appear to be of different thickness on the screen although they are the same in code. i am tying to find a way to make them look as sharp as the border around the canvas. setting Path.SnapsToDevicePixels does not have any effect. The code is a contrived example, and in general the canvas that plots these lines can be nested deeper inside the visual tree.

thanks for any help
konstantin


<Window x:Class="wpfapp.MyWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
  <Grid>
    <Border BorderBrush="Black"
            BorderThickness="1"
            Margin="10">
      <Canvas x:Name="Canvas"
              SizeChanged="OnCanvasSizeChanged" />
    </Border>
  </Grid>
</Window>

using System.Windows;
using System.Windows.Media;
using System.Windows.Shapes;

namespace wpfapp
{
    public partial class MyWindow : Window
    {
        public MyWindow()
        {
            InitializeComponent();
        }

        private void OnCanvasSizeChanged(object sender, SizeChangedEventArgs e)
        {
            StreamGeometry g = new StreamGeometry();
            double h = this.Canvas.ActualHeight;

            using (StreamGeometryContext c = g.Open())
            {
                c.BeginFigure(new Point(7, 0), false, false);
                c.LineTo(new Point(7, h), true, false);

                c.BeginFigure(new Point(14, 0), false, false);
                c.LineTo(new Point(14, h), true, false);
            }
            g.Freeze();

            Path p = new Path();

            p.Data = g;
            p.SnapsToDevicePixels = true;
            p.Stroke = new SolidColorBrush(Colors.Black);
            p.StrokeThickness = 1;

            this.Canvas.Children.Clear();
            this.Canvas.Children.Add(p);
        }
    }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

携余温的黄昏 2024-10-06 13:25:57

需要使用GuidelineSet:


        protected override void OnRender(DrawingContext c)
        {
            base.OnRender(c);

            Pen pen = new Pen(Brushes.Black, 1);
            double h = this.ActualHeight;
            double d = pen.Thickness / 2;

            foreach (double x in new double[] { 7, 14 })
            {
                GuidelineSet g = new GuidelineSet(new double[] { x + d },
                                                  new double[] { 0 + d, h + d });

                c.PushGuidelineSet(g);
                c.DrawLine(pen, new Point(x, 0), new Point(x, h));
                c.Pop();
            }
        }

need to use GuidelineSet:


        protected override void OnRender(DrawingContext c)
        {
            base.OnRender(c);

            Pen pen = new Pen(Brushes.Black, 1);
            double h = this.ActualHeight;
            double d = pen.Thickness / 2;

            foreach (double x in new double[] { 7, 14 })
            {
                GuidelineSet g = new GuidelineSet(new double[] { x + d },
                                                  new double[] { 0 + d, h + d });

                c.PushGuidelineSet(g);
                c.DrawLine(pen, new Point(x, 0), new Point(x, h));
                c.Pop();
            }
        }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文