2012-01-11 108 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