Link to home
Start Free TrialLog in
Avatar of JensB
JensB

asked on

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
Avatar of AlexFM
AlexFM

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;
}
Avatar of JensB

ASKER

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
Avatar of JensB

ASKER

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?
ASKER CERTIFIED SOLUTION
Avatar of AlexFM
AlexFM

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
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.
Avatar of JensB

ASKER

yes indeed
thanks alot
it works great now! :)

Avatar of JensB

ASKER

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