Solved

change button font

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Pressing cancel button with C# UI Automation 3 51
Set Font and Size on Excel Template 2 101
sum28 challenge 31 105
either24  challenge 19 90
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…
Introduction: Dialogs (2) modeless dialog and a worker thread.  Handling data shared between threads.  Recursive functions. Continuing from the tenth article about sudoku.   Last article we worked with a modal dialog to help maintain informat…
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…

912 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

21 Experts available now in Live!

Get 1:1 Help Now