2012-01-11 107 views
6

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

回答

10

您将不得不使用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); 

对不起,我不得不张贴这一点,因为我打我的头靠在墙上十五分钟试图弄清楚这一点。我不希望任何人通过它。

+1

Altough这个答案已很旧,我想补充一点,转化该位置应该由另一个转换对象完成,而不是“手动”交换Point()ctor的值,该值仅与角度90一起工作。当使用文本位置创建TranslateTransform对象并在旋转之前将其推入时,代码将以任何角度工作。 – user2261015 2015-10-14 08:26:28

-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(); 
     ... 
+2

请考虑添加一些文字来解释您的答案。 – 2017-02-20 11:49:52