C#利用GDI+绘制旋转文字等效果实例
2021-04-25 17:53
标签:clear ble onpaint pen 内容 类型 dbr 下载 nbsp 本文实例讲述了C#利用GDI+绘制旋转文字等效果的方法,是非常实用的技巧。分享给大家供大家参考之用。具体如下: C#中利用GDI+绘制旋转文本的文字,网上有很多资料,基本都使用矩阵旋转的方式实现。但基本都只提及按点旋转,若要实现在矩形范围内旋转文本,资料较少。经过琢磨,可以将矩形内旋转转化为按点旋转,不过需要经过不少的计算过程。利用下面的类可以实现该功能。 具体实现代码如下: 测试代码如下: 效果如下图: 完整实例点此 。 希望本文所述对大家的C#程序设计有所帮助 本文地址: http://www.paobuke.com/develop/c-develop/pbk23483.html C#利用GDI+绘制旋转文字等效果实例 标签:clear ble onpaint pen 内容 类型 dbr 下载 nbsp 原文地址:http://www.cnblogs.com/paobuke/p/7919983.htmlusing System;
using System.Collections.Generic;
using System.Drawing;
using System.Drawing.Drawing2D;
namespace RotateText
{
public class GraphicsText
{
private Graphics _graphics;
public GraphicsText()
{
}
public Graphics Graphics
{
get { return _graphics; }
set { _graphics = value; }
}
///
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Windows.Forms;
namespace RotateText
{
public partial class FormMain : Form
{
private Font _font = new Font("Arial", 12);
private Brush _brush = new SolidBrush(Color.Black);
private Pen _pen = new Pen(Color.Black, 1f);
private string _text = "Crow Soft";
public FormMain()
{
InitializeComponent();
}
protected override void OnPaint(PaintEventArgs e)
{
base.OnPaint(e);
GraphicsText graphicsText = new GraphicsText();
graphicsText.Graphics = e.Graphics;
// 绘制围绕点旋转的文本
StringFormat format = new StringFormat();
format.Alignment = StringAlignment.Center;
format.LineAlignment = StringAlignment.Center;
graphicsText.DrawString(_text, _font, _brush, new PointF(100, 80), format, 45f);
graphicsText.DrawString(_text, _font, _brush, new PointF(200, 80), format, -45f);
graphicsText.DrawString(_text, _font, _brush, new PointF(300, 80), format, 90f);
graphicsText.DrawString(_text, _font, _brush, new PointF(400, 80), format, -60f);
// 绘制矩形内旋转的文本
// First line
RectangleF rc = RectangleF.FromLTRB(50, 150, 200, 230);
RectangleF rect = rc;
format.Alignment = StringAlignment.Near;
e.Graphics.DrawRectangle(_pen, rect.Left, rect.Top, rect.Width, rect.Height);
graphicsText.DrawString(_text, _font, _brush, rect, format, 30);
rect.Location += new SizeF(180, 0);
format.LineAlignment = StringAlignment.Near;
e.Graphics.DrawRectangle(_pen, rect.Left, rect.Top, rect.Width, rect.Height);
graphicsText.DrawString(_text, _font, _brush, rect, format, -30);
rect.Location += new SizeF(180, 0);
format.LineAlignment = StringAlignment.Center;
e.Graphics.DrawRectangle(_pen, rect.Left, rect.Top, rect.Width, rect.Height);
graphicsText.DrawString(_text, _font, _brush, rect, format, -90);
rect.Location += new SizeF(180, 0);
format.LineAlignment = StringAlignment.Far;
e.Graphics.DrawRectangle(_pen, rect.Left, rect.Top, rect.Width, rect.Height);
graphicsText.DrawString(_text, _font, _brush, rect, format, 70);
// Second line
rect = rc;
rect.Location += new SizeF(0, 100);
format.Alignment = StringAlignment.Center;
e.Graphics.DrawRectangle(_pen, rect.Left, rect.Top, rect.Width, rect.Height);
graphicsText.DrawString(_text, _font, _brush, rect, format, 40);
rect.Location += new SizeF(180, 0);
format.LineAlignment = StringAlignment.Near;
e.Graphics.DrawRectangle(_pen, rect.Left, rect.Top, rect.Width, rect.Height);
graphicsText.DrawString(_text, _font, _brush, rect, format, 30);
rect.Location += new SizeF(180, 0);
format.LineAlignment = StringAlignment.Center;
e.Graphics.DrawRectangle(_pen, rect.Left, rect.Top, rect.Width, rect.Height);
graphicsText.DrawString(_text, _font, _brush, rect, format, -70);
rect.Location += new SizeF(180, 0);
format.LineAlignment = StringAlignment.Far;
e.Graphics.DrawRectangle(_pen, rect.Left, rect.Top, rect.Width, rect.Height);
graphicsText.DrawString(_text, _font, _brush, rect, format, 60);
// Third line
rect = rc;
rect.Location += new SizeF(0, 200);
format.Alignment = StringAlignment.Far;
e.Graphics.DrawRectangle(_pen, rect.Left, rect.Top, rect.Width, rect.Height);
graphicsText.DrawString(_text, _font, _brush, rect, format, -30);
rect.Location += new SizeF(180, 0);
format.LineAlignment = StringAlignment.Near;
e.Graphics.DrawRectangle(_pen, rect.Left, rect.Top, rect.Width, rect.Height);
graphicsText.DrawString(_text, _font, _brush, rect, format, -30);
rect.Location += new SizeF(180, 0);
format.LineAlignment = StringAlignment.Center;
e.Graphics.DrawRectangle(_pen, rect.Left, rect.Top, rect.Width, rect.Height);
graphicsText.DrawString(_text, _font, _brush, rect, format, 90);
rect.Location += new SizeF(180, 0);
format.LineAlignment = StringAlignment.Far;
e.Graphics.DrawRectangle(_pen, rect.Left, rect.Top, rect.Width, rect.Height);
graphicsText.DrawString(_text, _font, _brush, rect, format, 45);
}
}
}
C#利用GDI+绘制旋转文字等效果实例相关内容
文章标题:C#利用GDI+绘制旋转文字等效果实例
文章链接:http://soscw.com/index.php/essay/79460.html