Cool Font selection combo

This article was contributed by Norm Almond of Paramax Technology Limited

I came accross a fancy font selection box the other day. It was so cool I thought I'd copy the idea.

Its basically an imitation of the font selection box found in MS Word, it has an MRU font list and the other cool feature is that it has a Tooltip window showing the currently selected font name displayed using the selected font. This gives the user a sample of what the font is going to look like without actually making a selection.

The main component is an ownerdraw combo with has strings flags set, the combo isn't sorted because the Most Recently Used font are at  the top of the combo.

The combo is called CFontCombo it is simple to use in dialog boxes, just follow this simple instructions.

  1. Copy "FontCombo.h" and "TipWnd.h" to your project directory.
  2. Add the files to your project.
  3. Copy the "Printer.bmp" to your project "res" directory.
  4. #include "FontCombo.h" and "TipWnd.h" to the header file of your dialog class or to the "stdafx.h"
  5. Using the resource studio Import the printer.bmp and name it IDB_GLYPHS
  6. Using the dialog editor create your combo box and give it the following styles: "DropDown", Fixed, "Has Strings", and Verticle Scroll .
  7. Edit the dialogs class header file replace "CComboBox" for CFontCombo
  8. In OnInitDialog before the return statement Add the following line

    m_yourCombo.SubclassDlgItem(IDC_COMBOID,this);

    m_yourCombo.Initialize();

fontcombo.gif (5338 bytes)

Files and resources needed to be included into you own project:

FontCombo.h & cpp

TipWnd.h & cpp,

IDB_GLYPH bitmap resource

Download Source Code



Comments

  • Bug when scrolling with arrow key

    Posted by Legacy on 02/08/2002 12:00am

    Originally posted by: St�phane Hamel

    If you use the arrow key to scroll in the dropdown list, the cursor jumps every time OnTimer is called.

    Reply
  • How to make it work on a toolbar?

    Posted by Legacy on 01/01/2000 12:00am

    Originally posted by: Rama

    The code given here is used in a combo box on a dialog box.
    I tried to implement the same on a combo box placed on a toolbar for selecting the font (like the Wordpad or MS Word programs). However, somehow it did not succeed. Can anybocy suggest a way out? Thanks in advance.

    Rama
    rama_1000@yahoo.com

    Reply
  • bug: scrolling listbox while tooltip is up

    Posted by Legacy on 02/18/1999 12:00am

    Originally posted by: Sean

    In FontCombo.cpp
    
    

    Change the timer code so that it only repaints the tooltip if the
    selected item is visible in the listbox.

    void CFontCombo::OnTimer(UINT nIDEvent)
    {
    // Is combo open
    if (GetDroppedState( ))
    {
    int nSel = GetCurSel();
    int FirstVis = GetTopIndex();

    // Selected
    if (nSel != -1 && FirstVis <= nSel)
    {
    CRect rc;
    GetDroppedControlRect(rc);
    int itemHeight = GetItemHeight(0);
    int lastVis = FirstVis + ((rc.Height()-itemHeight)/itemHeight);
    if (nSel <= lastVis)
    {
    int nHeight = itemHeight * ((nSel - FirstVis) + 1);

    CPoint pt(rc.right + 5,rc.top + nHeight);

    // Show tip in correct position
    CString str;
    GetLBText(nSel,str);
    m_wndTip.ShowTips(pt,str);
    }
    }
    }
    CComboBox::OnTimer(nIDEvent);
    }

    Reply
  • a couple more mods

    Posted by Legacy on 02/17/1999 12:00am

    Originally posted by: Sean

    From TipWnd.cpp
    
    

    If using the infotip background, why not the foreground too?

    void CTipWnd::OnPaint()
    {
    CPaintDC dc(this); // device context for painting

    dc.SelectObject(&m_font);
    CRect rc;
    GetClientRect(rc);
    dc.SetBkMode(TRANSPARENT); // Add
    dc.SetTextColor(GetSysColor(COLOR_INFOTEXT)); // Add
    dc.DrawText(m_strFont,rc,DT_SINGLELINE|DT_VCENTER|DT_CENTER);
    // Do not call CWnd::OnPaint() for painting messages
    }


    In FontCombo.cpp

    I changed all instances of ANSI_CHARSET to DEFAULT_CHARSET.
    I also removed the early returns in the 2 Callback fns.
    These changes result in a font list with contents more similar to
    the one I get in the MSDev Tools|Options|Format tab.

    Reply
  • void CTipWnd::OnPaint()

    Posted by Legacy on 10/19/1998 12:00am

    Originally posted by: Akihiro Shamoto

    Modify this function...

    // from TipWnd.cpp
    void CTipWnd::OnPaint()
    {
    CPaintDC dc(this); // device context for painting

    dc.SelectObject(&m_font);
    CRect rc;
    GetClientRect(rc);
    dc.SetBkMode(TRANSPARENT); // Add!!
    dc.DrawText(m_strFont,rc,DT_SINGLELINE|DT_VCENTER|DT_CENTER);
    // Do not call CWnd::OnPaint() for painting messages
    }


    Reply
Leave a Comment
  • Your email address will not be published. All fields are required.

Top White Papers and Webcasts

  • The explosion in mobile devices and applications has generated a great deal of interest in APIs. Today's businesses are under increased pressure to make it easy to build apps, supply tools to help developers work more quickly, and deploy operational analytics so they can track users, developers, application performance, and more. Apigee Edge provides comprehensive API delivery tools and both operational and business-level analytics in an integrated platform. It is available as on-premise software or through …

  • The first phase of API management was about realizing the business value of APIs. This next wave of API management enables the hyper-connected enterprise to drive and scale their businesses as API models become more complex and sophisticated. Today, real world product launches begin with an API program and strategy in mind. This API-first approach to development will only continue to increase, driven by an increasingly interconnected web of devices, organizations, and people. To support this rapid growth, …

Most Popular Programming Stories

More for Developers

Latest Developer Headlines

RSS Feeds