Setting a font on a CButton

mhebert
mhebert used Ask the Experts™
on
I have a basic CPropertySheet that isn't using a resource in my MFC project.

By calculation, I'm placing, in the OnInitDialog() function, a new CButton object near the 3 default button(OK, Cancel & Apply);

Now, I want to change the font of this CButton. How can I do this? (note: the CButton is not in resource files)

Mario
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®

Commented:
Hi use
pButton->SetFont(&m_Font);
m_Font is the Dialog class member (must be destruted in the class destructor)

Author

Commented:
What is m_Font ?
How do you initialize it?

  CFont m_Font;

  InOnInitDialog

LOGFONT lf;
memset(&lf, 0, sizeof(LOGFONT)); // zero out structure
lf.lfHeight = 12;  // request a 12-pixel-height font
strcpy(lf.lfFaceName, "Arial"); // request a face name "Arial"
VERIFY(m_Font.CreateFontIndirect(&lf));  // create the font

  pButton->SetFont ( &m_Font );

 
  Should Work for You

 

Exploring ASP.NET Core: Fundamentals

Learn to build web apps and services, IoT apps, and mobile backends by covering the fundamentals of ASP.NET Core and  exploring the core foundations for app libraries.

I've done this and I found that you need to control the lifetime of the font object. So I made it a member variable of the property sheet class. You can also use GetTabControl to get the same font as the other buttons.

   LOGFONT logFont;
   CFont * pFont = this->GetTabControl()->GetFont();
   pFont->GetLogFont(&logFont);

   this->m_buttonFont.CreateFontIndirect(&logFont);
   pButton->SetFont(&this->m_buttonFont);

Commented:
code3 abive sets identiacal font for buttons and all other controls in the dialog
to change font modify logfont members:

 LOGFONT logFont;
 CFont * pFont = GetTabControl()->GetFont();
 pFont->GetLogFont(&logFont);
// sets bold italic font.
logFont.lfBold = true;
logFont.lfItalic = true;
 m_Font.CreateFontIndirect(&logFont);
 pButton->SetFont(&m_Font);

Author

Commented:
My problem was that my object font was not in the scope of the CPropertySheet class..

Thanks !

Mario

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial