tags:

views:

212

answers:

1

I created a font object and selected it into the device content, then calculate the extent of the string using WIN32 API GetTextExtentExPoint, but the extent i got is the extent while using system default font. For example, when i using system default font, the extent of the string is 36 pixel width and 16 pixel height, and 72 pixel width and 24 pixel height while using the font i created. But, i always got 36 pixel no mater using system default font or the font i created. What's the problem with my codes?

Codes:

HDC hDC = GetDC();
ATLASSERT(hDC);

HFONT _hFontTitle = 0;
HFONT hSysFont = (HFONT)GetCurrentObject(hDC, OBJ_FONT);
ATLASSERT(hSysFont);
LOGFONT lf;
if(0 == GetObject(hSysFont, sizeof(LOGFONT), &lf))
    _hFontTitle = CreateFont(16, 12, 0, 0, FW_BOLD, FALSE, FALSE, FALSE, DEFAULT_CHARSET, OUT_DEFAULT_PRECIS, CLIP_DEFAULT_PRECIS, PROOF_QUALITY, FIXED_PITCH|FF_DONTCARE, _T("Fixedsys"));
else{
    lf.lfHeight = 16;
    lf.lfWidth  = 12;
    lf.lfWeight = FW_BOLD;

    _hFontTitle = CreateFontIndirect(&lf);
    ATLASSERT(_hFontTitle);
}
HFONT _hFontContent = 0;
HFONT hSysFont = (HFONT)GetCurrentObject(hDC, OBJ_FONT);
LOGFONT lf;
if(0 == GetObject(hSysFont, sizeof(LOGFONT), &lf))
        _hFontContent = CreateFont(12, 9, 0, 0, FW_NORMAL, FALSE, FALSE, FALSE, DEFAULT_CHARSET, OUT_DEFAULT_PRECIS, CLIP_DEFAULT_PRECIS, PROOF_QUALITY, FIXED_PITCH|FF_DONTCARE, _T("Fixedsys"));
else{
    lf.lfHeight = 12;
    lf.lfWidth  = 9;
    lf.lfWeight = FW_NORMAL;

    _hFontContent = CreateFontIndirect(&lf);
    ATLASSERT(_hFontContent);
}

SIZE sizeTitle = TextMetricsHelper::GetTextLayout(hDC, _szTitle.c_str(), _szTitle.size(), _hFontTitle);
SIZE sizeContent = TextMetricsHelper::GetTextLayout(hDC, _szContent.c_str(), _szContent.size(), _hFontContent);

While GetTextLayout is:

SIZE GetTextLayout(HDC hDC, LPCTSTR lpszText, unsigned int cbText, HFONT hFont)
{
    //RECT rcText = {0, 0, 8, 10};

    HFONT hOldFont = (HFONT)SelectObject(hDC, (HGDIOBJ)hFont);

    SIZE textSize;

    GetTextExtentPoint32(hDC, lpszText, cbText, &textSize);
    //GetTextExtentExPoint(hDC, lpszText, cbText, 0, 0, 0, &sizeOfTitle);
    //DrawText(hDC, lpszText, cbText, &rcText, DT_CALCRECT);

    SelectObject(hDC, hOldFont);

    return textSize;
}
A: 

Don't know much about creating fonts and so on, but I notice that both your CreateFont lines assign their result to the same variable _hFontTitle. Is this the intention ?

Edelcom
Oh, thank you, but, it will never been executed..
Lixiang Xie