📄 listbox_extent.shtml
字号:
<HTML>
<!-- Header information-->
<HEAD>
<META HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=iso-8859-1">
<META NAME="Author" CONTENT="Chris Maunder">
<TITLE>Controls - Adding Horizontal Extent handling to a CListBox</TITLE>
</HEAD>
<!-- Set background properties -->
<body background="../fancyhome/back.gif" bgcolor="#FFFFFF" link="#B50029" vlink="#8E2323" alink="#FF0000">
<!-- A word from our sponsors... -->
<table WIDTH="100%">
<tr WIDTH="100%"><td><!--#exec cgi="/cgi/ads.cgi"--><td></tr>
</table>
<!-- Article Title -->
<CENTER><H3><FONT COLOR="#AOAO99">
How to add Horizontal Extent handling for a CListBox
</FONT></H3></CENTER>
<CENTER><H3><HR></H3></CENTER>
<!-- Author and contact details -->
This article was contributed by <A HREF="mailto:rhollis@keypoint.demon.co.uk">Richard Hollis</A>.
<!-- Sample image and source code/demo project -->
<P><A HREF="listbox_extent.zip">Download Source Code</A></p>
<br>
<!-- The article... -->
<p>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.
<h4>CListBoxEx.h</h4>
<FONT COLOR="#990000"><TT><PRE>
//
// CListBoxEx.h v1.0
//
#ifndef _LISTBOX_EX_H
#define _LISTBOX_EX_H
#include <afxwin.h>
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
</tt></PRE></FONT>
<br><br><br>
<h4>CListBoxEx.cpp</h4>
<FONT COLOR="#990000"><TT><PRE>
// 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;
for(int n = 0; n<=GetCount(); n++)
{
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;
}
</tt></PRE></FONT>
<p>BUGFIX (28 June 1998): In function GetExtentForEntireControl() changed SText = "" ro SText == "".
<!-- Remember to update this -->
<p>Last updated: 28 June 1998
<P><HR>
<!-- Codeguru contact details -->
<TABLE BORDER=0 WIDTH="100%">
<TR>
<TD WIDTH="33%"><FONT SIZE=-1><A HREF="http://www.codeguru.com">Goto HomePage</A></FONT></TD>
<TD WIDTH="33%"><CENTER><FONT SIZE=-2>© 1997 Zafir Anjum</FONT> </CENTER></TD>
<TD WIDTH="34%"><DIV ALIGN=right><FONT SIZE=-1>Contact me: <A HREF="mailto:zafir@home.com">zafir@home.com</A> </FONT></DIV></TD>
</TR>
</TABLE>
<!-- Counter -->
<!-- COUNTER REMOVED -->
</BODY>
</HTML>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -