I am doing some custom drawing using DrawingContext in WPF. I am using DrawingContext.DrawText
for drawing strings. Now, at a place I want to draw the text vertically. Is there any option in the DrawingContext OR DrawText() function to draw text vertically?
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试):
问题:
回答1:
You will have to use PushTransform
and Pop
methods of DrawingContext
class.
DrawingContext dc; // Initialize this correct value
RotateTransform RT = new RotateTransform();
RT.Angle = 90
dc.PushTransform(RT)
dc.DrawText(...);
dc.Pop();
回答2:
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);
Sorry, I had to post this because I was hitting my head against a wall for fifteen minutes trying to figure this out. I don't want anyone else to go through that.
回答3:
Here is my solution: It is need to create rotate transform around the text origin, so we pass x and y to RotateTransform constructor
...
// 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();
...