VC GDI+ 字体的特效

一 首先下载,gdi 有关的头文件和库文件 

#include

 

二 这些语句最好放到文件 StdAfx.h中

 

#ifndef ULONG_PTR

typedef  unsigned long*   ULONG_PTR;

#include "Include\\GdiPlus.h"

using namespace Gdiplus;

#pragma comment(lib, "lib\\gdiplus.lib")

#endif 

 

 

三 初始化调用

HWND hWnd;

BOOL CGdiDlg::OnInitDialog()

{

CDialog::OnInitDialog();

hWnd=this->m_hWnd;

// TODO: Add extra initialization here

//1 最好在App的 InitInstance()实现

ULONG_PTR m_gdiplusToken;

GdiplusStartupInput gdiplusStartupInput;

GdiplusStartup(&m_gdiplusToken,&gdiplusStartupInput,NULL);

//2 use

// CDC *pDC=GetDC();

Graphics graphics(pDC->m_hDC);

Image image(L"2.jpg");

drawFont(graphics);

 

return TRUE;  // return TRUE  unless you set the focus to a control

}

 

以上的说明卡参看《VC调用GDI+绘图

 

四 以下是具体处理的函数

 

void OnRefresh() 

{

CRect rect;

GetClientRect(hWnd,&rect);

rect.bottom-=100;

HDC hDC=GetDC(hWnd);

//SelectStockObject((hDC,WHITE_BRUSH);

//SelectStockObject

Rectangle(hDC,rect.left,rect.top,rect.right,rect.bottom);

}

void pause_run()

{

continue_flag=0;

// while (continue_flag==0)

{Sleep(1000);}

continue_flag=0;

OnRefresh();

}

 

 

 

//这是把asii字符转换为unicode字符

void c2w(wchar_t *pwstr,const char *str,size_t len)

{

if(str)

    {

size_t nu = strlen(str);

//得到转换后的字符个数;每个汉字和英文字符都占1个字符(每个字符两个字节),

size_t n =(size_t)MultiByteToWideChar(CP_ACP,0,(const char *)str,(int)nu,NULL,0);

if(n>=len)n=len-1;

MultiByteToWideChar(CP_ACP,0,(const char *)str,(int)nu,pwstr,(int)n);

pwstr[n]=0;

    }

}

 

//将wchar_t* 转成char*的实现函数如下:

void w2c(char *pcstr,const wchar_t *pwstr, size_t len)

{

int nlength=wcslen(pwstr);

//获取转换后的长度,是字节数,不是字符数,1个汉字占2个字节,1个英文字符占1个字节

int nbytes = WideCharToMultiByte(CP_ACP,0,pwstr,nlength,NULL,0,NULL, NULL); 

if(nbytes>len)   nbytes=len;

WideCharToMultiByte( CP_ACP,0, pwstr,nlength,pcstr,nbytes,NULL,NULL );  

// return pcstr ;

}

 

 

 

//3 字体处理

drawFont(Graphics &graph)

