代码如下:

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
    }

C#编写的艺术字类方法的更多相关文章

  1. Spring Boot 2.x 综合示例-整合thymeleaf、mybatis、shiro、logging、cache开发一个文章发布管理系统

    一.概述 经过HelloWorld示例(Spring Boot 2.x 快速入门(上)HelloWorld示例)( Spring Boot 2.x 快速入门(下)HelloWorld示例详解)两篇的学 ...

  2. Spring MVC请求参数绑定 自定义类型转化 和获取原声带额servlet request response信息

    首先还在我们的框架的基础上建立文件 在domian下建立Account实体类 import org.springframework.stereotype.Controller; import org. ...

  3. 云服务器AWD平台搭建

    开学后实验室来了几个新同学,在线上CTF方面大家一直在持续学习,但AWD模式的CTF我们练习并不多,所以准备搭建一个AWD平台用于实验室成员的线下赛攻防练习. 最开始的是防灾科技大学的线下AWD靶场: ...

  4. 编写高质量代码:改善Java程序的151个建议(第8章:异常___建议114~117)

    建议114:不要在构造函数中抛出异常 Java异常的机制有三种: Error类及其子类表示的是错误,它是不需要程序员处理也不能处理的异常,比如VirtualMachineError虚拟机错误,Thre ...

  5. 编写高质量代码:改善Java程序的151个建议(第6章:枚举和注解___建议88~92)

    建议88:用枚举实现工厂方法模式更简洁 工厂方法模式(Factory Method Pattern)是" 创建对象的接口,让子类决定实例化哪一个类,并使一个类的实例化延迟到其它子类" ...

  6. iOS 触摸事件与UIResponder(内容根据iOS编程编写)

    触摸事件 因为 UIView 是 UIResponder 的子类,所以覆盖以下四个方法就可以处理四种不同的触摸事件: 1.  一根手指或多根手指触摸屏幕 - (void)touchesBegan:(N ...

  7. 分享:使用 TypeScript 编写的 JavaScript 游戏代码

    <上篇博客>我写出了我一直期望的 JavaScript 大型程序的开发模式,以及 TS(TypeScript) 的一些优势.博客完成之后,我又花了一天时间试用 TS,用它来重构之前编写的一 ...

  8. Unity调用Android类方法

    Unity调用Android类方法 1.  添加Unity的classes.jar文件 创建一个Android工程AndroidUnityDemo. 由于Unity的版本不同,直接在Unity安装包文 ...

  9. Objective-C 对象(内容根据iOS编程编写)

    开发iOS程序需要使用 Objective-C 语言和Cocoa Touch框架.Objective-C 源于 C 语言,是 C 语言的扩展. Cocoa Touch框架是一个Objective-C类 ...

随机推荐

  1. 三分钟浅谈TT猫的前端优化

    首先看一张访问TT猫首页的截图: 测试环境为谷歌浏览器,暂且不讨论其它浏览器,截图下方我们可以观察到以下参数: DOMContentLoaded:1.42s | Load:2.31s 以上参数是在CT ...

  2. 设置MySQL最大连接数

    <pre name="code" class="sql">在使用MySQL数据库的时候,经常会遇到这么一个问题,就是"Can not co ...

  3. HttpHelper类使用方法

    HttpHelper http = new HttpHelper(); HttpItem item = new HttpItem() { URL = "http://www.sufeinet ...

  4. SQL SERVER 数据库级联删除

    --SQL SERVER 2008R2 级联删除:主子表设置外键关联,当主表数据删除的时候会自动删除子表中对应的数据 --创建主表 create table test_main( ID ,) PRIM ...

  5. 使用spark对hive表中的多列数据判重

    本文处理的场景如下,hive表中的数据,对其中的多列进行判重deduplicate. 1.先解决依赖,spark相关的所有包,pom.xml spark-hive是我们进行hive表spark处理的关 ...

  6. DOS命令(系统错误5,拒绝访问)的解决方法

    在用DOS命令启动MySQL服务时,出现(系统错误5,拒绝访问)的错误提示,这是由于我们操作的权限不足造成的,需要以管理员身份启动,解决问题方法如下: 1."Windows+S"- ...

  7. 【笔记】【VSCode】Windows下VSCode编译调试c/c++

    转载自http://m.2cto.com/kf/201606/516207.html 首先看效果 设置断点,变量监视,调用堆栈的查看: 条件断点的使用: 下面是配置过程: 总体流程: 下载安装vsco ...

  8. C# App 中嵌入 Chrome 浏览器

    http://www.codeceo.com/article/cefsharp-charp-app-chrome.html http://developer.51cto.com/art/201304/ ...

  9. ASP 文件内部访问数据库的通常途径

    创建至数据库的 ADO 连接(ADO connection) 打开数据库连接 创建 ADO 记录集(ADO recordset) 打开记录集(recordset) 从数据集中提取你所需要的数据 关闭数 ...

  10. jstl 处理字符串

    1.引入 <%@ taglib uri="http://java.sun.com/jsp/jstl/functions" prefix="fn"%> ...