一 首先下载,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);

}

VC GDI+ 字体的特效相关推荐

  1. CSS3发光文字字体切换特效

    index.html代码 <!DOCTYPE html> <html lang="en" > <head> <meta charset=& ...

  2. html字体自动变大特效,css怎么实现字体放大特效?(首字符变化代码实测)

    css在网页制作中有着在非常重要的作用,一个页面或者页面中的一篇文章除了内容上的吸引力,如何在样式上抓住用户的眼球呢?比如本篇文章就是介绍css字体放大的特效及css首字符放大的效果展示实现.有需要的 ...

  3. VC++设置字体 字幕变色功能的实现 DrawText函数 本章小结

    目录 设置字体 字幕变色功能的实现 DrawText函数 本章小结 接上:VC++字符输入 ASCII码 接下: 设置字体 MFC提供了一个CFont类专门用来设置字体.这个类派生于CGdiObjec ...

  4. VC GDI+: error C2660: 'new' : function does not take 3 parameters

    今天在用GDI+写程序时,有 bmp = new Bitmap(L"E:\\1.png"); 用VC6 SP6或VS2005编译错误为error C2660: 'new' : fu ...

  5. 鼠标悬停文字断裂字体js特效

    下载地址 一款鼠标悬停文字断裂字体特效,该特效实现了鼠标悬停文字断裂的动画特效,能够让您的网页生动起来. dd:

  6. GDI+字体Font

    字体,对于大部分人来说都不陌生,在文本编辑软件中(如 Word)字体是必不可少的,同样,在GDI+中,绘制字符串也是需要字体的.在介绍字体Font类的使用之前,先引入一些与其有关的类或者枚举: (1) ...

  7. html字体变色特效,css字体特效,渐变颜色+动画效果

    css属性中有一个background-clip,用于设置绘图的背景,它的值可以是content-box,padding-box,border-box,text,其中text就是把颜色绘制到文字上,还 ...

  8. CSS技巧:字体霓虹灯特效

    关键点 多重阴影的过渡效果与白色字体的叠加 <div><p class="pink">PINK</p> </div> <div ...

  9. VC GDI+ 窗口截图内存BMP转JPG,压缩、JPG再转IStream流,IStream流再转 BYTE

    最近用到 PC远程监控,用GDI+屏幕截图内存BMP转JPG,压缩 ,JPG再转IStream流,IStream流再转BYTE,通过SOCKET 转发: /****************BMP转JP ...

最新文章

  1. GEO数据挖掘(2)-GEO数据库
  2. poj 2528 Mayor's posters (线段树+离散化)
  3. STM32F407的硬件I2C
  4. c语言循环拆分成和,C语言拆分循环链表程序
  5. html5绘制矩形动画,HTML5下绘制矩形教程
  6. python使用ssh 中文_Python3制作简易SSH登录工具
  7. ubuntu 下操作目录,出现Permission denied的解决办法
  8. matlab mex gcc 支持c99
  9. 好久不上来,发现这个世界变得真是快啊,都.NET 2.0 AJAX了~~
  10. D1-H哪吒 设置hdmi分辨率
  11. LDA模型实战(1)|理解模型的假设、要素与学习
  12. 成都电子信息学校计算机专业,成都电子信息学校2020招生简章
  13. 网页视频抓取插件-自动网页视频抓取插件
  14. C语言实战--DDOS攻击器
  15. 如何管理计算机软件,驱动人生怎么管理软件 让你轻松管理电脑中的程序
  16. 循序渐进实现仿QQ界面(三):界面调色与控件自绘
  17. 芯片设计流程及各步骤使用工具简介
  18. 手机便签内容修改后可以恢复吗?
  19. 光引发剂主要用途_可见光引发剂的应用
  20. c 语言中12u是多少,填空题总复习题及参考答案

热门文章

  1. 【吐血整理】Java语言基础学习资料(电子书+视频+视频)
  2. 关于 cc3k-villain 作业的思考
  3. 机器学习 - 机器学习名字的由来
  4. 2023亚马逊云科技中国峰会——Amazon DeepRacer
  5. 前端面试题汇总HTML篇
  6. 解读:Few-shot classification in Named Entity Recognition Task
  7. 阿里云企业官网建站标准版、高级版和尊贵版功能区别选择攻略
  8. 协议(七层协议、四层协议、五层协议)
  9. python+pandas气象数据处理
  10. R语言 台湾银行还款预测 数据预处理 距离判别(懒得再打一遍了,源文件再主页有)