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
Solved

change button font

Posted on 2001-08-24
4
1,113 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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
mapAB Challlenge 35 155
Is there a simple front-end menu system. 9 102
How to install a font on WIN2003SBS/IIS 6 & test 17 31
I could not set window to top 4 20
Introduction: Displaying information on the statusbar.   Continuing from the third article about sudoku.   Open the project in visual studio. Status bar – let’s display the timestamp there.  We need to get the timestamp from the document s…
Have you tried to learn about Unicode, UTF-8, and multibyte text encoding and all the articles are just too "academic" or too technical? This article aims to make the whole topic easy for just about anyone to understand.
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…

790 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