WEB开发网
开发学院软件开发VC WTL字体类 阅读

WTL字体类

 2010-07-06 20:42:52 来源:WEB开发网   
核心提示:说明类中字体可以设置为以下风格 ,也可以对它们进行或操作:加重Bold (CWindowFont::typeBold)斜体Italic (CWindowFont::typeItalic)下划线 (CWindowFont::typeUnderline)两倍高度 (CWindowFont::typeDoubleHeight

说明

类中字体可以设置为以下风格 ,也可以对它们进行或操作:

加重Bold (CWindowFont::typeBold)
   斜体Italic (CWindowFont::typeItalic)
   下划线 (CWindowFont::typeUnderline)
   两倍高度 (CWindowFont::typeDoubleHeight)

CWindowFont部分源码

CWindowFont类的部分源码如下所示:

#pragma once
#include
// LOGFONT 结构的包裹类
class CLogFont : public LOGFONt
{
public:
  CLogFont()
  {
    memset(this, 0, sizeof(LOGFONT));
  }
};
// 建立基于指定窗口的字体
class CWindowFont : public CFont
{
public:
  //字体风格
  typedef enum tagEType
  {
    typeNormal    = 0x00,
    typeBold     = 0x01,
    typeItalic    = 0x02,
    typeUnderline  = 0x04,
    typeDoubleHeight = 0x08,
  } EType;
public:
  CWindowFont() : CFont()
  {
  }

  /// hWnd -窗口句柄
  /// nType - 字体风格
  CWindowFont(HWND hWnd, int nType)
  {
    // HWND不能为NULL
    ATLASSERT(hWnd != NULL);
    //创建字体
    Create(hWnd, nType);
  }

  virtual ~CWindowFont()
  {
  }
public:
  //创建字体
  // hWnd -窗口句柄
  // nType -字体风格
  //成功则返回TRUE
  bool Create(HWND hWnd, int nType)
  {
    // 窗口句柄不能为NULL
    ATLASSERT(hWnd != NULL);
    ATLASSERT(::IsWindow(hWnd) != FALSE);
    // 获得当前窗口的字体
    HFONT hFont = (HFONT)::SendMessage(hWnd, WM_GETFONT, 0, 0);
    // 是否获得当前字体成功?
    if (hFont == NULL)
      return false;

    CLogFont lf;
    // 填充 LOGFONT结构
    if (::GetObject(hFont, sizeof(lf), &lf) == 0)
      return false;
    // 分离LOGFONT成员变量
    if (nType & typeBold)
      lf.lfWeight = FW_BOLD;
    if (nType & typeItalic)
      lf.lfItalic = TRUE;
    if (nType & typeUnderline)
      lf.lfUnderline = TRUE;
    if (nType & typeDoubleHeight)
      lf.lfHeight *= 2;

    // 建立新字体
    return CreateFontIndirect(&lf) ? true : false;
  }

  //建立新字体并应用到控件上去
  bool Apply(HWND hWnd, int nType, UINT nControlID)
  {
    // 先建立字体
    if (!Create(hWnd, nType))
      return false;
    // 应用到控件上
    CWindow wndControl = ::GetDlgItem(hWnd, nControlID);
    ATLASSERT(wndControl != NULL);
    wndControl.SetFont(m_hFont);
    return true;
  }
};

本文配套源码

上一页  1 2 

Tags:WTL 字体

编辑录入:爽爽 [复制链接] [打 印]
赞助商链接