2010年2月8日 星期一

CuFontManager


// CuFontManager.h



class CuFontManager  
{
private:
    static  CuFontManager   m_pInstance; 
    map<int, HFONT>       m_mapFontHandle;

    CuFontManager();
public:
    virtual ~CuFontManager();

    static CuFontManager *Instance()
    {
        return &m_pInstance;
    }

    HFONT GetFontHandle(HFONT hFont,int iFontSize);
};


// CuFontManager.cpp



CuFontManager CuFontManager::m_pInstance;

CuFontManager::CuFontManager()
{

}

CuFontManager::~CuFontManager()
{
    map<int, HFONT>::iterator it;

    for(it = m_mapFontHandle.begin(); it != m_mapFontHandle.end(); it++) {
        HFONT hFontCreated = it->second;
        DeleteObject(hFontCreated);
    }

    m_mapFontHandle.clear();
}

HFONT CuFontManager::GetFontHandle(HFONT hFont,int iFontSize)
{
    HFONT hFontCreated;
    LOGFONT lf;
    map<int, HFONT>::const_iterator it;

    it = m_mapFontHandle.find(iFontSize);

    if(it == m_mapFontHandle.end()) {
        if (GetObject(hFont, sizeof LOGFONT, &lf)) {
            HDC hDC;

            hDC = ::GetDC(NULL);
            lf.lfHeight=-((iFontSize * GetDeviceCaps(hDC, LOGPIXELSY)) / 72);
            lf.lfWeight = FW_SEMIBOLD;

            hFontCreated = CreateFontIndirect(&lf);
            m_mapFontHandle.insert(map<int, HFONT>::value_type(iFontSize, hFontCreated));
            ReleaseDC(NULL, hDC);
        }
    }
    else
        hFontCreated = it->second;

    return hFontCreated;
}

沒有留言:

張貼留言