Solved

Setting Fonts in MFC Dialog application

Posted on 2003-11-21
9
2,392 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
Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

 
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

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
IdTCPClient1->Disconnect(); not working 3 66
erros on link  checking 2 67
Which Linux flavors will this run on? 6 87
DCT of  2D array using fftw in c++ 9 38
Introduction This article is the first in a series of articles about the C/C++ Visual Studio Express debugger.  It provides a quick start guide in using the debugger. Part 2 focuses on additional topics in breakpoints.  Lastly, Part 3 focuses on th…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
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…

785 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