【问题标题】:WPF Viewbox size changes but PointToScreen of child is always sameWPF Viewbox 大小发生变化,但子项的 PointToScreen 始终相同
【发布时间】:2011-03-05 01:45:48
【问题描述】:

为了处理多种分辨率,我在同一个 xaml 页面中有以下控件。

  • 网格 -> 视图框(拉伸,居中)-> 画布(居中)-> 矩形 X(居中)
  • 框架元素

我正在尝试用黑色覆盖覆盖整个页面,并在页面中心为 Rectangle X 剪切一个矩形。

为了创建黑色覆盖,我将整个页面涂成黑色,然后:

        DrawingVisual dv = new DrawingVisual();

        using (DrawingContext dc = dv.RenderOpen())
        {
            double sw = SystemParameters.PrimaryScreenWidth;
            double sh = SystemParameters.PrimaryScreenHeight;
            PathGeometry cmbg = null;

            RectangleGeometry rectangleFullScreen = new RectangleGeometry(new Rect(0, 0, sw, sh));
            cmbg = Geometry.Combine(Geometry.Empty, rectangleFullScreen, GeometryCombineMode.Union, null);

            RectangleGeometry rectangleHole = new RectangleGeometry(holeRectangle);
            cmbg = Geometry.Combine(cmbg, rectangleHole, GeometryCombineMode.Exclude, null);

            Brush b = new SolidColorBrush(Colors.Black);
            b.Opacity = 0.85;
            dc.DrawGeometry(b, new Pen(), cmbg);
        }

        return dv;

为了计算要排除的矩形,我得到了 RectangleX 的屏幕坐标 (.PointToScreen(new Point(0, 0));)

问题是,如果我修改 Viewbox 的大小(取决于分辨率),RectangleX 会改变它的位置,但 PointToScreen 总是返回相同的屏幕坐标。

在不改变视图框的情况下,孔在正确的位置被切割,就在 RectangleX 上方。

如何获取 RectangleX 的正确坐标以绘制排除矩形?

非常感谢您的帮助。

谢谢

彼得

【问题讨论】:

    标签: c# wpf viewbox


    【解决方案1】:

    我已经为此苦苦挣扎了几天...但是在发布问题 10 分钟后找到了解决方案...

    回答:

    更改 Viewbox 的宽度和最大宽度后,我必须调用 UpdateLayout()

    现在可以了!

    p.

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2020-05-24
      • 1970-01-01
      • 2013-08-23
      • 2013-01-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多