首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式碼之家 >> 編程語言 >  >> C語言 >> VC >> 關於VC++

WTL 字體輔助類 (CWindowFont) 簡化字體建立與套用

     編輯:關於VC++

前言

WTL (Windows Template Library) 輕量級但缺乏 MFC CFont 的便利封裝。本文提供 CWindowFont 類別:以視窗字體為基礎,透過旗標組合 (粗體/斜體/底線/雙倍高) 快速建立新字體並套用至控件,極大簡化 WTL 對話框/視窗字體客製化。

類別定義 (windowfont.h)


#pragma once
#include 
#include 

// LOGFONT 包裝
class CLogFont : public LOGFONT
{
public:
    CLogFont() { memset(this, 0, sizeof(LOGFONT)); }
};

// 字體建立與套用核心類
class CWindowFont : public CFont
{
public:
    enum EType {
        typeNormal       = 0x00,
        typeBold         = 0x01,
        typeItalic       = 0x02,
        typeUnderline    = 0x04,
        typeDoubleHeight = 0x08,
    };

    CWindowFont() : CFont() {}
    CWindowFont(HWND hWnd, int nType) { Create(hWnd, nType); }
    virtual ~CWindowFont() {}

    // 建立字體 (回傳 bool 成敗)
    bool Create(HWND hWnd, int nType)
    {
        ATLASSERT(hWnd && ::IsWindow(hWnd));
        HFONT hFont = (HFONT)::SendMessage(hWnd, WM_GETFONT, 0, 0);
        if (!hFont) return false;

        CLogFont lf;
        if (!::GetObject(hFont, sizeof(lf), &lf)) return false;

        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 nCtrlID)
    {
        if (!Create(hWnd, nType)) return false;
        CWindow wndCtrl = ::GetDlgItem(hWnd, nCtrlID);
        ATLASSERT(wndCtrl.m_hWnd);
        wndCtrl.SetFont(m_hFont);
        return true;
    }
};

WTL 對話框中使用範例


#include "windowfont.h"

class CMyDialog : public CDialogImpl
{
public:
    enum { IDD = IDD_MYDIALOG };

    CWindowFont m_fontBold;    // 粗體
    CWindowFont m_fontBig;     // 雙倍高
    CWindowFont m_fontBoldBig; // 粗體+雙倍高

    BOOL OnInitDialog(CWindow, LPARAM)
    {
        // 方式 1: 建立後手動套用
        if (m_fontBold.Create(m_hWnd, CWindowFont::typeBold))
            GetDlgItem(IDC_STATIC_TITLE).SetFont(m_fontBold);

        // 方式 2: 一鍵建立並套用
        m_fontBig.Apply(m_hWnd, CWindowFont::typeDoubleHeight, IDC_STATIC_SUBTITLE);

        // 方式 3: 組合旗標
        m_fontBoldBig.Create(m_hWnd, CWindowFont::typeBold | CWindowFont::typeDoubleHeight);
        GetDlgItem(IDC_STATIC_VERSION).SetFont(m_fontBoldBig);

        return TRUE;
    }
};

效果展示

關於對話框:版本資訊使用雙倍高粗體字體;嚚導首頁:標題使用雙倍高字體增強視覺層次。

設計優勢

  • 零依賴:僅需 WTL 核心標頭,無 MFC/STL 依賴。
  • 旗標組合:typeBold | typeItalic | typeUnderline 任意組合。
  • 自動繼承基礎屬性:字型名稱、字集、Precision、Quality 等沿用視窗預設字體,保持風格一致。
  • RAII 管理:繼承 CFont,析構自動 DeleteObject,無記憶體洩漏。
  • 兩種用法:Create 靈活控制時機,Apply 一行搞定。

擴充建議

  • 加入 typeStrikeOut (刪除線)、typeAntiAlias (ClearType) 旗標。
  • 支援 lfCharSet 覆寫 (如 CHINESEBIG5_CHARSET / GB2312_CHARSET)。
  • 提供 CreateFromLogFont 接受完整 LOGFONT 進階客製化。
  • 整合 CFontHandle 互轉,方便搭配 GDI+ / DirectWrite。

完整 windowfont.h 可直接複製到專案 include 目錄使用,適用 WTL 7.0/7.1/8.0+。

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式碼之家 All Rights Reserved