C#编写的艺术字类方法
代码如下:
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#编写的艺术字类方法的更多相关文章
- Spring Boot 2.x 综合示例-整合thymeleaf、mybatis、shiro、logging、cache开发一个文章发布管理系统
一.概述 经过HelloWorld示例(Spring Boot 2.x 快速入门(上)HelloWorld示例)( Spring Boot 2.x 快速入门(下)HelloWorld示例详解)两篇的学 ...
- Spring MVC请求参数绑定 自定义类型转化 和获取原声带额servlet request response信息
首先还在我们的框架的基础上建立文件 在domian下建立Account实体类 import org.springframework.stereotype.Controller; import org. ...
- 云服务器AWD平台搭建
开学后实验室来了几个新同学,在线上CTF方面大家一直在持续学习,但AWD模式的CTF我们练习并不多,所以准备搭建一个AWD平台用于实验室成员的线下赛攻防练习. 最开始的是防灾科技大学的线下AWD靶场: ...
- 编写高质量代码:改善Java程序的151个建议(第8章:异常___建议114~117)
建议114:不要在构造函数中抛出异常 Java异常的机制有三种: Error类及其子类表示的是错误,它是不需要程序员处理也不能处理的异常,比如VirtualMachineError虚拟机错误,Thre ...
- 编写高质量代码:改善Java程序的151个建议(第6章:枚举和注解___建议88~92)
建议88:用枚举实现工厂方法模式更简洁 工厂方法模式(Factory Method Pattern)是" 创建对象的接口,让子类决定实例化哪一个类,并使一个类的实例化延迟到其它子类" ...
- iOS 触摸事件与UIResponder(内容根据iOS编程编写)
触摸事件 因为 UIView 是 UIResponder 的子类,所以覆盖以下四个方法就可以处理四种不同的触摸事件: 1. 一根手指或多根手指触摸屏幕 - (void)touchesBegan:(N ...
- 分享:使用 TypeScript 编写的 JavaScript 游戏代码
<上篇博客>我写出了我一直期望的 JavaScript 大型程序的开发模式,以及 TS(TypeScript) 的一些优势.博客完成之后,我又花了一天时间试用 TS,用它来重构之前编写的一 ...
- Unity调用Android类方法
Unity调用Android类方法 1. 添加Unity的classes.jar文件 创建一个Android工程AndroidUnityDemo. 由于Unity的版本不同,直接在Unity安装包文 ...
- Objective-C 对象(内容根据iOS编程编写)
开发iOS程序需要使用 Objective-C 语言和Cocoa Touch框架.Objective-C 源于 C 语言,是 C 语言的扩展. Cocoa Touch框架是一个Objective-C类 ...
随机推荐
- 【笔记】如何查看HTTP请求头&&【实验吧】天下武功唯快不破
打开Chrome浏览器,点击右上角“三”按钮. 点击工具-----再点击开发者工具 找到Network选项框.以百度经验页面为例,点击任务选框来查看网络请求流 在Network框内会有所有的请 ...
- c#使用GDI+简单绘图
private void button2_Click(object sender, EventArgs e) { Bitmap image = new Bitmap(200, 200); Graphi ...
- NodeJS 初学之安装配置环境
[TOC] 1.环境安装 操作系统: Ubuntu 16.04.2 LTS 1.1安装nvm ryan@ryan-900X5L:~/temp$ curl https://raw.githubuserc ...
- C#仪器数据文件解析-Excel文件(xls、xlsx)
不少仪器工作站可以将数据导出为Excel文件,包括97-2003版本的xls文件和2007+的xlsx文件. 采集Excel文件相比采集pdf文件更容易.程序更健壮,毕竟Excel中数据有明确的行.列 ...
- win10 uwp 简单MasterDetail
中文 English 本文主要讲实现一个简单的界面,可以在窗口比较大显示列表和内容,窗口比较小时候显示列表或内容.也就是在窗口比较小的时候,点击列表会显示内容,点击返回会显示列表. 先放图,很简单. ...
- junit搭配hamcrest使用
开篇 - 快速进行软件编码,与功能测试应该是每个写代码的人,应该掌握的技能,如何进行优雅的写代码,把测试的时间压缩,腾出时间来休息.下面听我一一道来: 依赖:junit 4.4 hamcrest 1. ...
- Vector容器构造函数
No1 vector(); No2 vector( const vector& c ); No3 explicit vector( size_type num, const TYPE& ...
- ViewPager使用记录2——展示动态数据
ViewPager是v4支持库中的一个控件,相信几乎所有接触Android开发的人都对它不陌生.之所以还要在这里翻旧账,是因为我在最近的项目中有多个需求用到了它,觉得自己对它的认识不够深刻.我计划从最 ...
- 在vmware 中使用桥连接 连接到网络
vMware虚拟机以后,连不上网,通过ifconfig命令,查看结果,如图所示: 然后,我想尝试一下,在虚拟机中ping 本地物理机地址,结果如图. 总结起来,主要有4步: 1.使用chkconfig ...
- NotePad++ 正则表达式替换 高级用法 [转]
转自:http://blog.csdn.net/gdp12315_gu/article/details/51730584 在我们处理文件时,很多时候会用到查找与替换.当我们想将文件中某一部分替换替换文 ...