Chinaunix首页 | 论坛 | 博客
  • 博客访问: 512969
  • 博文数量: 158
  • 博客积分: 4015
  • 博客等级: 上校
  • 技术积分: 1711
  • 用 户 组: 普通用户
  • 注册时间: 2009-01-27 14:00
文章分类

全部博文(158)

文章存档

2010年(71)

2009年(87)

我的朋友

分类: C/C++

2010-02-07 18:36:49

先看看CFont::CreateFont:

BOOL CreateFont(
   int nHeight,
   int nWidth,
   int nEscapement,
   int nOrientation,
   int nWeight,
   BYTE bItalic,
   BYTE bUnderline,
   BYTE cStrikeOut,
   BYTE nCharSet,
   BYTE nOutPrecision,
   BYTE nClipPrecision,
   BYTE nQuality,
   BYTE nPitchAndFamily,
   LPCTSTR lpszFacename
);

说实在的,有点复杂,不在于参数的长短,在于有些参数根本很难理解什么意思。
这是一般的参数配置:

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


创建一个字体,有点受不了。
幸好CFont给我们封装了另外几个函数,使用起来比较简单。
1.CreatePointFont:

This function provides a simple way to create a font of a specified typeface and point size.



BOOL CreatePointFont(
   int nPointSize,
   LPCTSTR lpszFaceName,
   CDC* pDC = NULL
);

比如说font.CreatePointFont(120, _T("新宋体"));
创建一个12点大小的宋体字体。

如果我们要加粗或者添加下划线怎么办?
看下面的例子,我只展示了OnPaint处理:

void CMainWindow::OnPaint() {
    CPaintDC dc(this);
    RECT rect;
    GetClientRect(&rect);
    LOGFONT lf;
    ::ZeroMemory(&lf, sizeof(lf));
    lf.lfHeight = 720;
    lf.lfWeight = FW_BOLD;
    lf.lfUnderline = TRUE;
    lf.lfItalic = TRUE;
    lf.lfStrikeOut = TRUE;
    ::lstrcpy(lf.lfFaceName, _T("微软雅黑"));
    CFont font;
    font.CreatePointFontIndirect(&lf);
    dc.SelectObject(&font);
    dc.SetTextColor(RGB(200,0,255));
    dc.DrawText("你好世界!\nHello MFC!", &rect, 0);
}

了呼?
运行结果:

阅读(6210) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~