Solved

Setting Fonts in MFC Dialog application

Posted on 2003-11-21
9
2,360 Views
Last Modified: 2013-12-03
In an MFC diaolog application, I know the <filename>.rc file contains the font type/size definition, which sets the font and size for every control and static text area on the dialog.

Is there a way to set the font when and where it's desired at runtime?

Thanks!
0
Comment
Question by:cox8355
9 Comments
 
LVL 19

Accepted Solution

by:
Dexstar earned 100 total points
ID: 9798933
@cox8355:

> Is there a way to set the font when and where it's desired at runtime?

Well, you can always use CWnd's SetFont member, but that won't necessarily give you the effect that you want.  In a dialog resource, the sizes of the controls are all relative to the size of the font.  So if you change the font for the dialog, then all the controls get resized automatically when the window is created.

However, once the dialog is created, those values are set, and it is tricky to resize them properly (unless you don't change the size of the font, just the face).

So, the answer is Yes you can do it, but it is a lot of work unless you keep the size of the font the same.

Hope That Helps,
Dex*
0
 
LVL 48

Expert Comment

by:AlexFM
ID: 9798973
0
 
LVL 19

Expert Comment

by:Dexstar
ID: 9799020
@AlexFM:

Yeah, that kind of splits the difference.  It edits the dialog box resource JUST BEFORE it is created.  I thought the Asker wanted to change it after it was already displayed, but I guess it wasn't specified...  :)

Dex*
0
 
LVL 4

Expert Comment

by:YuriPutivsky
ID: 9800621
void CYourDialogClass::OnClickSomeButton()
{

CFont m_LocalFont;
// fill font structure here
...
// change dialog font
SetFont(&m_LocalFont, FALSE);
// change font of all controls on dialog
SendMessageToDescendants(WM_SETFONT, (WPARAM)HFONT(m_LocalFont), FALSE);

}
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 19

Expert Comment

by:Dexstar
ID: 9800704
@Yuri:  You can do that, but if you do, all the controls in the dialog will no longer be of the correct size.

Dex*
0
 
LVL 4

Expert Comment

by:YuriPutivsky
ID: 9800712
May you need to change only face of font not the size :-))
0
 
LVL 3

Expert Comment

by:RJSoft
ID: 9804193
You can subclass your static text boxes, use CreateFont or CreateFontIndirect and then use DrawText to display the fonts you created.

That way you can do what you want in any color etc...

Only problem is some of the text in the static boxes you wish to show need to be centered while others you want left aligned.

But all you have to do is send some flag to the drawing routine of your subclassed static. Then you can again do what you want.


Ex.

First establish subclass.

class MyStatic : public CStatic
{
...
void MyDrawText( CString Data, int How);
};


Next in rc view establish unique id's for each static to subclass.
BTW, use sunken view properites in rc view of static (so you can easily see it)

Typical dialog which will use the subclass

#include "Mystatic.h"
class MyDialog : public CDialog
{
...
MyStatic Box1;
MyStatic Box2; .....

};


Instance of dialog from parent dialog view

void ParentDialog::AFunc()
{

MyDialog Md;

Md.Box1.MyDrawText("test data to show",1); //1==center
Md.Box2.MyDrawText("test data again",2);//2==left ?? whatever.

Md.DoModal();

}

void MyStatic::MyDrawText(CString Text, int how)
{
//I am use to doing things API way. So I forget but basics is same.
CreateFont
GetDC
Select font into dc

Get the rectangle area from the client area of this static
::GetClientRect...rr

if(how==1) DrawText(..&rr...DT_VCENTER | DT_SINGLELINE | DT_CENTER);
if(how==2) DrawText(..&rr... instead use default DT_LEFT);

clean up mem
...

}//endfunc

If you need more info I can supply

RJ



0
 
LVL 9

Expert Comment

by:tinchos
ID: 10249345
No comment has been added lately, so it's time to clean up this TA.
I will leave the following recommendation for this question in the Cleanup topic area:

Accept: Dexstar {http:#9798933}

Please leave any comments here within the next seven days.
PLEASE DO NOT ACCEPT THIS COMMENT AS AN ANSWER!

Tinchos
EE Cleanup Volunteer
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

When writing generic code, using template meta-programming techniques, it is sometimes useful to know if a type is convertible to another type. A good example of when this might be is if you are writing diagnostic instrumentation for code to generat…
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 be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…

930 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now