Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

CFont problem

Status
Not open for further replies.

HyperEngineer

Programmer
May 8, 2002
190
US
I would like to change the font in a static text box. But I want it to be that size until I change it again. I put the following code in the OnInitDialog() function:

Code:
  CWnd* pWndControl = GetDlgItem(IDC_PROGRESS_INDICATOR);
  pWndControl->MoveWindow(((MyRect.right - MyRect.left)/2) - 100/2,
                            MyRect.top + 100/2,
                            100,
                            100,
                            TRUE);

  CFont* pFont;
  pFont->CreatePointFont(360, "Arial", pWndControl->GetWindowDC());
  pWndControl->SetFont(pFont);

This will build fine. However, I get the following error:


The instruction at "0x5f440a1d" referenced memory at 0xccccccd0". The memory could not be "read".

Click on OK to terminate the program
Click on CANCEL to debug the program


I can put the same three lines of code elsewhere with differing results.

If I put it in a BN_CLICDED() then it will only change the font one time. But I get no errors or warnings at build time and no errors at run time. But, the next time I write to the static control it reverts back to the smaller font.

If I put it in the OnTimer() at build time I get this warning:

warning C4700: local variable 'MyFont' used without having been initialized

And I get the same error as before at run time.

I thought putting it in the OnInitDialog() would set the font for all time or until it was explicitly changed.




HyperEngineer
If it ain't broke, it probably needs improvement.
 
I got the answer to this in another forum at another website.

This is the solution FYI.

Add a control variable CStatic (m_ProgressControl) to the control, add a member variable m_pStaticFont to the dialog box.

Then in the OnInitDialog():
Code:
  m_pStaticFont.CreatePointFont(360, "Arial", pWndControl->GetDC);
  pWndControl->SetFont(&m_pStaticFont);


HyperEngineer
If it ain't broke, it probably needs improvement.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top