Solved

change button font

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

Industry Leaders: 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

Introduction: Ownerdraw of the grid button.  A singleton class implentation and usage. Continuing from the fifth article about sudoku.   Open the project in visual studio. Go to the class view – CGridButton should be visible as a class.  R…
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…

726 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