码迷,mamicode.com
首页 > 编程语言 > 详细

MFC中GDI之CFont(字体)

时间:2019-11-27 23:22:00      阅读:142      评论:0      收藏:0      [点我收藏+]

标签:created   dfa   lfw   信息   cpp   nal   isp   rect   else   

字体主要是用于修饰文字输出的形状、高度、宽度、粗度、倾斜、删除线等。

BOOL CreateFontIndirect(const LOGFONT* lpLogFont); 根据LOGFONT结构体创建一个字体
BOOL CreateFont(
     int nHeight,
     int nWidth,
     int nEscapenment,
     intnOrientation,
     int nWeight,
     BYTE bItalic,
     BYTE bUnderline,
     BYTE cStrikeOut,
     BYTE nCharset,
     BYTE nOutPrecision,
     BYTE nClipPrecision,
     BYTE nQuality,
     BYTE nPitchAndFamily,
     LPCTSTR lpszFacename
    );
根据指定数值创建字体,其参数与LOGFONT成员一致,故等价于 CreateFontIndirect
BOOL CreatePointFont(int nPointSize, LPCTSTR lpszFaceName, CDC* pDC = NULL); 根据字体的名字和高度创建字体
BOOL CreatePointFontIndirect(const LOGFONT* lpLogFont, CDC* pDC = NULL); 根据LOGFONT结构体创建点字体
static CFont* FromHandle(HFONT hFont) 将HFONT句柄转换为CFont对象
operator HFONT() const 从CFont对象中获取HFONT句柄
int GetLogFont(LOGFONT* pLogFont); 获取字体的名称和高、宽等属性信息

wingdi.h中定义:
#define ANSI_CHARSET  0    #define GB2312_CHARSET   134

/* Logical Font */
#define LF_FACESIZE         32

typedef struct tagLOGFONTA
{
    LONG      lfHeight;                //字体的高度
    LONG      lfWidth;                 //字体的宽度,取 0 :自适应
    LONG      lfEscapement;            //设定字符串底线与水平线的夹角,以0.1° 为单位
    LONG      lfOrientation;           //设定每一个字符底线与水平线的夹角,以0.1° 为单位
    LONG      lfWeight;                //设置字体的粗细,范围 0-1000,400为正常粗细,700为粗,若取 0 ,则选择默认粗细
    BYTE      lfItalic;                //斜体
    BYTE      lfUnderline;             //下划线
    BYTE      lfStrikeOut;             //删除线
    BYTE      lfCharSet;               //指定字符集
    BYTE      lfOutPrecision;          //指定输出时字体的精度  OUT_DEFAULT_PRECIS
    BYTE      lfClipPrecision;         //指定输出时字体被裁减的精度 CLIP_DEFAULT_PRECIS
    BYTE      lfQuality;               //指定输出的质量 DEFAULT_QUALITY
    BYTE      lfPitchAndFamily;        //指定字体的斜度和字体的类型 DEFAULT_PITCH | FF_SWISS FF_ROMAN
    CHAR      lfFaceName[LF_FACESIZE]; //字体的名称
} LOGFONTA, *PLOGFONTA, NEAR *NPLOGFONTA, FAR *LPLOGFONTA;
typedef struct tagLOGFONTW
{
    LONG      lfHeight;
    LONG      lfWidth;
    LONG      lfEscapement;
    LONG      lfOrientation;
    LONG      lfWeight;
    BYTE      lfItalic;
    BYTE      lfUnderline;
    BYTE      lfStrikeOut;
    BYTE      lfCharSet;
    BYTE      lfOutPrecision;
    BYTE      lfClipPrecision;
    BYTE      lfQuality;
    BYTE      lfPitchAndFamily;
    WCHAR     lfFaceName[LF_FACESIZE];
} LOGFONTW, *PLOGFONTW, NEAR *NPLOGFONTW, FAR *LPLOGFONTW;

#ifdef UNICODE
typedef LOGFONTW LOGFONT;
typedef PLOGFONTW PLOGFONT;
typedef NPLOGFONTW NPLOGFONT;
typedef LPLOGFONTW LPLOGFONT;
#else
typedef LOGFONTA LOGFONT;
typedef PLOGFONTA PLOGFONT;
typedef NPLOGFONTA NPLOGFONT;
typedef LPLOGFONTA LPLOGFONT;
#endif // UNICODE

 

 微软提供的例子:

技术图片
// The code fragment shows how to create a font object,
// select the font object into a DC (device context) for text
// drawing, and finally delete the font object.
 
// Initializes a CFont object with the specified characteristics. 
CFont font;
VERIFY(font.CreateFont(
   12,                        // nHeight
   0,                         // nWidth
   0,                         // nEscapement
   0,                         // nOrientation
   FW_NORMAL,                 // nWeight
   FALSE,                     // bItalic
   FALSE,                     // bUnderline
   0,                         // cStrikeOut
   ANSI_CHARSET,              // nCharSet
   OUT_DEFAULT_PRECIS,        // nOutPrecision
   CLIP_DEFAULT_PRECIS,       // nClipPrecision
   DEFAULT_QUALITY,           // nQuality
   DEFAULT_PITCH | FF_SWISS,  // nPitchAndFamily
   _T("Arial")));                 // lpszFacename
 
// Do something with the font just created...
CClientDC dc(this);  
CFont* def_font = dc.SelectObject(&font);
dc.TextOut(5, 5, _T("Hello"), 5);
dc.SelectObject(def_font);
 
// Done with the font.  Delete the font object.
font.DeleteObject(); 
View Code

 

MFC中GDI之CFont(字体)

标签:created   dfa   lfw   信息   cpp   nal   isp   rect   else   

原文地址:https://www.cnblogs.com/htj10/p/11946075.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!