Solved

How to change font size in CComboBox?

Posted on 1998-10-12
1
1,591 Views
Last Modified: 2013-12-03
How to change font size in CComboBox and CListBox?
0
Comment
Question by:Nightmare090197
1 Comment
 
LVL 8

Accepted Solution

by:
Answers2000 earned 50 total points
ID: 1323220
Send a WM_SETFONT message to the control.  this contains an HFONT of the font you want to use and a flag indicating if you want to redraw the control.

Steps

1. Associate a CComboBox or CListBox class with the control you want (e.g. use class wiz for dialog apps) - e.g. m_combo for CComboBox, or m_list for CListBox.

2. Use SendMessage to send the message.  If the control is on a dialog, and font doesn't change, then OnInitDialog is a good place to do this

m_combo.SendMessage( WM_SETFONT, (WPARAM)hFont, MAKELPARAM(TRUE,0) ) ; // FALSE instead of TRUE if you don't want immediate redraw

m_list.SendMessage( WM_SETFONT, (WPARAM)hFont, MAKELPARAM(TRUE,0) ) ; // FALSE instead of TRUE if you don't want immediate redraw

3. You can get the hFont a couple of ways

i. Use a stock font, in which case you do not need to clear up
hFont = (HFONT)::GetStockObject( SYSTEM_FIXED_FONT ) ;

or

ii. Create your own font.  In this case you need to clear up, set the font back before your dialog exits, and make sure any fonts you create are also deleted.

You create fonts using CreateFont or CreateFontIndirect.
You delete fonts using DeleteFont

Example:
    (a) Add member variable HFONT m_hOldFont to your dialog class
    (b) Add member variable HFONT m_hFont to your dialog class
    (c) In OnInitDialog you can create the font, based on a new LOGFONT that you supply, or by getting the current values

   m_hOldFont = (HFONT)m_list.SendMessage( WM_GETFONT, 0, 0 ) ;
   LOGFONT lf ;
   ::GetObject( (HGDIOBJ)m_hOldFont, sizeof lf, &lf ) ;
   lf.lfHeight = 32 ; // some new size
   m_hFont = ::CreateFontIndirect( &lf ) ;
   m_list.SendMessage( WM_SETFONT, (WPARAM)m_hFont, MAKELPARAM(TRUE,0) ) ;
   m_combo.SendMessage( WM_SETFONT, (WPARAM)m_hFont, MAKELPARAM(TRUE,0) ) ;

   (d) When destroy the dialog (e.g. OnOK) set font back, and delete the font you created
   m_list.SendMessage( WM_SETFONT, (WPARAM)m_hOldFont, MAKELPARAM(TRUE,0) ) ;
   m_combo.SendMessage( WM_SETFONT, (WPARAM)m_hOldFont, MAKELPARAM(TRUE,0) ) ;
   ::DeleteObject( m_hFont ) ;

   

0

Featured Post

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

Here is how to use MFC's automatic Radio Button handling in your dialog boxes and forms.  Beginner programmers usually start with a OnClick handler for each radio button and that's just not the right way to go.  MFC has a very cool system for handli…
Introduction: Load and Save to file, Document-View interaction inside the SDI. Continuing from the second article about sudoku.   Open the project in visual studio. From the class view select CSudokuDoc and double click to open the header …
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…

758 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

19 Experts available now in Live!

Get 1:1 Help Now