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
Solved

How to change font for all static text?

Posted on 2010-09-01
8
418 Views
Last Modified: 2012-05-10
Hello,
I would like to change the font for all my text (static-text).
In OnInitDialog I did this:

       CString strSchriftart1="Microsoft Sans Serif";
   
       CFont *fontPtr1 = new CFont();
      fontPtr1->CreateFont(
       12,
        0,
        0,
        0,
       FW_NORMAL,
        0,
        0,
        0,
       DEFAULT_CHARSET,
       OUT_CHARACTER_PRECIS,
       CLIP_CHARACTER_PRECIS,
       DEFAULT_QUALITY,
       DEFAULT_PITCH|FF_DONTCARE,
       strSchriftart1);
      

       CStatic *pStatic1 =(CStatic*) GetDlgItem(IDC_STATIC);
       pStatic1->SetFont(fontPtr1);


But this will only change one of my static text (ID=IDC_STATIC).
How can I do this.
Please help
500 points with a solution.
Thank you.
Best regards,
Thomas
0
Comment
Question by:tsp2002
  • 4
  • 2
  • 2
8 Comments
 
LVL 31

Accepted Solution

by:
Zoppo earned 500 total points
ID: 33576092
Hi tsp2002,

attached you can find a function which does this - to use it just call
> SetAllStaticsFont( this, fontPtr1 );
instead of your code
> CStatic *pStatic1 =(CStatic*) GetDlgItem(IDC_STATIC);
> pStatic1->SetFont(fontPtr1);

Hope that helps,

ZOPPO
void SetAllStaticsFont( CDialog* pDlg, CFont* pFont )
{
	if ( NULL == pDlg || NULL == pFont )
	{
		ASSERT( 0 );

		return;
	}

	CWnd* pChild = pDlg->GetWindow( GW_CHILD );
	const int nMaxCount = 32;
	TCHAR pszWndClassName[ nMaxCount ];

	while ( NULL != pChild )
	{
		if ( ::GetClassName( pChild->m_hWnd, pszWndClassName, nMaxCount ) )
		{
			if ( _tcsicmp( pszWndClassName, _T( "STATIC" ) ) == 0 )
			{
				pChild->SetFont( pFont );
			}
		}

		pChild = pChild->GetWindow( GW_HWNDNEXT );
	}
}

Open in new window

0
 
LVL 3

Expert Comment

by:WiB
ID: 33576149
uint statics[] = { IDC_STATIC, IDC_STATIC1, ... };
uint sz = sizeof(statics)/sizeof(uint);

CStatic *pStatic = NULL;
for (uint i=0; i<sz; i++)
{
    pStatic =(CStatic*) GetDlgItem(statics[i]);
    pStatic->SetFont(fontPtr1);
    pStatic->UpdateFont();
}
0
 
LVL 31

Expert Comment

by:Zoppo
ID: 33576174
@WiB: This will only work if the IDs of all statics in the dialog are manually modifies since the resource editor automatically sets the IDs of all static-controls to 'IDC_STATIC' ...
0
Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 

Author Closing Comment

by:tsp2002
ID: 33576191
Hello Zoppo,
great...that works just fine.
Thank you very much for your fast and great help.
Best regards from
Thomas



0
 
LVL 31

Expert Comment

by:Zoppo
ID: 33576203
You're welcome, I'm glad I could help ...

have a nice day,

best regards,

ZOPPO
0
 

Author Comment

by:tsp2002
ID: 33576214
for you too, best regards Thomas
0
 
LVL 3

Expert Comment

by:WiB
ID: 33576222
Zoppo, OK, didn't know it was an issue
0
 
LVL 31

Expert Comment

by:Zoppo
ID: 33576251
no problem, WiB, in general your suggestion was ok despite exactly the fact that static controls are handled differentyl by the resource editor than any other controls where the IDs have to be unique. But, even in the case this should be done i.e. for edit controls it's better to have a dynamic functionality like mine to avoid the function has to be maintained with any resource change.

A nice day to you too ...
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
How can i compile this github project?? 2 90
gdb doesn't stop on breakpoint 2 93
How do i run a c++ file? 15 46
Are the two lines the same? If not what is the difference? 2 22
Introduction This article is a continuation of the C/C++ Visual Studio Express debugger series. Part 1 provided a quick start guide in using the debugger. Part 2 focused on additional topics in breakpoints. As your assignments become a little more …
Many modern programming languages support the concept of a property -- a class member that combines characteristics of both a data member and a method.  These are sometimes called "smart fields" because you can add logic that is applied automaticall…
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.

840 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