Link to home
Start Free TrialLog in
Avatar of cox8355
cox8355

asked on

Setting Fonts in MFC Dialog application

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!
ASKER CERTIFIED SOLUTION
Avatar of Dexstar
Dexstar

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of AlexFM
AlexFM

@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*
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);

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

Dex*
May you need to change only face of font not the size :-))
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



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