?
Solved

Put text on label field into bold

Posted on 2003-03-10
10
Medium Priority
?
284 Views
Last Modified: 2013-11-20
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
0
Comment
Question by:JensB
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 3
10 Comments
 
LVL 48

Expert Comment

by:AlexFM
ID: 8103008
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
 

Author Comment

by:JensB
ID: 8103219
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
 

Author Comment

by:JensB
ID: 8103275
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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 48

Accepted Solution

by:
AlexFM earned 200 total points
ID: 8103320
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
 
LVL 48

Expert Comment

by:AlexFM
ID: 8103389
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
 

Author Comment

by:JensB
ID: 8103432
yes indeed
thanks alot
it works great now! :)

0
 

Author Comment

by:JensB
ID: 8103468
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

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction: Dynamic window placements and drawing on a form, simple usage of windows registry as a storage place for information. Continuing from the first article about sudoku.  There we have designed the application and put a lot of user int…
Introduction: Dialogs (2) modeless dialog and a worker thread.  Handling data shared between threads.  Recursive functions. Continuing from the tenth article about sudoku.   Last article we worked with a modal dialog to help maintain informat…
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.
Visualize your data even better in Access queries. Given a date and a value, this lesson shows how to compare that value with the previous value, calculate the difference, and display a circle if the value is the same, an up triangle if it increased…
Suggested Courses

719 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question