代碼如下:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Drawing.Text;
using System.Drawing.Drawing2D;
using System.Data;
using System.Text;
using System.Windows.Forms;
public partial class WordArt : UserControl//這是一個藝術字的控件
{
//文本屬性
private string _text = "WordArt";
public string Caption
{
get { return _text; }
set { _text = value; }
}
//字體以及大小
private Font _WordArtFont = new Font("宋體",15);
public Font WordArtFont
{
get { return _WordArtFont; }
set { _WordArtFont = value; }
}
//顏色
private Color _WordArtForeColor = Color.BlueViolet;
public Color WordArtForeColor
{
get { return _WordArtForeColor; }
set { _WordArtForeColor = value; }
}
//陰影的顏色
private Color _WordArtBackColor = Color.Gray;
public Color WordArtBackColor
{
set { _WordArtBackColor = value; }
get { return _WordArtBackColor; }
}
//文本輸出質量:呈現模式和平滑效果
private TextRenderingHint _TextRenderingHint = TextRenderingHint.ClearTypeGridFit;
public TextRenderingHint WordArtTextRenderingHint
{
get { return _TextRenderingHint; }
set { _TextRenderingHint = value; }
}
public SmoothingMode _SmoothingMode = SmoothingMode.AntiAlias;
public SmoothingMode WordArtSmoothingMode
{
get { return _SmoothingMode; }
set { _SmoothingMode = value; }
}
public WordArt()
{
InitializeComponent();
}
//藝術字的形式:陰影,浮雕……
private WordArtEffectStyle _WordArtEffect=WordArtEffectStyle.projection;//投影為默認形式;
public WordArtEffectStyle WordArtEffect
{
get { return _WordArtEffect; }
set { _WordArtEffect = value; }
}
protected override void OnPaint(PaintEventArgs e)
{
base.OnPaint(e);
Graphics g = this.CreateGraphics();
Brush backBrush=new SolidBrush(this.WordArtBackColor);
Brush foreBrush=new SolidBrush(this.WordArtForeColor);
SizeF size = g.MeasureString(this.Caption, this.WordArtFont);
Single posX = (this.Width - Convert.ToInt16(size.Width)) / 2;
Single posY = (this.Height - Convert.ToInt16(size.Height)) / 2;
switch (this.WordArtEffect)
{
case WordArtEffectStyle.projection://投影效果
//設置文本輸出質量
g.TextRenderingHint = this.WordArtTextRenderingHint;
g.SmoothingMode = this.WordArtSmoothingMode;
Matrix matrix = new Matrix();
//投射
matrix.Shear(-1.5f, 0.0f);
//縮放
matrix.Scale(1, 0.5f);
//平移
matrix.Translate(120, 75);
//對繪圖平面坐標實施變換
g.Transform = matrix;