Click here to Skip to main content
15,893,190 members
Articles / Desktop Programming / MFC

A CListBox with automatic HSCROLL maintenance

Rate me:
Please Sign up or sign in to vote.
4.50/5 (8 votes)
27 Jun 20013 min read 159.1K   1.7K   41  
Taking the pain out of adding a horizontal scrollbar to a listbox.
// HListBox.cpp : implementation file
//

#include "stdafx.h"
#include "hscroll.h"
#include "HListBox.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

/////////////////////////////////////////////////////////////////////////////
// CHListBox

CHListBox::CHListBox()
{
 width = 0;
}

CHListBox::~CHListBox()
{
}


BEGIN_MESSAGE_MAP(CHListBox, CListBox)
	//{{AFX_MSG_MAP(CHListBox)
		// NOTE - the ClassWizard will add and remove mapping macros here.
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CHListBox message handlers
void CHListBox::updateWidth(LPCTSTR s)
    {
     CClientDC dc(this);
     CFont * f = CListBox::GetFont();
     dc.SelectObject(f);
     CSize sz = dc.GetTextExtent(s, _tcslen(s));
     sz.cx += 3 * ::GetSystemMetrics(SM_CXBORDER);
     if(sz.cx > width)
	 { /* extend */
	  width = sz.cx;
	  CListBox::SetHorizontalExtent(width);
	 } /* extend */
    }

int CHListBox::AddString(LPCTSTR s)
    {
     int result = CListBox::AddString(s);
     if(result < 0)
	 return result;
     updateWidth(s);
     return result;
    }

int CHListBox::InsertString(int i, LPCTSTR s)
    {
     int result = CListBox::InsertString(i, s);
     if(result < 0)
	 return result;
     updateWidth(s);
     return result;
    }

void CHListBox::ResetContent()
    {
     CListBox::ResetContent();
     width = 0;
    }

int CHListBox::DeleteString(int n)
    {
     int result = CListBox::DeleteString(n);
     if(result < 0)
	 return result;
     CClientDC dc(this);

     CFont * f = CListBox::GetFont();
     dc.SelectObject(f);

     width = 0;
     for(int i = 0; i < CListBox::GetCount(); i++)
	 { /* scan strings */
	  CString s;
	  CListBox::GetText(i, s);
	  CSize sz = dc.GetTextExtent(s);
          sz.cx += 3 * ::GetSystemMetrics(SM_CXBORDER);
	  if(sz.cx > width)
	      width = sz.cx;
	 } /* scan strings */
     CListBox::SetHorizontalExtent(width);
     return result;
    }

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
Retired
United States United States
PhD, Computer Science, Carnegie Mellon University, 1975
Certificate in Forensic Science and the Law, Duquesne University, 2008

Co-Author, [i]Win32 Programming[/i]

Comments and Discussions