Question

I read code in MFC, but was confused with the code below:

void EditView::ResetDefaultFont()
{
    HFONT hFont = (HFONT)::GetStockObject(DEFAULT_GUI_FONT);
    CDC* pDC = GetDC();

    CFont* pFont = pDC->SelectObject(CFont::FromHandle(hFont));
    pDC->SelectObject(pFont);
    ::DeleteObject(hFont);

}

Why CDC Select the default font first(CFont* pFont = pDC->SelectObject(CFont::FromHandle(hFont));), but select pFont again?

Was it helpful?

Solution

The first SelectObject call changes the font selected in the device context.

The second SelectObject call resets the font to whatever it was before the first call.

While that answers the "why" for the second call, which is what you ask about, I do not have any idea what the point of doing the complete call sequence is. I find no documentation results for ResetDefaultFont, neither online in MSDN Library nor in the local Visual Studio 2012 help. Just to be thorough I created a new default MFC project in VS 2012, and used the identifier ResetDefaultFont in the constructor of a class derived from CEditView. It did not compile: no such.

So,

where did you get that ResetDefaultFont function from?

OTHER TIPS

The answer is quite simple. This code is just for getting the current font of the DC. If they had placed the code following these statements, it would have been obvious.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top