locked
How to determine font to use?

    Question

  • Sorry if this is a really silly question but I am new to all this - how on earth do I determine the font that CMFCButton is using?

    I have some CMFCButton, CEdit and CStatic controls on my window (created programatically in my widow OnCreate handler).

    The font that the CMFCButton uses is totally different to the other two.  It's nice and I want the CEdit and CStatic to look the same - in fact all the CMFCblah controls look nice - it's just the others that have the old plain font.

    Anyway at first I thought I could try out setting the font using whatever the CMFCButton is using:

    CFont *bf = my_button->GetFont();

    Then derive a copy in the usual way (get logfont then create a new cfont and createindirect...)

    However the CMFCButton GetFont() method returns NULL- even though I call it after invoking its Create() method - so it should be established proper right???

    In any case I should not need to clone the font from a button - I should use the same method that the button itself uses to determine its font.

    So - how do I determine the font in the same way that CMFCButton does?

    Cheers guys

    Gareth

    Saturday, March 27, 2010 7:37 PM

All replies

  • GetFont returns NULL if control is using the system font.
    Nikita Leontiev
    Sunday, March 28, 2010 12:37 PM
  • I think you could use GetWindowDC() then call GetCurrentFont() to determine the font assigned to a window.  Worth a try anyway.

    http://msdn.microsoft.com/en-us/library/242xhdxx.aspx

    Tom

    • Proposed as answer by Yi Feng, Li Thursday, April 01, 2010 2:34 AM
    Monday, March 29, 2010 11:16 PM
  • Hi Gareth

    Your question is insteresting. I have tried your way to "getFont" from a CButton object, it returns a "NON-NULL" CFont. The font name I get from my case is "MS Shell Dlg 2", following is the way I used to get it. In the sample code I get the font (font1) from Button1, then I use createPointFont to create another font, font2, and set it to Button2.

    LOGFONT lFont;  //structure defines the attributes of a font

     //get font form Button1
     CFont *font1 = GetDlgItem(IDC_BUTTON1)->GetFont();
     
     //get name of font1
     font1->GetLogFont(&lFont);

     //Create font2 using the font1's name
     CFont *font2 = new CFont();
     CString fName =CString(lFont.lfFaceName);
     VERIFY(font2->CreatePointFont(120, fName));// set size =120
     
     //set to Button2
     GetDlgItem(IDC_BUTTON2)->SetFont(font2);

    Acctually, I think you can direct set font1 to Button2, if you just want clone the font.

    If I misunderstand your questions, please let me know. 

    Cheers

    Cherubim

    Tuesday, March 30, 2010 7:13 AM
  • Hi Cherubim,

    Gareth is using a CMFCButton which may be slightly different.  Some of the new controls don't behave like the base classes exactly because of the theming implementation.

    Tom

    Tuesday, March 30, 2010 6:05 PM
  • Hi Cherubim,

    Gareth is using a CMFCButton which may be slightly different.  Some of the new controls don't behave like the base classes exactly because of the theming implementation.

    Tom

    Tuesday, March 30, 2010 6:05 PM
  • Hi All

    Good to know about CMFCButton. Tom is correct. I have tried to get font by through Button's DC, it works.

    CDC *dc =Button4.GetDC();
    CFont *font1 = dc->GetCurrentFont();

    Here Button4 is a CMFCButton Object.

    Thank you, Tom

    Cherubim

    Wednesday, March 31, 2010 2:49 AM
  • Hi.

    In the end I subclassed the CMFCButton (amongst other controls) and passed the font type I needed from an application global.

    But is there a way to get the font from the theme?  I looked through the api's for theming but don't see anything obvious.

    Tuesday, April 27, 2010 12:55 PM