今天在部落格園上看到一篇文章,講的是如何用C#來實現Word中的藝術字效果,看完了深有感觸,將其代碼歸納整合,寫成一個類,這樣使用起來要更方便一些。(原文:http://www.cnblogs.com/ziyiFly/archive/2008/09/22/1296218.html)順便在此對作者表示深切敬意和誠摯的感謝! 代碼如下: 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; //繪製陰影 SolidBrush grayBrush = new SolidBrush(this.WordArtBackColor); SolidBrush colorBrush = new SolidBrush(this.WordArtForeColor); g.DrawString(this.Caption, this.WordArtFont, grayBrush, new PointF(0, 20)); g.ResetTransform(); //繪製前景 g.DrawString(this.Caption, this.WordArtFont, colorBrush, new PointF(0,20)); break; case WordArtEffectStyle.embossment://浮雕效果 backBrush = Brushes.Black; foreBrush = Brushes.White; g.DrawString(this.Caption, this.WordArtFont, backBrush, posX + 1, posY + 1); g.DrawString(this.Caption, this.WordArtFont, foreBrush, posX, posY ); break; case WordArtEffectStyle.forme://印版效果 int i = 0; backBrush = new SolidBrush(this.WordArtBackColor); foreBrush = new SolidBrush(this.WordArtForeColor); while (i < 20) { g.DrawString(this.Caption, this.WordArtFont, backBrush, posX - i, posY + i); i = i + 1; } g.DrawString(this.Caption, this.WordArtFont, foreBrush, posX, posY); break; case WordArtEffectStyle.Reflection://倒影效果 backBrush = new SolidBrush(this.WordArtBackColor); foreBrush = new SolidBrush(this.WordArtForeColor); g.TranslateTransform(posX, posY); int ascent = this.WordArtFont.FontFamily.GetCellAscent(this.WordArtFont.Style); int spacing = this.WordArtFont.FontFamily.GetLineSpacing(this.WordArtFont.Style); int lineHeight = System.Convert.ToInt16(this.WordArtFont.GetHeight(g)); int height = lineHeight * ascent / spacing; GraphicsState state = g.Save(); g.ScaleTransform(1, -1.0f); g.DrawString(this.Caption, this.WordArtFont, backBrush, 0, -height); g.Restore(state); g.DrawString(this.Caption,this.WordArtFont, foreBrush, 0, -height); break; case WordArtEffectStyle.shadow://陰影製作效果 Brush shadowBrush = Brushes.Gray; foreBrush = new SolidBrush(this.WordArtBackColor); posX = (this.Width - Convert.ToInt16(size.Width)) / 4; posY = (this.Height - Convert.ToInt16(size.Height)) / 3; g.DrawString(this.Caption, this.WordArtFont, shadowBrush, posX + 20, posY + 20); g.DrawString(this.Caption, this.WordArtFont, foreBrush, posX, posY); break; case WordArtEffectStyle.grain://紋理的效果 break; case WordArtEffectStyle.slope://傾斜 g.TranslateTransform(posX, posY); Matrix transform = g.Transform; //右傾斜文字 //float shearX = -0.230F; //左傾斜文字 float shearX = 0.550F; float shearY = 0.10F; transform.Shear(shearX, shearY); g.Transform = transform; g.DrawString(this.Caption, this.WordArtFont, foreBrush, 0, 0); break; case WordArtEffectStyle.shadeLines://漸層 Brush ShadowBrush = Brushes.Gray; PointF point = new PointF(0, 0); RectangleF rectangle = new RectangleF(point, size); Brush brush = new LinearGradientBrush(rectangle, Color.Red, Color.Green, LinearGradientMode.Horizontal); int xwidth = (this.Width - Convert.ToInt16(size.Width)) / 2; int xheight = (this.Height - Convert.ToInt16(size.Height)) / 2; g.DrawString(this.Caption,this.WordArtFont, brush, xwidth, xheight); break; case WordArtEffectStyle.circumgyrate://旋轉 for (int n = 0; n <= 360; n += 30) { //平移到對象中心 g.TranslateTransform(this.Width / 2, this.Height / 2); //設定Graphics對象的輸出角度 g.RotateTransform(n); //設定文字填充顏色 g.DrawString(this.Caption, this.WordArtFont, foreBrush, 0, 0); //恢複全域變換矩陣 g.ResetTransform(); } break; } } } public enum WordArtEffectStyle { //投影,浮雕,印版,倒影,陰影,紋理, 傾斜,漸層,旋轉 projection, embossment, forme, Reflection, shadow, grain, slope, shadeLines, circumgyrate } |