CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Page 1 of 2 12 LastLast
Results 1 to 15 of 18
  1. #1
    Join Date
    Dec 2002
    Location
    God's own country
    Posts
    201

    Question Font for CButton

    hai,

    How can we change the font of caption on a button?
    I tried with both using CreateFontIndirect and Logfont.

    parts of code is given below:-

    VERIFY(font.CreateFont(
    12, // nHeight
    0, // nWidth
    0, // nEscapement
    0, // nOrientation
    FW_NORMAL, // nWeight
    FALSE, // bItalic
    FALSE, // bUnderline
    0, // cStrikeOut
    ANSI_CHARSET, // nCharSet
    OUT_DEFAULT_PRECIS, // nOutPrecision
    CLIP_DEFAULT_PRECIS, // nClipPrecision
    DEFAULT_QUALITY, // nQuality
    DEFAULT_PITCH | FF_SWISS, // nPitchAndFamily
    "Ponni")); // lpszFacename

    GetDlgItem(IDC_BUTTON1)->SetFont(&font);

    thanks,

    saj

  2. #2
    Join Date
    Feb 2002
    Posts
    5,757
    One solution is LOGFONT structure.

    Code:
    LOGFONT lf;
    memset(&font, 0, sizeof(LOGFONT));
    font.lfHeight = -MulDiv(12, GetDeviceCaps(dc, LOGPIXELSY), 72);
    _tcscpy(font.lfFaceName, TEXT("Ponni"));
    
    CFont font;
    font.CreateFontIndirect(&lf);
    GetDlgItem(IDC_BUTTON1)->SetFont(font);
    Kuphryn

  3. #3
    Join Date
    Dec 2002
    Location
    God's own country
    Posts
    201

    Question Not working

    Thank you kuphryn for your suggestion, but it is not working for me. I did try with the code you gave, but even then, the font is not changing.
    Any idea why this?

    saj

  4. #4
    Join Date
    Feb 2002
    Posts
    5,757
    Try Win32 solution.

    ::SendMessage(button.m_hWnd, WM_SETFONT, reinterpret_cast<WPARAM>(lf), MAKELPARAM(false, 0));

    Kuphryn

  5. #5
    Join Date
    May 2000
    Location
    KY, USA
    Posts
    18,652
    Code:
    CFont Font;
    
    Font.CreatePointFont(120, _T("Courier"));        // creates a 12-point-Courier-font
    
    m_Button.SetFont(&Font);                        // with a member variable associated
    GetDlgItem(IDC_BUTTON)->SetFont(&Font);         // without a member variable associated
    The 'CFont' variable needs to be a member variable of the dialog containing the button...

  6. #6
    Join Date
    Dec 2002
    Location
    God's own country
    Posts
    201

    Its working.....

    Thank you very much, its working....
    btw, why the CFont variable needs to be a member variable of dialog class?

    Regards
    saj

  7. #7
    Join Date
    Feb 2001
    Posts
    15
    Cause if you declared your font inside a function, after the function returned, the font is no longer exist anymore.

  8. #8
    Join Date
    Dec 2002
    Posts
    214

    Re: Its working.....

    if it' s a local varible, the GDI object will be deleted by the destructor of CFont automatically when step out the scope.


    Originally posted by saj
    Thank you very much, its working....
    btw, why the CFont variable needs to be a member variable of dialog class?

    Regards
    saj

  9. #9

    Exclamation Re: Font for CButton

    Andreas,
    your solution is not working for windows XP when the manifest file is compiled with the application for styles.
    I cannot get the XP style button to change font,
    please help!

  10. #10
    Join Date
    Aug 2005
    Posts
    104

    Re: Font for CButton

    I think fonts are part of the theme, so it will use those instead of the one you pass.

    To remove a theme from a window, call
    SetWindowTheme(hwnd, L"", L"");

    where hwnd is the hwnd of your button.

    See if that works?

  11. #11
    Join Date
    May 1999
    Location
    ALABAMA, USA
    Posts
    9,917

    Re: Font for CButton

    Quote Originally Posted by Brooks Younce
    Andreas,
    your solution is not working for windows XP when the manifest file is compiled with the application for styles.
    It works perfectly in XP.
    Could you post a code snippet that assigns font to a button? Including font creation/retrieval.
    There are only 10 types of people in the world:
    Those who understand binary and those who do not.

  12. #12

    Exclamation Re: Font for CButton

    sure, here is my code...
    it does not work in onInitDlg....
    and it does not work in any other functions called at any time.....
    this code will set the font fine when the Manifest file is not included at compile time. But this is not acceptable because I want to support XP syles,
    thanks in advance for any replies you have...

    Code:
     
    	//create the button font
    	//CFont bFont; //this is a member variable in the header class
    	LOGFONT lf;
    	memset(&lf, 0, sizeof(LOGFONT));// zero out structure
    	strcpy(lf.lfFaceName, "Verdana");
    	lf.lfHeight = 30;
    	lf.lfWeight = FW_BOLD;	// nWeight
    	bFont.CreateFontIndirect(&lf);
    	//bFont.CreatePointFont(140,"Verdana"); //this also does not work
    	GetDlgItem(IDC_BUTTON1)->SetFont(&bFont);
    	//end button font

  13. #13
    Join Date
    May 1999
    Location
    ALABAMA, USA
    Posts
    9,917

    Re: Font for CButton

    It works. Picture below shows font using your code (bottom button) and using code below (that is similar) for top button.
    Code:
    	LOGFONT lf;
    	::GetObject((HFONT)GetStockObject(DEFAULT_GUI_FONT), sizeof(lf), &lf);
    	
    	CClientDC dc(this);
    	strcpy(lf.lfFaceName, "times new roman");
    	lf.lfHeight = -MulDiv(13, GetDeviceCaps(dc.m_hDC, LOGPIXELSY), 72);;
    	
    	m_Font.CreateFontIndirect(&lf);
    	m_Button.SetFont(&m_Font);
    Therefore either your code snippet is not exactly the same as code, or there is something else going on (gremlins).
    Attached Images Attached Images  
    There are only 10 types of people in the world:
    Those who understand binary and those who do not.

  14. #14

    Exclamation Re: Font for CButton

    I copy and pasted that code directly out of my program,
    I think Visual Studio 2003 sucks! could that be the problem?
    I know everytime I use a Time Control in a program, when I close the compiler, and reopen it, suddenly the stupied compiler has converted my time control into a date control, Microsoft sucks.

    What compiler are you using, which Visual Studio version?
    Please let me know, thanks!

  15. #15

    Angry Re: Font for CButton

    This is a problem, my code works perfect for every control Except a CButton! I have VS 2002 and VS2003, both compilers will not set the font for a CButton, no matter what I do,
    Anyone have any ideas?

Page 1 of 2 12 LastLast

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured