Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Setting Fonts in MFC Dialog application

Posted on 2003-11-21
9
Medium Priority
?
2,635 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
[X]
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
9 Comments
 
LVL 19

Accepted Solution

by:
Dexstar earned 400 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
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
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
 
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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

C++ Properties One feature missing from standard C++ that you will find in many other Object Oriented Programming languages is something called a Property (http://www.experts-exchange.com/Programming/Languages/CPP/A_3912-Object-Properties-in-C.ht…
This article shows you how to optimize memory allocations in C++ using placement new. Applicable especially to usecases dealing with creation of large number of objects. A brief on problem: Lets take example problem for simplicity: - I have a G…
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…
If you’ve ever visited a web page and noticed a cool font that you really liked the look of, but couldn’t figure out which font it was so that you could use it for your own work, then this video is for you! In this Micro Tutorial, you'll learn yo…

670 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