【问题标题】:Drawing vertical text in WPF using DrawingContext.DrawText()使用 DrawingContext.DrawText() 在 WPF 中绘制垂直文本
【发布时间】:2012-01-11 06:49:24
【问题描述】:

我正在使用 WPF 中的 DrawingContext 进行一些自定义绘图。我正在使用DrawingContext.DrawText 绘制字符串。现在,在我想垂直绘制文本的地方。 DrawingContext OR DrawText() 函数中是否有任何选项可以垂直绘制文本?

【问题讨论】:

    标签: c# .net wpf drawing


    【解决方案1】:

    这是我的解决方案:需要围绕文本原点创建旋转变换,因此我们将 x 和 y 传递给 RotateTransform 构造函数

           ...
            // ft - formatted text, (x, y) - point, where to draw            
            dc.PushTransform(new RotateTransform(-90, x, y));
            dc.DrawText(ft, new Point(x, y));
            dc.Pop();
            ...
    

    【讨论】:

    • 请考虑添加一些文字来解释您的答案。
    • 使用RotateTransform 定义旋转点以供一般使用是很有意义的。 x 和 y 是文本位置坐标。
    【解决方案2】:

    您将不得不使用DrawingContext 类的PushTransformPop 方法。

    DrawingContext dc; // Initialize this correct value
    RotateTransform RT = new RotateTransform();
    RT.Angle = 90
    dc.PushTransform(RT)
    dc.DrawText(...);
    dc.Pop();
    

    【讨论】:

      【解决方案3】:
      DrawingContext dc;
      Point textLocation
      var RT = new RotationTransform(-90.0);
      
      // You should transform the location likewise...
      location = new Point(-location.Y, location.X);
      
      dc.PushTransform(RT);
      dc.DrawText(formattedText, location);
      

      抱歉,我不得不发布这个,因为我用头撞墙 15 分钟试图弄清楚这一点。我不想让其他人经历那件事。

      【讨论】:

      • 虽然这个答案相当陈旧,但我想补充一点,转换位置应该由另一个转换对象完成,而不是“手动”交换 Point() ctor 的值,它与只有 90 度角。当使用文本位置创建 TranslateTransform 对象并在旋转之前推动它时,代码将适用于任何角度。
      猜你喜欢
      • 2014-10-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-03-04
      • 2011-01-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多