Link to home
Start Free TrialLog in
Avatar of has
has

asked on

I cant get the font correct ?

I have a CWnd drived window that has a
pointer to a CComboBox m_pCBox.
and a CFont* pointer m_pFont.
in construction, I get it as:

CDC* pCBoxpDC = m_pCBox->GetDC();
m_pFont = pCBoxpDC->GetCurrentFont();
m_pCBox->ReleaseDC(pCBoxpDC);

and use it in the windows paint as:

void CNewWnd::OnPaint()
{
CPaintDC dc(this);
dc.SetBkMode(TRANSPARENT);
CRect rc;
GetClientRect(rc);
CFont* pFont = dc.SelectObject(m_pFont);
dc.DrawText(_T("text"), rc, DT_LEFT);
dc.SelectObject(pFont);
}

But text shows bold (darker and ticker
than the combo text)

Avatar of has
has

ASKER

When I changed the font in ComboBox,
text font is very different.
The CFont* returned by CDC::GetCurrentFont may be a temporary object, meaning that you can't use it out of scope of a message. Try moving the code into the OnPaint.
Avatar of has

ASKER

no luck with this

OnPaint()
{
      CDC* pCBoxpDC = m_pCBox->GetDC();

      CPaintDC dc(this); // device context for painting
      dc.SetBkMode(TRANSPARENT);
      CRect rc;
      GetClientRect(rc);
      CFont* pFont = dc.SelectObject(pCBoxpDC->GetCurrentFont());
      dc.DrawText(m_tip, rc, DT_LEFT);
      dc.SelectObject(pFont);

      m_pCBox->ReleaseDC(pCBoxpDC);
      }
Does CDC::GetCurrentFont return non-NULL? If so, get the font information with CFont::GetLogFont. Then, create your own font object with theLOGFONT structure returned.
ASKER CERTIFIED SOLUTION
Avatar of liaobin
liaobin

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
>it restore original font(system font) after it finished drawing.

liaobin is on the right track. I feel that I was stupid.
Avatar of has

ASKER

Are you telling me that there is a
GetFont() of CDC, or ?? how do I
solve the problem then ??
GetFont is a member function of CWnd.
Avatar of has

ASKER

Thanks to both of you.