(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
参考 :http://www.codeproject.com/Articles/19774/Extended-Vertical-Label-Control-in-C-NET#_rating


浙公网安备 33010602011771号