GDI+学习及代码总结之------文本与字体.docx

上传人:A*** 文档编号:142724977 上传时间:2020-08-22 格式:DOCX 页数:16 大小:250.32KB
返回 下载 相关 举报
GDI+学习及代码总结之------文本与字体.docx_第1页
第1页 / 共16页
GDI+学习及代码总结之------文本与字体.docx_第2页
第2页 / 共16页
GDI+学习及代码总结之------文本与字体.docx_第3页
第3页 / 共16页
GDI+学习及代码总结之------文本与字体.docx_第4页
第4页 / 共16页
GDI+学习及代码总结之------文本与字体.docx_第5页
第5页 / 共16页
点击查看更多>>
资源描述

《GDI+学习及代码总结之------文本与字体.docx》由会员分享,可在线阅读,更多相关《GDI+学习及代码总结之------文本与字体.docx(16页珍藏版)》请在金锄头文库上搜索。

1、GDI+学习及代码总结之-文本与字体字体、字体系列基本概念与构造字体系列GDI+中将具有相同字样、包括不同风格的字体称为字体系列。字体从其系列中获得名称、重量(如粗体、规则、加亮)以及风格。例如Arial字体系列包含了下列4种字体:Arial Regular(规则)、Arial Bold(黑体)、Arial Italic(斜体)、Arial Bold Italic(粗斜体);在GDI+中输出文本之前,需要构造一个FontFamily对象和一个Font对象。FontFamily类的构造函数如下:FontFamily() FontFamily(name, fontCollection) name:

2、字体系列名。fontCollection:指向一个FontCollection对象(字体集),表明字体系列所属的私有字体集,默认为NULL示例(定义一个Arial Regular字体对象font)FontFamily fontFamily(LArial);Font font(&fontFamily,16,FontStyleRegular,UnitPixel);看几个结构类型1、LOGFONTA和LOGFONTW两个都是一样的,只是一个用于ASCII编码,一个用于UNICODE编码字体大小单位枚举(Unit)enum Unit UnitWorld,/世界坐标,非物理上的单位 UnitDispla

3、y,/使用和输出设备相同的单位,如输出设备为显示器,其单位为像素 UnitPixel,/像素 UnitPoint,/点(磅),1点等于1/72英寸 UnitInch,/英寸 UnitDocument,/1/300英寸 UnitMillimeter/毫米;字体风格枚举(FontStyle)enum FontStyle FontStyleRegular = 0,/常规 FontStyleBold = 1,/加粗 FontStyleItalic = 2,/斜体 FontStyleBoldItalic = 3,/粗斜 FontStyleUnderline = 4,/下划线 FontStyleStrik

4、eout = 8/强调线,即在字体中部划线;注意:在这几个样式中,常规、加粗、倾斜、粗斜不能同时使用。下划线和强调线风格可以同时使用,也就是说,字体风格由两部分组成,一部分与字体外形有关,另一部分是附加的线条风格(下划线和强调线),这两部分可以配合使用组成字体的整体风格。例如,要描述一个带下划线和强调线的粗斜体风格的字体,可以使用下面的组合来完成:FontStyleBoldItalic |FontStyleUnderline |FontStyleStrikeout 列举系统目前安装的字体信息我们可以通过InstalledFontCollection类的成员函数GetFamilies枚举安装在计

5、算机上的所有字体。GetFamilies函数返回的是一个FontFamily对象的数组。所以,在使用GetFamilies前,应该为FontFamily数组分配足够的内存空间。GetFamilies函数:Status GetFamilies( INT numSought, FontFamily* gpfamilies, INT* numFound) const;numSought:in字体集中的字体系列总数,通过InstalledFontCollection:GetFamilyCount()获取gpfamilies:out存储返回的已安装的字体系列的数组;numFound:out存储返回的字体

6、系列总数,数值与numSought一样;应用:列举出系统中的所有字体:SolidBrush solidbrush(Color:Black);FontFamily fontfamily(LArial);Font font(&fontfamily,8,FontStyleRegular,UnitPoint);int count=0;int found=0;WCHAR familyName100;/这里为了简化程序,分配足够大的空间WCHAR *familyList=NULL;FontFamily pFontFamily500;/同样,分配足够大的空间CRect rect;this-GetClient

