Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to create a bold and italic label in MFC?

Tags:

c++

label

mfc

Please do not mark it as a dupe of this question just yet:

Bold labels in MFC

That question does not help me; for some reason I do not see the rich edit control. Instead I believe I have to do it in code. here is a sample I found:

http://www.tech-archive.net/Archive/VC/microsoft.public.vc.mfc/2006-10/msg00245.html

My problem is that I prefer not to re-invent the wheel and test for errors myself or through QA.

Someone must have implemented this before. Please share your code.

What I would like to do is:

  • Keep the same font size, family, etc. as in the already created label, but make it bold and italic as well.
  • Keep the memory footprint reasonably low (do not create any new unnecessary objects), but do not get the app into an inconsistent state either.

I appreciate your help.

like image 969
Hamish Grubijan Avatar asked Apr 13 '10 17:04

Hamish Grubijan


1 Answers

You will want to do the following before the static text control is shown on the parent window.

  1. Get a handle to the window: CWnd * pwnd = GetDlgItem(IDC_LABEL);
  2. Get the current font for the static text: CFont * pfont = pwnd->GetFont();
  3. Get the characteristics of the font: LOGFONT lf; pfont->GetLogFont(&lf);
  4. Change the lfWeight and lfItalic fields in lf.
  5. Put a CFont object in your parent window, so it will exist for the entire lifetime of the child window.
  6. Initialize the CFont: m_font.CreateFontIndirect(&lf);
  7. Set the font into the static text window: pwnd->SetFont(&m_font);
like image 63
Mark Ransom Avatar answered Oct 10 '22 01:10

Mark Ransom