How to add Horizontal Extent handling for a CListBox

Download Source Code


The following code illustrates how to implement Horizonal Extent handling for a CListBox. For more information you can look at Q66370. This class implements a derived CListBox which automatically sets the horizontal extent.

CListBoxEx.h

//
// CListBoxEx.h     v1.1
//

#ifndef _LISTBOX_EX_H
#define _LISTBOX_EX_H

#include 

class CListBoxEx : public CListBox
{
public:
     //virtual functions
     virtual int AddString( LPCTSTR sString );
     virtual int DeleteString( UINT nIndex );
     virtual int InsertString( int nIndex, LPCTSTR lpszItem );
     virtual void ResetContent();
     virtual int Dir( UINT attr, LPCTSTR lpszWildCard );

private:
     //helpers
     LONG GetExtentForString( LPCTSTR sText );
     long GetExtentForEntireControl();
};


#endif




CListBoxEx.cpp


// CListBoxEx.cpp   v1.0
//

#include "stdafx.h"
#include "listboxex.h"


//
// see Q66370 for more information
//

///////////////////////////////////////////////////////////////////////////
///////
//
//override functions
///////////////////////////////////////////////////////////////////////////
///////

int CListBoxEx::AddString(LPCTSTR lpszItem)
{

     LONG lStringExtent = GetExtentForString(lpszItem);
     if(GetHorizontalExtent() < lStringExtent)
        SetHorizontalExtent(lStringExtent); 

     return CListBox::AddString(lpszItem); 
}

int CListBoxEx::DeleteString( UINT nIndex ) 
{
     int nCountLeft=CListBox::DeleteString(nIndex);
     if (nCountLeft== LB_ERR) 
         return LB_ERR; 

      LONG lMaxExtent=GetExtentForEntireControl();
      SetHorizontalExtent(lMaxExtent); 

      return nCountLeft; 
}

int CListBoxEx::InsertString( int nIndex, LPCTSTR lpszItem ) 
{
      int nInsertPos=InsertString(nIndex, lpszItem); 
      if (nInsertPos== LB_ERR)
           return LB_ERR;

      LONG lStringExtent=GetExtentForString(lpszItem);
      if(GetHorizontalExtent() < lStringExtent) 
           SetHorizontalExtent(lStringExtent); 

      return nInsertPos; 
}

void CListBoxEx::ResetContent() 
{
      SetHorizontalExtent(0); 
      CListBox::ResetContent(); return; 
} 

int CListBoxEx::Dir( UINT attr, LPCTSTR lpszWildCard ) 
{
     int nReturn=Dir(attr, lpszWildCard ); 
     if (nReturn== LB_ERR || nReturn== LB_ERRSPACE) 
         return nReturn; 

     LONG lMaxExtent=GetExtentForEntireControl();
     SetHorizontalExtent(lMaxExtent); 
     return nReturn; 
}
////////////////////////////////////////////////////////////////////////////////// 
//helper functions 
////////////////////////////////////////////////////////////////////////////////// 

LONG CListBoxEx::GetExtentForString(LPCTSTR lpszItem)
{ 
    CDC* cdc=this->GetDC();
    CFont* font = this->GetFont();
    CFont* pOldFont;

    if (font)
     {
        pOldFont = cdc->SelectObject(font);

          TEXTMETRIC tm;
          cdc->GetTextMetrics(&tm);

          CSize size = cdc->GetTextExtent(lpszItem, lstrlen(lpszItem) );
          size.cx += tm.tmAveCharWidth;

          cdc->SelectObject(pOldFont);
          this->ReleaseDC(cdc);

          return (LONG) size.cx;
     }

     this->ReleaseDC(cdc);

     return 0L;
}

long CListBoxEx::GetExtentForEntireControl()
{
	CDC* cdc = this->GetDC();
	CFont* font = this->GetFont();
	CFont* pOldFont;
	if (font)
	{
		pOldFont = cdc->SelectObject(font);
		TEXTMETRIC tm;
		cdc->GetTextMetrics(&tm);
		CString sText;
		LONG max_cx = 0;

/****----------------C H A N G E---REY--8/29/98----------****
 ****		for(int n = 0; n<=GetCount(); n++)
 ****----------------------------------------------------****/
		for(int n = 0; n<GetCount(); n++)
/****--------------------END CHANGE----------------------****/
		{
			this->GetText(n, sText);
			if (sText == "")
				continue;
			CSize size = cdc->GetTextExtent(sText, sText.GetLength());
			size.cx += tm.tmAveCharWidth;  
			if(max_cx <= size.cx)
				max_cx = size.cx;          
		}
		cdc->SelectObject(pOldFont);
		this->ReleaseDC(cdc);
		return max_cx;     
	}     
	this->ReleaseDC(cdc);     
	return 0L;
}

BUGFIX (28 June 1998): In function GetExtentForEntireControl() changed SText = "" ro SText == "".
BUGFIX (4 Oct. 1998): In function GetExtentForEntireControl() modify for loop statement to fix Assertion error that occurs when you delete a string from the listbox.

Last updated: 11 November 1998

Comments:



Comments

  • Here You Will Find The Fashion Mbt Lami In Cheap Price

    Posted by mbt shoes on 12/01/2012 07:36pm

    can help you lose weight, it has been recognized by everyone, then is worn on the feet instantly see weight loss results? Of course not, it is a process, and now a look at how products are put on the experience of MBT process it.

    Reply
  • Strange

    Posted by siavoshkc on 06/05/2010 04:16pm

    Thank you. I thought we can use GetTextExtent directly and because DC knows the font there should be no problem calculating text width.

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

Top White Papers and Webcasts

  • On-demand Event Event Date: September 10, 2014 Modern mobile applications connect systems-of-engagement (mobile apps) with systems-of-record (traditional IT) to deliver new and innovative business value. But the lifecycle for development of mobile apps is also new and different. Emerging trends in mobile development call for faster delivery of incremental features, coupled with feedback from the users of the app "in the wild." This loop of continuous delivery and continuous feedback is how the best mobile …

  • Java developers know that testing code changes can be a huge pain, and waiting for an application to redeploy after a code fix can take an eternity. Wouldn't it be great if you could see your code changes immediately, fine-tune, debug, explore and deploy code without waiting for ages? In this white paper, find out how that's possible with a Java plugin that drastically changes the way you develop, test and run Java applications. Discover the advantages of this plugin, and the changes you can expect to see …

Most Popular Programming Stories

More for Developers

Latest Developer Headlines

RSS Feeds