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

  • There is no understating the impact of security and network connectivity on today's cloud environments. Yes, clouds can scale, expedite processing, and reduce costs, but they also incur risks associated with multi-tenancy, availability, and access control. How users connect to the cloud is vital because not only are security risks non-negotiable, but performance, flexibility, and reliability are critical as well, which leads to a heavy emphasis on combining strong security with private network ecosystems. …

  • The enterprise has experienced a data explosion over the past few years that has redefined the possible. In the HP eBook Big Data 20/20, HP experts explore what the future of Big Data and analytics means for that enterprise. The widely varied deluge of data in companies presents both challenges and opportunities to deliver greater value and gain competitive advantage.

Most Popular Programming Stories

More for Developers

RSS Feeds

Thanks for your registration, follow us on our social networks to keep up-to-date