{

SolidBrush solidBrush(Color());

Pen pen(Color(255, 255, 0, 0)); // 创建红色笔

Font font(L"华文新魏", 100);

//Font *font;//=new Font(L"华文新魏", 100,FontStyleRegular ,UnitPoint,NULL);

int n;

 

 

REAL fs[] = {100, 60, 42, 36, 26, 24, 22, 18, 16, 15, 14, 12, 

10.5, 9, 7.5, 6.5, 5.5, 5};

CString fno[] = {L"特", L"小特", L"初", L"小初", L"一", 

L"小一", L"二", L"小二", L"三", L"小三", L"四", L"小四", 

L"五", L"小五", L"六", L"小六", L"七", L"八"};

WCHAR wcstr[100];// wchar_t

REAL size, y = 10.0f;

SolidBrush textBrush(Color::Black);

// Graphics graph(pDC->m_hDC);

char chStr[MAX_PATH];

CString str;

for (int i = 0; i < 18; i++) {

size = fs[i];  

str.Format( 

"%s号(%.4g像素 %g点 %.4g英寸 %.4g文档 %.4g毫米)", 

fno[i], size * 4 / 3.0, size, size / 72.0, 

size * 300 / 72.0, size / 72.0 * 25.4);

c2w(wcstr ,str.GetBuffer(str.GetLength()),sizeof(wcstr));

graph.DrawString(wcstr, INT(wcslen(wcstr)), &Font(L"宋体", size), PointF(10.0f, y), &textBrush);

y += size * 1.5f;

}

 

  pause_run();

 

//int  n = wcstombs(ch, wcstr, sizeof(ch));        //宽字节转换为muiltychar

// str="";

// w2c(str.GetBuffer(0) ,wcstr,sizeof(str));

FontFamily fontFamily2(L"楷体_GB2312");

Font font1(&fontFamily2,20,FontStyleRegular,UnitPixel);

solidBrush.SetColor(Color(255,0,0,255));

WCHAR str1[]=L"是5号01没有任何优化处理";

graph.SetTextRenderingHint(TextRenderingHintSingleBitPerPixel);

graph.DrawString(str1,(int)wcslen(str1),&font1,PointF(10,10),&solidBrush);

WCHAR str2[]=L"字体优化,但边不做平滑处理";

graph.SetTextRenderingHint(TextRenderingHintSingleBitPerPixelGridFit);

graph.DrawString(str2,(int)wcslen(str2),&font1,PointF(10,30),&solidBrush);

WCHAR str3[]=L"消除走样,且边做平滑处理";

graph.SetTextRenderingHint(TextRenderingHintAntiAliasGridFit);

graph.DrawString(str3,(int)wcslen(str3),&font1,PointF(10,50),&solidBrush);

 

  pause_run();

 

 

//美术字

//1.阴影字

SolidBrush textBrush2(Color::Red), shadowBrush(Color::Gray);

HatchBrush hatchBrush(HatchStyleForwardDiagonal, 

Color::Black, Color::White);

wcscpy(wcstr, L"圣诞前夜");

REAL d = 10.0f, dd = 5.0f;

graph.DrawString(wcstr, wcslen(wcstr), &Font(L"华文新魏", 100),//font, 

PointF(d + dd, d + dd), &shadowBrush);

graph.DrawString(wcstr,  wcslen(wcstr), &Font(L"华文新魏", 100),// &font,

PointF(d, d), &textBrush2);

for (  i = 0; i < 20; i++)

graph.DrawString(wcstr,  wcslen(wcstr),  &Font(L"华文新魏", 100),//&font, 

PointF(d + i, 150 + d + i + 2), &hatchBrush);

graph.DrawString(wcstr,  wcslen(wcstr), &Font(L"华文新魏", 100),//&font,

PointF(d, 150 + d), &textBrush2);

 

pause_run();

 

 

//2.条纹字

wcscpy(wcstr, L"圣诞快乐");

// Font font(L"华文新魏", 140);

HatchBrush hatchBrush1(HatchStyleForwardDiagonal, Color::Red, Color::White);

graph.DrawString(wcstr,  wcslen(wcstr), &Font(L"华文新魏", 100), PointF(0.0f, 0.0f), &hatchBrush1);

HatchBrush hatchBrush2(HatchStyleBackwardDiagonal, Color::Green, Color::White);

graph.DrawString(wcstr,  wcslen(wcstr), &Font(L"华文新魏", 100), PointF(0.0f, 110.0f), &hatchBrush2);

 

HatchBrush hatchBrush3(HatchStyleCross, Color::Blue, Color::White);

graph.DrawString(wcstr,  wcslen(wcstr), &Font(L"华文新魏", 100), PointF(0.0f, 220.0f), &hatchBrush3);

 

pause_run();

 

//3.纹理字

wcscpy(wcstr, L"圣诞快乐");

// Font font(L"华文新魏", 140);

TextureBrush textureBrush(&Image(L"2.jpg"));

graph.DrawString(wcstr,  wcslen(wcstr), &Font(L"华文新魏", 140), PointF(10.0f, 10.0f), &textureBrush);

pause_run();

 

 

//4.颜色渐变字

wcscpy(wcstr, L"祝大家圣诞快乐");

//Font font(L"华文行楷", 80);

 

Color cols[] = {Color::Red, Color::Orange, Color::Yellow, Color::Green, Color::Cyan, Color::Blue, Color::Purple, Color::Magenta};

 

REAL bps[] = {0.0f, 0.15f, 0.3f, 0.45f, 0.6f, 0.75f, 0.875f, 1.0f};

LinearGradientBrush brush(Point(10, 10), Point(810, 10), Color::Black, Color::White);

brush.SetInterpolationColors(cols, bps, 8);

graph.DrawString(wcstr,  wcslen(wcstr), &Font(L"华文行楷", 80), PointF(10.0f, 10.0f), &brush);

 

pause_run();

 

//5.空心字与彩心字

//利用GDI+的路径和路径渐变刷,来绘制空心和彩心字符串。

FontFamily ff(L"华文行楷");

wcscpy(wcstr, L"圣诞快乐");

REAL emSize = 120; // UnitWorld

graph.DrawString(wcstr, -1, &Font(L"华文行楷", emSize, 

FontStyleRegular, UnitWorld), PointF(0, 0), 

&SolidBrush(Color::Green));

GraphicsPath path, *pOutlinePath;

path.AddString(wcstr, -1, &ff, FontStyleRegular, emSize,

Point(0, 100), NULL); // 847个点

pen.SetColor(Color::Red);

graph.DrawPath(&pen, &path);

pOutlinePath = path.Clone();

pOutlinePath->Outline();

PathGradientBrush pgBrush(pOutlinePath);

n = pOutlinePath->GetPointCount(); // 1023个点

//delete cols;

Color *pcols = new Color[n];

 

for ( i = 0; i < n; i++) pcols[i] = 

Color(rand() % 255, rand() % 255, rand() % 255);

pgBrush.SetCenterColor(Color(rand() % 255, rand() % 255,

rand() % 255));

pgBrush.SetSurroundColors(pcols, &n);

graph.TranslateTransform(0.0f, 100.0f);

graph.FillPath(&pgBrush, &path);

 

pause_run();

 

//6 平滑处理与ClearType(清晰活字)技术

//利用Graphics类的两个文本绘制提示(hint)方法:

//TextRenderingHint GetTextRenderingHint(VOID) const;

//Status SetTextRenderingHint(TextRenderingHint newMode);

//来获取和设置文字绘制时的平滑处理方法。其中的枚举类型TextRenderingHint的定义为:

typedef enum {

    TextRenderingHintSystemDefault = 0, // 同系统平滑方式

    TextRenderingHintSingleBitPerPixelGridFit = 1, // 不消锯齿,网格匹配

    TextRenderingHintSingleBitPerPixel = 2, // 不消锯齿,不网格匹配

    TextRenderingHintAntiAliasGridFit = 3, // 消锯齿,网格匹配

    TextRenderingHintAntiAlias = 4, // 锯齿,不网格匹配

    TextRenderingHintClearTypeGridFit = 5 // 使用ClearType技术,不网格匹配

} TextRenderingHint;

SolidBrush textBrush6(Color::Black);

// Font font(L"Arial", 16);

wcscpy(wcstr, L"平滑字符串");

wchar_t buf[5];

for ( i = 0; i < 6; i++) {

// _itow_s(i, buf, 5, 10);

// wsprintf((char*)buf,L"%d",i);

graph.SetTextRenderingHint( enum TextRenderingHint(i));

// graph.DrawString(buf, -1, &font, PointF(5.0f, 20.0f * i), 

// &textBrush6);

graph.DrawString(wcstr, wcslen(wcstr), &Font(L"Arial", 16), 

PointF(30.0f, 20.0f * i), &textBrush6);

}

 

pause_run();

 

//(文本投影)

// Graphics graph(pDC->m_hDC);

SolidBrush grayBrush(Color::Gray), redBrush(Color::Red);

// font=Font(L"宋体", 100, FontStyleRegular, UnitPixel);

Matrix M;

M.Shear(1.5f, 0.0f);

M.Scale(0.97f, 0.5f);

M.Translate(90,-60);

graph.SetTransform(&M);

// M.Reset();

graph.DrawString(L"Merry Christmas", -1, &Font(L"宋体", 100), PointF(-168, 142), &grayBrush);//文本投影

graph.ResetTransform();

graph.DrawString(L"Merry Christmas", -1, &Font(L"宋体", 100), PointF(50, 0), &redBrush);//文本投影

 

pause_run();

 

//对路径进行矩阵变换。(大小渐变文字)

// GraphicsPath path; // 定义路径对象

path.Reset; // 定义路径对象

path.AddString(L"大小渐变文字测试", -1, // 将文本串加入路径

&FontFamily(L"隶书"), FontStyleRegular, 100, Point(0, 0), NULL);

RectF boundRect;

path.GetBounds(&boundRect); // 获取路径的界限矩形

 M.Reset(); // 定义矩阵对象(单位阵)

M.Translate(-(boundRect.X + boundRect.Width / 2), 

-(boundRect.Y + boundRect.Height / 2)); // 平移原点到文本路径的中心

path.Transform(&M); // 更改路径的中心点

n = path.GetPointCount(); // 获取路径中的点数

PointF *points = new PointF[n]; // 动态创建点数组

path.GetPathPoints(points, n); // 获取路径的点数组

BYTE *types = new BYTE[n]; // 动态创建类型数组

path.GetPathTypes(types, n); // 获取路径类型数组(用于路径重构)

for ( i= 0; i < n; i++) // 根据路径点到中心的距离,按比例修改点的y值

points[i].Y *= 2*(boundRect.Width-abs(points[i].X))/boundRect.Width;

GraphicsPath newPath(points, types, n); // 用新的路径点构造新路径

CRect crect(10,10,100,100);

// GetClientRect(&crect); // CRect

// Graphics graph(pDC->m_hDC); // 将坐标原点移到窗口中心:

graph.TranslateTransform(REAL(crect.Width()/2)+400,REAL(crect.Height()/2));

graph.FillPath(&SolidBrush(Color::Green), &newPath);//填充路径(绘制文本串) 

delete points; // 陈宝楷_add 

delete types;  // 陈宝楷_add

graph.TranslateTransform(-REAL(crect.Width()/2)-400,-REAL(crect.Height()/2));

 

pause_run();

M.Reset();

graph.SetTransform(&M);

 

 

//镜像文本)

// Graphics graph(pDC->m_hDC);

// font=Font(L"华文行楷", 50, FontStyleRegular, UnitPixel); 

Font ifont(L"华文行楷", 50, FontStyleItalic, UnitPixel);

SolidBrush  mirrorBrush(Color::Gray);//textBrush(Color::Blue),

SolidBrush  mirrorBrush1(Color::Red);

SolidBrush mirrorBrush2(Color::Green);

SolidBrush mirrorBrush3(Color::Blue);

str=L"圣诞快乐"; // 普通文本输出

 

c2w(wcstr ,str.GetBuffer(str.GetLength()),sizeof(wcstr));

 

graph.DrawString(wcstr, wcslen(wcstr), &Font(L"华文行楷",50), PointF(250.0f, 10.0f), &textBrush);

// 垂直镜像文本输出(上下倒置)

graph.ScaleTransform(1, -1);

graph.TranslateTransform(0, -120);

graph.DrawString(wcstr, wcslen(wcstr), &ifont, PointF(250.0f, 10.0f), &mirrorBrush1);

graph.ResetTransform(); // 陈宝楷_???// 重置所有变换(还原为恒等变换)

// 水平镜像文本输出(左右倒置)

graph.ScaleTransform(-1, 1);

graph.TranslateTransform(-300, 0);

graph.DrawString(wcstr, wcslen(wcstr), &Font(L"华文行楷",50), PointF(10.0f, 10.0f), &mirrorBrush2);

// 垂直+水平镜像文本输出(上下左右全倒置)

graph.ResetTransform();

graph.ScaleTransform(-1, -1);

graph.TranslateTransform(-300, -120);

graph.DrawString(wcstr, wcslen(wcstr), &ifont, PointF(10.0f, 10.0f), &mirrorBrush3);

 

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值