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:

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.

Recommended Answers

All 3 Replies

1. Add a Control Variable of type CStatic to the Static Control( lets say m_Caption )
2. Add a CFont Private Variable to the Main Dialog ( say m_Font )
3. At the OnInitDialog Function do this

m_Font.Detach();
	m_Font.CreatePointFont(360, "Arial", (GetDlgItem(IDC_PROGRESS_INDICATOR) )->GetDC());
	m_Caption.SetFont(&m_Font );

WolfPack,

Thanks, it works the way I want it to. Tried to add to your reputation, but it wants me to spread it around some. Not sure what that means???

WolfPack,

Thanks, it works the way I want it to. Tried to add to your reputation, but it wants me to spread it around some. Not sure what that means???

Dont worry man. It is the thought that counts. :D

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.