Solved

How to change font size in CComboBox?

Posted on 1998-10-12
1
1,776 Views
Last Modified: 2013-12-03
How to change font size in CComboBox and CListBox?
0
Comment
Question by:Nightmare090197
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

In this article, I'll describe -- and show pictures of -- some of the significant additions that have been made available to programmers in the MFC Feature Pack for Visual C++ 2008.  These same feature are in the MFC libraries that come with Visual …
In this post we will learn different types of Android Layout and some basics of an Android App.
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…
If you’ve ever visited a web page and noticed a cool font that you really liked the look of, but couldn’t figure out which font it was so that you could use it for your own work, then this video is for you! In this Micro Tutorial, you'll learn yo…

635 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