zoukankan      html  css  js  c++  java
  • (C#)GDI+绘制垂直文字

    有时候在应用程序C# GDI+绘图中需要使用到垂直文字,在网上搜索一下。

    有两种方法:1.使用坐标轴旋转实现。

                          2.使用StringFormat实现。

    1.使用坐标轴旋转实现。

    这种方法比较常见,也是比较实用的方法。但对于我个人来说,使用这种方法有一定的不便。首先这种方法使用时需要注意坐标,因为坐标轴旋转,坐标也需要旋转,这需要计算。
                                                           
    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.使用StringFormat实现。

    使用这种方法不需要旋转坐标轴,比较方便。
    	StringFormat sf = new StringFormat();
    	sf.FormatFlags = StringFormatFlags.DirectionVertical;
    	g.DrawString("深度", new System.Drawing.Font("微软雅黑", 16, FontStyle.Regular),
    	Brushes.Black, new PointF(15, staPoint.Y + 5), sf);

    3.比较

    使用坐标轴变换来绘制垂直文字,相对复杂,但其适用的地方多。使用StringFormat方法比较方便,但局限性比较大。当然,也可以自定义一个垂向Lable控件。

    参考 :http://www.codeproject.com/Articles/19774/Extended-Vertical-Label-Control-in-C-NET#_rating
  • 相关阅读:
    curl 带 body
    import com.sun.org.apache.xml.internal.security.utils.Base64问题
    动静分离业务解决网页请求不被串改
    java 主动信任证书
    IO 多路复用详解
    spanish-1.1
    spring data JPA entityManager查询 并将查询到的值转为实体对象
    微信二维码支付报错
    军训入营学生发言稿
    电位器控制两个 LED 灯交替闪烁
  • 原文地址:https://www.cnblogs.com/finlay/p/3234737.html
Copyright © 2011-2022 走看看