1

I am having a message window to display an error message. In that message some text should be bold and underlined. I am using static text. I am using the following code.

m_font.CreateFont(10,0,0,0,FW_BOLD,0,0,0,0,0,0,0,0,"Arial");
GetDlgfItem(Id of the lable)->SendMessage(WM_SETFONT,WPARAM(HFONT)),0);

Using this I can make it as bold. But I am not able change the boldness of the text. And how can I underline the text in the label.

Thanks in advance.

Jabberwocky
  • 48,281
  • 17
  • 65
  • 115
Prabhu Harin
  • 69
  • 2
  • 7

1 Answers1

5

Try This

CWnd * pwnd = GetDlgItem(LABEL_ID);
CFont * pfont = pwnd->GetFont();
LOGFONT lf; pfont->GetLogFont(&lf);
lf.lfItalic = TRUE;         //To Make Text Italic
lf.lfWeight = 500;          //To Make BOLD, Use FW_SEMIBOLD,FW_BOLD,FW_EXTRABOLD,FW_BLACK
lf.lfUnderline = TRUE;      //Underline Text
pfont->CreateFontIndirect(&lf);
pwnd->SetFont(pfont);

Or you can use

CFont *m_font;
m_font->CreateFont(10,0,0,0,FW_BOLD,0 , 1, 0, 0, 0, 0, 0, 0,_T("Arial"));
                                       ^^ 
                                  //(for underline)
GetDlgItem(IDC_MOUSEPOS)->SetFont(m_font);

http://msdn.microsoft.com/en-us/library/2ek64h34.aspx

Himanshu
  • 4,327
  • 16
  • 31
  • 39
  • The line pfont->CreateFontIndirect(&lf); throw exception, so you have to create new variable. CFont *new_font = new CFont; new_font->CreateFontIndirect(&lf); new_font->SetFont(new_font); And don't forget to delete it after use. delete new_font; – 0x000f Mar 31 '21 at 10:23