Solved

Setting Fonts in MFC Dialog application

Posted on 2003-11-21
9
2,314 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
Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
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

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

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…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.

708 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

12 Experts available now in Live!

Get 1:1 Help Now