【问题标题】:drawstring rotate long text 180拉绳旋转长文本 180
【发布时间】:2016-11-15 09:47:07
【问题描述】:

我使用drawstring方法在矩形内绘制文本,..我想将文本旋转180度所以我使用了这个代码

   Rectangle displayRectangleh =
       new Rectangle(new Point((int)posh.X, (int)posh.Y), new Size(rectwdh, recth));

   StringFormat format1h = new StringFormat(StringFormatFlags.DirectionVertical);
   format1h.LineAlignment = StringAlignment.Center;
   format1h.Alignment = StringAlignment.Center;
   b = new SolidBrush(Color.Black);
   e.ChartGraphics.Graphics.TranslateTransform((float)rectwdh / 2 + posh.X, recth / 2 + posh.Y);
   e.ChartGraphics.Graphics.RotateTransform(180);
   e.ChartGraphics.Graphics.TranslateTransform(-((float)rectwdh / 2 + posh.X), -(recth / 2 + posh.Y));
            Font boldFonth = new Font(FontFamily.GenericSansSerif, 16, FontStyle.Bold, GraphicsUnit.Pixel, 1, true);
   e.ChartGraphics.Graphics.DrawRectangle(new Pen(Color.Black, 2), displayRectangleh);
   e.ChartGraphics.Graphics.DrawString("This is rotated long text test ", boldFonth, b, (RectangleF)displayRectangleh, format1h);
   e.ChartGraphics.Graphics.ResetTransform();

其中 posh.X 和 posh.Y(重新排列位置)、rectwdh(矩形宽度)和 recth(矩形高度)

它非常适合短文本,但对于长文本,新行将在我的旧行上方,请参见下一张图片:

我什至尝试添加换行符 \n 但结果相同。 我的问题:如何在矩形内旋转文本 180 并以正确对齐结束??

【问题讨论】:

    标签: c# winforms drawstring


    【解决方案1】:

    之前我什至从未使用过 StringFormat API,我随意地敲了敲它,直到我发现了这个:

    StringFormat format1h = new StringFormat(StringFormatFlags.DirectionVertical | StringFormatFlags.DirectionRightToLeft);

    【讨论】:

    • 我不明白为什么这个答案被接受。
    • 因为它有效。
    • 如果你的意思是这会改变方向,不,它不会! StringFormatFlags.DirectionVertical | StringFormatFlags.DirectionRightToLeft 永远不会将方向更改为底部顶部。只有转换才能解决这个问题。您的意思是这必须与转换解决方案结合使用吗?
    • 何不试试看?
    • 我试过了,不行。 Transform 工程解决方案
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-11-16
    • 2023-04-07
    • 1970-01-01
    • 2020-03-03
    • 2011-09-20
    相关资源
    最近更新 更多