DonationCoder.com forum
 
Read-only archive. This is a static copy of the DonationCoder.com forum. Reading and searching work as before; logging in, posting and private messages do not.
Archive of 2026-10-21 · About this archive · Main site

Author Topic: Static Text Control Font Size  (Read 27,935 times)

Davidtheo

  • Participant
  • Joined in 2008
  • *
  • Posts: 119
Static Text Control Font Size
« on: March 03, 2009, 08:28 AM »
Hi,

Can some one please tell me a easy way to make these to static text label controls in MFC, MS Visual Studio have a larger text font.

I would like.

IDC_Words to have a font size of 70.
IDC_Answer to have a font size of 50.

I have tried GetDlgItem(IDC_Word)->SetWindowTextW(); and IDC_Answer::FONTSIZE(50); but both do not work.

Thanks

Eóin

  • O'Callaghan
  • Charter Member
  • Joined in 2006
  • ***
  • Posts: 1,401
Re: Static Text Control Font Size
« Reply #1 on: March 03, 2009, 08:53 AM »
I believe you would have to create your own font object and load it into the control. All I can offer is WTL based code, but the MFC equivilent may not differ all that much.

Code: (cpp)
CFont font_fixedw_;

...

if (CFont font = GetFont())
{
	CLogFont lf;		

	if (font.GetLogFont(&lf))
	{
		lf.lfWidth = 6;
		lf.lfPitchAndFamily = FIXED_PITCH;
		lf.lfFaceName[0] = 0;
		font_fixedw_.CreateFontIndirect(&lf);
	}
}

CWindow wndControl = GetDlgItem(IDC_DESCRIPTION);
wndControl.SetFont(font_fixedw_);

Hope it is of some help.
Interviewer: Is there anything you don't like?
Bjarne Stroustrup: Marketing hype as a substitute for technical argument. Thoughtless adherence to dogma. Pride in ignorance.

Davidtheo

  • Participant
  • Joined in 2008
  • *
  • Posts: 119
Re: Static Text Control Font Size
« Reply #2 on: March 03, 2009, 09:49 AM »
Hi

If I change your code to this I only get one error, which I am not sure on how to fix.

Code: (cpp)
CFont font_fixedw_;
.
.
.
LOGFONT lf;
lf.lfWidth = 6;
lf.lfPitchAndFamily = FIXED_PITCH;
lf.lfFaceName[0] = 0;
font_fixedw_.CreateFontIndirect(&lf);
	
CWnd *wndControl = GetDlgItem(IDC_Word);
wndControl.SetFont(font_fixedw_); //Error Error C2228: left of '.SetFont' must have class/struct/union	

Eóin

  • O'Callaghan
  • Charter Member
  • Joined in 2006
  • ***
  • Posts: 1,401
Re: Static Text Control Font Size
« Reply #3 on: March 03, 2009, 10:04 AM »
From your code I'd guess the last line should be wndControl->SetFont(font_fixedw_); as wndControl is declared as a CWnd pointer. But as I say, I'd guess :)
Interviewer: Is there anything you don't like?
Bjarne Stroustrup: Marketing hype as a substitute for technical argument. Thoughtless adherence to dogma. Pride in ignorance.

Davidtheo

  • Participant
  • Joined in 2008
  • *
  • Posts: 119
Re: Static Text Control Font Size
« Reply #4 on: March 03, 2009, 12:45 PM »
Hi,

I have tried these two lines in different ways including the ways below.

Code: (cpp)
CWnd *wndControl = GetDlgItem(IDC_Word);
wndControl.SetFont(font_fixedw_);

CWnd wndControl = new CWind;
wndControl = GetDlgItem(IDC_Word);
wndControl.SetFont(font_fixedw_);

CWnd wndControl = GetDlgItem(IDC_Word);
wndControl.SetFont(font_fixedw_);

CWnd *wndControl = GetDlgItem(IDC_Word);
wndControl->SetFont(font_fixedw_);

All of them seem to give me a error. Not sure what to do now.

Eóin

  • O'Callaghan
  • Charter Member
  • Joined in 2006
  • ***
  • Posts: 1,401
Re: Static Text Control Font Size
« Reply #5 on: March 03, 2009, 01:38 PM »
I don't really know with MFC but you probably have to Attach the CWnd class you're creating to the existing Dialog control. Honestly though I'm not sure.
Interviewer: Is there anything you don't like?
Bjarne Stroustrup: Marketing hype as a substitute for technical argument. Thoughtless adherence to dogma. Pride in ignorance.

Davidtheo

  • Participant
  • Joined in 2008
  • *
  • Posts: 119
Re: Static Text Control Font Size
« Reply #6 on: March 03, 2009, 03:03 PM »
Hi,

I got it to build. I did it by adding a control variable called C_word to the Static text control and the removing the following two lines and adding this one.

Code: (cpp)
//CWnd *wndControl  = GetDlgItem(IDC_Word);
//wndControl.SetFont(font_fixedw_,TRUE);

C_Word.SetFont(&font_fixedw_);

However are text does not seem to get bigger.
I will keep playing with it.

Thanks for your Help.

Davidtheo

  • Participant
  • Joined in 2008
  • *
  • Posts: 119
Re: Static Text Control Font Size
« Reply #7 on: March 04, 2009, 05:38 AM »
Hi Again

Just to let you know I did it in three lines of code.

Code: (cpp)
CFont *m_Word = new CFont;
m_Word->CreatePointFont(600, _T("Arial") );
C_Word.SetFont(m_Word); //C_Word is a control variable for the Static text control 

I thought there is a easy way to do it just did not know how, Thanks again for your help.

tez93

  • Participant
  • Joined in 2009
  • *
  • Posts: 1
Re: Static Text Control Font Size
« Reply #8 on: April 02, 2009, 10:47 AM »

I was trying to work out how to do this myself recently and came up with another method as Visual Studio didn't seem to want to let me create a control variable for a static text control within a dialog.

Code: (cpp)

CFont* g_hFont = NULL;

...

case WM_INITDIALOG:
{
	if (g_hFont)
	{
		delete g_hFont;
		g_hFont = NULL;
	}

	g_hFont = new CFont;
	g_hFont->CreatePointFont(140, _T("Verdana"));
	CWnd::FromHandle(GetDlgItem(hDlg, IDC_STATIC_NAME))->SetFont(g_hFont, TRUE);

	return (INT_PTR)TRUE;
}

...

Original address: https://www.donationcoder.com/forum/index.php?topic=17271.0