Solved

change button font

Posted on 2001-08-24
4
1,106 Views
Last Modified: 2013-12-03
i have created a button dynamically in MFC. i would like to set MFC default font for this button.

How can i do this?
0
Comment
Question by:pratimag
4 Comments
 
LVL 4

Expert Comment

by:albay
ID: 6423802
hi pratimag. try this...

CFont font;
     font.CreatePointFont(120,_T("Times New Roman"));
     CButton* button = (CButton*) GetDlgItem(IDC_BUTTON2);
     //IDC_BUTTON2 = your button ID
     CDC* dc = button->GetDC();
     CFont* pOldFont = dc->SelectObject(&font);
     CString str; button->GetWindowText(str);
     if(!str.IsEmpty()) {
          CRect rect; button->GetClientRect(&rect);
          button->SetWindowText(_T(""));
          dc->SetBkMode(TRANSPARENT);
          dc->DrawText(str,&rect,DT_CENTER);
     }
     dc->SelectObject(pOldFont);
     button->ReleaseDC(dc);

hope this helps,
best wishes.
albay
0
 
LVL 9

Expert Comment

by:Pacman
ID: 6427674
> i would like to set MFC default font for this button.

what do you mean with "default font".
The button has already a default font after creation.
0
 
LVL 4

Accepted Solution

by:
jtwine100697 earned 10 total points
ID: 6429223
If you want to change the font that the button uses when it draws itself, create a CFont object similar to how "albay" does, but use CWnd::SetFont(...) to set the font for the button itself.

For example:

//
//  Assume You Have "m_btButton" Bound To The Button Control
//  By Using ClassWizard And Have A CFont Object ("m_fMyFont")
//Created And Initialized Already...
//
m_btButton.SetFont( &m_fMyFont );

That will cause the button to use the new font whenever it draws its text (BTW: this works for other controls, like the Edit, Static, and ListView as well).

IMHO, Drawing the button text, when the button can already do that itself is using a shotgun to kill a fly.

-=- James.
0
 
LVL 23

Expert Comment

by:Roshan Davis
ID: 9528428
No comment has been added lately, so it's time to clean up this TA.
I will leave a recommendation in the Cleanup topic area that this question is:

Answered by: jtwine

Please leave any comments here within the next seven days.

PLEASE DO NOT ACCEPT THIS COMMENT AS AN ANSWER!

Roshan Davis
EE Cleanup Volunteer
0

Featured Post

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

Suggested Solutions

Here is how to use MFC's automatic Radio Button handling in your dialog boxes and forms.  Beginner programmers usually start with a OnClick handler for each radio button and that's just not the right way to go.  MFC has a very cool system for handli…
Introduction: Dialogs (1) modal - maintaining the database. Continuing from the ninth article about sudoku.   You might have heard of modal and modeless dialogs.  Here with this Sudoku application will we use one of each type: a modal dialog …
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.
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…

758 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

18 Experts available now in Live!

Get 1:1 Help Now