MFC中GDI之CFont(字体)

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

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]; //字体的名称 注意:LF_FACESIZE=32
} 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

 例子:

void CMFC7_6FontDlg::OnPaint() 
{

    CPaintDC dc(this); // device context for painting
    CFont *pFont = GetFont(); // CWnd::GetFont() 获取当前字体
/*    LOGFONT lf;
    pFont->GetLogFont(&lf); // lf : -12 400 134 宋体  
*/
    dc.SetBkMode(TRANSPARENT); // 设置文字背景透明

    Normal(&dc, pFont);
    Height(&dc, pFont);
    Weight(&dc, pFont);
    Width(&dc, pFont);
    Other(&dc, pFont);
    Escape(&dc, pFont);
    CreateFont(&dc);
    dc.TextOut(10,10,"缺省字体");
}

// The system calls this to obtain the cursor to display while the user drags
//  the minimized window.
HCURSOR CMFC7_6FontDlg::OnQueryDragIcon()
{
    return (HCURSOR) m_hIcon;
}

void CMFC7_6FontDlg::Normal(CDC *pDC, CFont *pFont)
{
    LOGFONT lf;
    pFont->GetLogFont(&lf); // lf : -12 400 134 宋体
    pFont = pDC->SelectObject(pFont); // 返回前一个字体(即缺省字体)
    pDC->TextOut(10,40,"对话框字体-静态文本");
    pDC->SelectObject(pFont); // 恢复为前字体样式
}

void CMFC7_6FontDlg::Height(CDC *pDC, CFont *pFont)
{
    LOGFONT lf;
    pFont->GetLogFont(&lf); // lf : -12 400 134 宋体
    lf.lfHeight = 2*lf.lfHeight;
    CFont font ;
    font.CreateFontIndirect(&lf);
    pFont = pDC->SelectObject(&font); // 返回前一个字体(即缺省字体)
    pDC->TextOut(10,60,"字体高度(大小)是原来的2倍");
    pDC->SelectObject(pFont); // 恢复为前字体样式
}

void CMFC7_6FontDlg::Weight(CDC *pDC, CFont *pFont)
{
    LOGFONT lf;
    pFont->GetLogFont(&lf); // lf : -12 400 134 宋体
    lf.lfWeight = 700; // 粗字体
    CFont font ;
    font.CreateFontIndirect(&lf);
    pFont = pDC->SelectObject(&font); // 返回前一个字体(即缺省字体)
    pDC->TextOut(10,100,"测试粗字体");
    pDC->SelectObject(pFont); // 恢复为前字体样式
}

void CMFC7_6FontDlg::Width(CDC *pDC, CFont *pFont)
{
    LOGFONT lf;
    pFont->GetLogFont(&lf); // lf : -12 400 134 宋体
    lf.lfWidth = lf.lfHeight; // width 默认为0  lfHeight = -12
    CFont font ;
    font.CreateFontIndirect(&lf);
    
    pFont = pDC->SelectObject(&font); // 返回前一个字体(即缺省字体)
    pDC->TextOut(10,120,"测试字体宽度");
    pDC->SelectObject(pFont);         // 恢复为进入函数前字体样式
}

void CMFC7_6FontDlg::Other(CDC *pDC, CFont *pFont)
{
    LOGFONT lf;
    pFont->GetLogFont(&lf); // lf : -12 400 134 宋体
    lf.lfItalic = TRUE;        // 斜体
    lf.lfUnderline = TRUE;    // 下划线
    lf.lfStrikeOut = TRUE;    // 删除线
    lf.lfHeight = 20;        // 字体大小
    strcpy(lf.lfFaceName, "楷体");      // 字体名称
    CFont font ;
    font.CreateFontIndirect(&lf);
    
    pFont = pDC->SelectObject(&font); // 返回前一个字体(即缺省字体)
    pDC->TextOut(10,140,"测试斜体");
    pDC->SelectObject(pFont);         // 恢复为进入函数前字体样式
}

void CMFC7_6FontDlg::Escape(CDC *pDC, CFont *pFont) //字符串倾斜
{
    LOGFONT lf;
    pFont->GetLogFont(&lf); // lf : -12 400 134 宋体
    lf.lfEscapement = 200;
    lf.lfHeight = 25;
    CFont font ;
    font.CreateFontIndirect(&lf);
    
    pFont = pDC->SelectObject(&font); // 返回前一个字体(即缺省字体)
    pDC->TextOut(10,200,"测试字符串倾斜");
    pDC->SelectObject(pFont);         // 恢复为进入函数前字体样式

}

void CMFC7_6FontDlg::CreateFont(CDC *pDC)
{
    LOGFONT lf = {24}; //第一个成员为24,其他初始化0
    strcpy(lf.lfFaceName, "隶书");
    lf.lfCharSet = 134; // #define GB2312_CHARSET 134
    CFont font;
    font.CreateFontIndirect(&lf);
    CFont* pOldFont = pDC->SelectObject(&font);
    pDC->TextOut(10,250,"自己创建字体");

/*    CFont ft;
    ft.CreatePointFont(120,"楷体"); // 大小与实际大小相差10倍
    pDC->SelectObject(&ft);
    pDC->TextOut(10,270,"自己创建字体");
*/
    pDC->SelectObject(pOldFont);
}
View Code

结果:

-----------------------------------------

参考:吕鑫MFC7.6章节

常记溪亭日暮,沉醉不知归路。兴尽晚回舟,误入藕花深处。争渡,争渡,惊起一滩鸥鹭。

昨夜雨疏风骤,浓睡不消残酒。试问卷帘人,却道海棠依旧。知否?知否?应是绿肥红瘦。
原文地址:https://www.cnblogs.com/htj10/p/11946075.html