Put text on label field into bold

Hi

Does anyone know how to put text on a label field into bold?
I used a LOGFONT for my new font but i want it to be set on initdialog when the dialog shows, and you don't have a CDC pointer available in the initdialog function.

I have it like this:
LOGFONT logFont;
logFont.lfHeight = 8;
logFont.lfWidth = 0;
logFont.lfEscapement = 0;
logFont.lfOrientation = 0;
logFont.lfWeight = FW_BOLD;
logFont.lfItalic = 0;
logFont.lfUnderline = 0;
logFont.lfStrikeOut = 0;
logFont.lfCharSet = ANSI_CHARSET;
logFont.lfOutPrecision = OUT_DEFAULT_PRECIS;
logFont.lfClipPrecision = CLIP_DEFAULT_PRECIS;
logFont.lfQuality = PROOF_QUALITY;
logFont.lfPitchAndFamily = VARIABLE_PITCH | FF_ROMAN;
strcpy(logFont.lfFaceName, 'Times New Roman');

CFont font;
font.CreateFontIndirect(&logFont);
CFont* oldFont = pDC->SelectObject(&font);
m_FuncNedCtrl.SetWindowText("Example");

pDC->SelectObject(oldFont);

Can anyone help?


Thanks for the effort

Greetings Jens
JensBAsked:
Who is Participating?
 
AlexFMConnect With a Mentor Commented:
Remove this line:

 //pDC->SelectObject(oldFont);   // !!!
 return hbr;
}

With this line this function does nothing.

IDC_MYSTATIC is just a sample, replace it with your control ID.
0
 
AlexFMCommented:
See CWnd::OnCtlColor. You can select the font you created in device context passed to this function:

HBRUSH CMyDlg::OnCtlColor(CDC* pDC, CWnd* pWnd, UINT nCtlColor)
{
   // Call the base class implementation first! Otherwise, it may
   // undo what we're trying to accomplish here.
   HBRUSH hbr = CDialog::OnCtlColor(pDC, pWnd, nCtlColor);

   // Are we painting the IDC_MYSTATIC control? We can use
   // CWnd::GetDlgCtrlID() to perform the most efficient test.
   if (pWnd->GetDlgCtrlID() == IDC_MYSTATIC)
   {
        pDC->SelectObject(&m_BoldFont);
   }

   return hbr;
}
0
 
JensBAuthor Commented:
ok, i have it like this now but it still doesn't work:

HBRUSH CDPersgegevens::OnCtlColor(CDC* pDC, CWnd* pWnd, UINT nCtlColor)
{
  HBRUSH hbr = CDialog::OnCtlColor(pDC, pWnd, nCtlColor);

  LOGFONT logFont;
  logFont.lfHeight = 8;
  logFont.lfWidth = 0;
  logFont.lfEscapement = 0;
  logFont.lfOrientation = 0;
  logFont.lfWeight = FW_BOLD;
  logFont.lfItalic = 0;
  logFont.lfUnderline = 0;
  logFont.lfStrikeOut = 0;
  logFont.lfCharSet = ANSI_CHARSET;
  logFont.lfOutPrecision = OUT_DEFAULT_PRECIS;
  logFont.lfClipPrecision = CLIP_DEFAULT_PRECIS;
  logFont.lfQuality = PROOF_QUALITY;
  logFont.lfPitchAndFamily = VARIABLE_PITCH | FF_ROMAN;
  strcpy(logFont.lfFaceName, "Times New Roman");

  CFont font;
  font.CreateFontIndirect(&logFont);
  CFont* oldFont = pDC->SelectObject(&font);

  if (pWnd->GetDlgCtrlID() == CTLCOLOR_STATIC)
  {
   pDC->SelectObject(&font);
  }
  pDC->SelectObject(oldFont);
  return hbr;
}


'IDC_MYSTATIC': compiler gave an error on that, i replaced it with 'CTLCOLOR_STATIC'. i alsoo don't know if it's necessary to restore the old font to the DC.


Greetings from Jens
0
Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

 
JensBAuthor Commented:
sorry about posting 2 times the same but i've seen the CTLCOLOR_STATIC wasn't correct .. needed to be the control id name

is it necessary to store the old font and restore it later?
0
 
AlexFMCommented:
You can create font only once somewhere outside of this function. Or, at least, move font creation code into
if (pWnd->GetDlgCtrlID() == CTLCOLOR_STATIC)
branch.
This function is called for each dialog control and gives you a chance to change default device context settings before control is redrawn.
0
 
JensBAuthor Commented:
yes indeed
thanks alot
it works great now! :)

0
 
JensBAuthor Commented:
HBRUSH CDPersgegevens::OnCtlColor(CDC* pDC, CWnd* pWnd, UINT nCtlColor)
{
  HBRUSH hbr = CDialog::OnCtlColor(pDC, pWnd, nCtlColor);

 LOGFONT logFont;
 logFont.lfHeight = 16;
 logFont.lfWidth = 0;
 logFont.lfEscapement = 0;
 logFont.lfOrientation = 0;
 logFont.lfWeight = FW_BOLD;
 logFont.lfItalic = 0;
 logFont.lfUnderline = 0;
 logFont.lfStrikeOut = 0;
 logFont.lfCharSet = ANSI_CHARSET;
 logFont.lfOutPrecision = OUT_DEFAULT_PRECIS;
 logFont.lfClipPrecision = CLIP_DEFAULT_PRECIS;
 logFont.lfQuality = PROOF_QUALITY;
 logFont.lfPitchAndFamily = VARIABLE_PITCH | FF_ROMAN;
 strcpy(logFont.lfFaceName, "Times New Roman");
 CFont newFont;
 newFont.CreateFontIndirect(&logFont);

 switch(pWnd->GetDlgCtrlID())
 {
  case ST_NR:
    pDC->SelectObject(&newFont);
    break;
  case ST_GESLACHT:
    //and so on
 }
 return hbr;
}


It works fine now, and i create a font in the beginning of the function.

Greetings
jens
0
All Courses

From novice to tech pro — start learning today.