7、Rect(&rect);RectF r(0,0,rect.Width(),rect.Height();InstalledFontCollection installedFontCollection;count=installedFontCollection.GetFamilyCount();installedFontCollection.GetFamilies(count,pFontFamily,&found);familyList=new WCHARcount*sizeof(familyName);wcscpy(familyList,L);/先清空familyList,wcscpy实现对宽字

8、节的复制操作for(int j=0;jcount;j+) pFontFamilyj.GetFamilyName(familyName); wcscat(familyList,familyName);/把familyName添加到familyList的最后 wcscat(familyList,L,);/wcscat实现对宽字节字符的添加操作graphics.DrawString(familyList,-1,&font,r,NULL,&solidbrush);delete familyList;注意:wcscpywcscat函数分别实现了对双字节字符(宽字符)的复制、添加操作,在ASCII码中对应

9、strcpy、strcat;字体边沿平滑处理大家都知道,当字体过大时,就会出现失真,边缘会出现锯齿,GDI+对字体边缘有一套自己的处理方式,是通过SetTextRenderingHint来实现的;Status SetTextRenderingHint( TextRenderingHint newMode);其中TextRenderingHint是个枚举类,枚举了几种处理边缘的方式enum TextRenderingHint TextRenderingHintSystemDefault = 0,/使用与系统相同的处理方式 TextRenderingHintSingleBitPerPixelGri

10、dFit,/不消除锯齿,使用网格匹配 TextRenderingHintSingleBitPerPixel,/不消除锯齿,不使用网格匹配 TextRenderingHintAntiAliasGridFit,/消除锯齿,使用网格匹配 TextRenderingHintAntiAlias,/消除锯齿,不使用网格匹配 TextRenderingHintClearTypeGridFit /在液晶显示器上使用ClearType技术增强字体清晰度;看示例: FontFamily fontfamily(L宋体); Font font(&fontfamily,60,FontStyleRegular,UnitP

11、ixel); /使用与系统相同的处理方式 graphics.SetTextRenderingHint(TextRenderingHintSystemDefault); graphics.DrawString(L什么玩意,-1,&font,PointF(0,0),&SolidBrush(Color:Green); /不消除锯齿,使用网格匹配 graphics.TranslateTransform(0,font.GetHeight(0.0f); graphics.SetTextRenderingHint(TextRenderingHintSingleBitPerPixelGridFit ); gr

12、aphics.DrawString(L什么玩意,-1,&font,PointF(0,0),&SolidBrush(Color:Green); /不消除锯齿,不使用网格匹配 graphics.TranslateTransform(0,font.GetHeight(0.0f); graphics.SetTextRenderingHint(TextRenderingHintSingleBitPerPixel ); graphics.DrawString(L什么玩意,-1,&font,PointF(0,0),&SolidBrush(Color:Green); /消除锯齿,使用网格匹配 graphics

13、.TranslateTransform(0,font.GetHeight(0.0f); graphics.SetTextRenderingHint(TextRenderingHintAntiAliasGridFit ); graphics.DrawString(L什么玩意,-1,&font,PointF(0,0),&SolidBrush(Color:Green); /消除锯齿,不使用网格匹配 graphics.TranslateTransform(0,font.GetHeight(0.0f); graphics.SetTextRenderingHint(TextRenderingHintAnt

14、iAlias ); graphics.DrawString(L什么玩意,-1,&font,PointF(0,0),&SolidBrush(Color:Green); /在液晶显示器上使用ClearType技术增强字体清晰度 graphics.TranslateTransform(0,font.GetHeight(0.0f); graphics.SetTextRenderingHint(TextRenderingHintClearTypeGridFit ); graphics.DrawString(L什么玩意,-1,&font,PointF(0,0),&SolidBrush(Color:Green);从上面可以看出,列出的这六种边缘处理方式中,最后一种枚举成员TextRenderingHintClearTypeGridFit的处理效果最好。这种处理方式使用了Microsoft特有的ClearType技术。是专在液晶显示器上使用的一种增强字体清晰度的技术。但这种技术有时会出现问题,用投影仪投射到白色墙壁上,会出出字体显示不正常的情况,而且对于老式的CRT显示器是根本不支持的。操作系统也必须是XP及以上的系统才行。所以一般用第一个参数,与系统处理方式相同就好。让系统决定我们适合哪种技术。FontFamily和Font在GDI+中输出文本之前,需要构造一个FontFamily对象和一个F

展开阅读全文
相关资源
正为您匹配相似的精品文档
相关搜索

最新文档


当前位置:首页 > IT计算机/网络 > 其它相关文档

电脑版 |金锄头文库版权所有
经营许可证:蜀ICP备13022795号 | 川公网安备 51140202000112号