• <legend id='8agI1'><style id='8agI1'><dir id='8agI1'><q id='8agI1'></q></dir></style></legend>
    • <bdo id='8agI1'></bdo><ul id='8agI1'></ul>

    <tfoot id='8agI1'></tfoot>
    <i id='8agI1'><tr id='8agI1'><dt id='8agI1'><q id='8agI1'><span id='8agI1'><b id='8agI1'><form id='8agI1'><ins id='8agI1'></ins><ul id='8agI1'></ul><sub id='8agI1'></sub></form><legend id='8agI1'></legend><bdo id='8agI1'><pre id='8agI1'><center id='8agI1'></center></pre></bdo></b><th id='8agI1'></th></span></q></dt></tr></i><div id='8agI1'><tfoot id='8agI1'></tfoot><dl id='8agI1'><fieldset id='8agI1'></fieldset></dl></div>

      <small id='8agI1'></small><noframes id='8agI1'>

      1. C#中的旋转文本对齐

        Rotated text align in C#(C#中的旋转文本对齐)

        <i id='F5Y0t'><tr id='F5Y0t'><dt id='F5Y0t'><q id='F5Y0t'><span id='F5Y0t'><b id='F5Y0t'><form id='F5Y0t'><ins id='F5Y0t'></ins><ul id='F5Y0t'></ul><sub id='F5Y0t'></sub></form><legend id='F5Y0t'></legend><bdo id='F5Y0t'><pre id='F5Y0t'><center id='F5Y0t'></center></pre></bdo></b><th id='F5Y0t'></th></span></q></dt></tr></i><div id='F5Y0t'><tfoot id='F5Y0t'></tfoot><dl id='F5Y0t'><fieldset id='F5Y0t'></fieldset></dl></div>
        1. <legend id='F5Y0t'><style id='F5Y0t'><dir id='F5Y0t'><q id='F5Y0t'></q></dir></style></legend>

        2. <tfoot id='F5Y0t'></tfoot>
            <tbody id='F5Y0t'></tbody>

          • <bdo id='F5Y0t'></bdo><ul id='F5Y0t'></ul>

              <small id='F5Y0t'></small><noframes id='F5Y0t'>

                  本文介绍了C#中的旋转文本对齐的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我需要能够旋转标签中的文本并将其左对齐、右对齐或居中对齐.到目前为止,我可以在派生标签的 onPaint 方法中使用此代码进行旋转:

                  I need to be able to rotate text in a label and align it to the left, right or center. So far I am able to do rotation with this code in the derived label's onPaint method:

                   float width = graphics.MeasureString(Text, this.Font).Width;
                   float height = graphics.MeasureString(Text, this.Font).Height;
                  
                   double angle = (_rotationAngle / 180) * Math.PI;
                   graphics.TranslateTransform(
                       (ClientRectangle.Width + (float)(height * Math.Sin(angle)) - (float)(width * Math.Cos(angle))) / 2,
                       (ClientRectangle.Height - (float)(height * Math.Cos(angle)) - (float)(width * Math.Sin(angle))) / 2);
                   graphics.RotateTransform(270f);
                   graphics.DrawString(Text, this.Font, textBrush, new PointF(0,0), stringFormat);
                   graphics.ResetTransform();
                  

                  而且效果很好.我可以看到文字旋转了 270 度.

                  And it works fine. I can see text rotated 270 degrees.

                  但是当我尝试在 stringFormat 中设置对齐时,它变得疯狂,我无法弄清楚发生了什么.

                  But when I try to set alignment in stringFormat it goes crazy, and I can't figure out what's going on.

                  如何让文本旋转 270 度并向上对齐?

                  How can I have text rotated by 270 degrees and align it to up?

                  推荐答案

                  如果有人在寻找提示,这里是 0、90、180、270 和 360 度旋转的解决方案,其中 StringAligment 可以工作.

                  In case somebody was looking for tips, here is the solution for 0, 90, 180, 270, and 360 degrees rotation, where StringAligment works.

                  一件事是选择正确的点将原点移动到,第二件事是根据旋转修改显示矩形.

                  One thing was choosing the right point for moving the origin to, and the second one was to modify the display rectangle according to rotation.

                  StringFormat format = new StringFormat();
                  format.Alignment = StringAlignment.Center;
                  
                  SizeF txt = e.Graphics.MeasureString(Text, this.Font);
                  SizeF sz = e.Graphics.VisibleClipBounds.Size;
                  
                  //90 degrees
                  e.Graphics.TranslateTransform(sz.Width, 0);
                  e.Graphics.RotateTransform(90);
                  e.Graphics.DrawString(Text, this.Font, Brushes.Black, new RectangleF(0, 0, sz.Height, sz.Width), format);
                  e.Graphics.ResetTransform();
                  
                  //180 degrees
                  e.Graphics.TranslateTransform(sz.Width, sz.Height);
                  e.Graphics.RotateTransform(180);
                  e.Graphics.DrawString(Text, this.Font, Brushes.Black, new RectangleF(0, 0, sz.Width, sz.Height), format);
                  e.Graphics.ResetTransform();
                  
                  //270 degrees
                  e.Graphics.TranslateTransform(0, sz.Height);
                  e.Graphics.RotateTransform(270);
                  e.Graphics.DrawString(Text, this.Font, Brushes.Black, new RectangleF(0, 0, sz.Height, sz.Width), format);
                  e.Graphics.ResetTransform();
                  
                  //0 = 360 degrees
                  e.Graphics.TranslateTransform(0, 0);
                  e.Graphics.RotateTransform(0);
                  e.Graphics.DrawString(Text, this.Font, Brushes.Black, new RectangleF(0, 0, sz.Width, sz.Height), format);
                  e.Graphics.ResetTransform();
                  

                  如果您将此代码放在标签的 OnPaint 事件中,它会显示四次旋转表单的标题.

                  If you put this code in label's OnPaint event, it would display your rotated form's title four times.

                  这篇关于C#中的旋转文本对齐的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                  相关文档推荐

                  Populate ListBox with a IEnumrable on another thread (winforms)(在另一个线程(winforms)上使用 IEnumrable 填充 ListBox)
                  listbox selected item give me quot; System.Data.DataRowViewquot; , C# winforms(列表框选择的项目给我quot;System.Data.DataRowView, C# Winforms)
                  Cannot remove items from ListBox(无法从列表框中删除项目)
                  Preventing ListBox scrolling to top when updated(更新时防止列表框滚动到顶部)
                  Drag and drop from list to canvas on windows phone with MVVM(使用 MVVM 在 Windows 手机上从列表拖放到画布)
                  Deselection on a WPF listbox with extended selection mode(具有扩展选择模式的 WPF 列表框上的取消选择)
                  • <bdo id='CFPCi'></bdo><ul id='CFPCi'></ul>

                      <tfoot id='CFPCi'></tfoot>
                    1. <small id='CFPCi'></small><noframes id='CFPCi'>

                    2. <legend id='CFPCi'><style id='CFPCi'><dir id='CFPCi'><q id='CFPCi'></q></dir></style></legend>

                      1. <i id='CFPCi'><tr id='CFPCi'><dt id='CFPCi'><q id='CFPCi'><span id='CFPCi'><b id='CFPCi'><form id='CFPCi'><ins id='CFPCi'></ins><ul id='CFPCi'></ul><sub id='CFPCi'></sub></form><legend id='CFPCi'></legend><bdo id='CFPCi'><pre id='CFPCi'><center id='CFPCi'></center></pre></bdo></b><th id='CFPCi'></th></span></q></dt></tr></i><div id='CFPCi'><tfoot id='CFPCi'></tfoot><dl id='CFPCi'><fieldset id='CFPCi'></fieldset></dl></div>
                            <tbody id='CFPCi'></tbody>