(C #) GDI + draw vertical text

Source: Internet
Author: User

Sometimes you need to use vertical text in the application C # GDI + drawing and search online.

There are two methods: 1. Use axis rotation.

2. Use stringformat.

1. Use axis rotation. This method is common and practical. However, it is inconvenient for me to use this method. First, pay attention to the coordinates when using this method. Because the coordinate axes are rotated, the coordinates also need to be rotated, which requires calculation.
protected override void OnPaint(System.Windows.Forms.PaintEventArgs e){    float vlblControlWidth;    float vlblControlHeight;    float vlblTransformX;    float vlblTransformY;    Color controlBackColor = BackColor;    Pen labelBorderPen;    SolidBrush labelBackColorBrush;    if (_transparentBG)    {        labelBorderPen = new Pen(Color.Empty, 0);        labelBackColorBrush = new SolidBrush(Color.Empty);    }    else    {        labelBorderPen = new Pen(controlBackColor, 0);        labelBackColorBrush = new SolidBrush(controlBackColor);    }        SolidBrush labelForeColorBrush = new SolidBrush(base.ForeColor);    base.OnPaint(e);    vlblControlWidth = this.Size.Width;    vlblControlHeight = this.Size.Height;    e.Graphics.DrawRectangle(labelBorderPen, 0, 0,                              vlblControlWidth, vlblControlHeight);    e.Graphics.FillRectangle(labelBackColorBrush, 0, 0,                              vlblControlWidth, vlblControlHeight);    e.Graphics.TextRenderingHint = this._renderMode;    e.Graphics.SmoothingMode =                System.Drawing.Drawing2D.SmoothingMode.HighQuality;        if (this.TextDrawMode == DrawMode.BottomUp)    {        vlblTransformX = 0;        vlblTransformY = vlblControlHeight;        e.Graphics.TranslateTransform(vlblTransformX, vlblTransformY);        e.Graphics.RotateTransform(270);        e.Graphics.DrawString(labelText, Font, labelForeColorBrush, 0, 0);    }    else    {        vlblTransformX = vlblControlWidth;        vlblTransformY = vlblControlHeight;        e.Graphics.TranslateTransform(vlblControlWidth, 0);        e.Graphics.RotateTransform(90);        e.Graphics.DrawString(labelText, Font, labelForeColorBrush, 0, 0,                               StringFormat.GenericTypographic);    }            }
2. Use stringformat. It is convenient to use this method without rotating the coordinate axis.
Stringformat Sf = new stringformat (); SF. formatflags = stringformatflags. directionvertical; G. drawstring ("depth", new system. drawing. font ("", 16, fontstyle. regular), brushes. black, new pointf (15, stapoint. Y + 5), SF );
3. It is relatively complicated to use coordinate axis transformation to draw vertical text, but it is applicable in many places. It is convenient to use the stringformat method, but it has many limitations. Of course, you can also customize a vertical lable control.

Reference: http://www.codeproject.com/Articles/19774/Extended-Vertical-Label-Control-in-C-NET#_rating http://www.cnblogs.com/peterzb/archive/2009/06/07/1498173.html

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.