重点几个函数
参考 http://blog.csdn.net/god00/article/details/6783947
int CHScrollbarListBox::AddString( LPCTSTR lpszItem )
{
int nRet = CListBox::AddString(lpszItem);
SCROLLINFO scrollInfo;
memset(&scrollInfo, 0, sizeof(SCROLLINFO));
scrollInfo.cbSize = sizeof(SCROLLINFO);
scrollInfo.fMask = SIF_ALL;
GetScrollInfo(SB_VERT, &scrollInfo, SIF_ALL);
int nScrollWidth = 0; //vScrollwidth
if(GetCount() > 1 && ((int)scrollInfo.nMax >= (int)scrollInfo.nPage))
{
nScrollWidth = GetSystemMetrics(SM_CXVSCROLL);
//Metrics 分辨率 宽度以分辨率为单位
}
RECT lRect;
GetWindowRect(&lRect);
int nBoxWidth = lRect.right - lRect.left - nScrollWidth;
SIZE sSize;
CClientDC myDC(this);
CFont* pListBoxFont = GetFont();
if(pListBoxFont == NULL)
{
}
CFont* pOldFont =
myDC.SelectObject(pListBoxFont);
GetTextExtentPoint32(myDC.m_hDC, lpszItem, _tcslen(lpszItem), &sSize);
m_nMaxWidth = max(m_nMaxWidth, (int)sSize.cx);
if (m_nMaxWidth+5 > nBoxWidth)
{
SetHorizontalExtent(m_nMaxWidth + 4);
}
myDC.SelectObject(pOldFont);
return nRet;
}
int CHScrollbarListBox::DeleteString( UINT nIndex )
{
int nRet = CListBox::DeleteString(nIndex);
RECT lRect;
GetWindowRect(&lRect);
SCROLLINFO scrollInfo;
memset(&scrollInfo, 0, sizeof(SCROLLINFO));
scrollInfo.cbSize = sizeof(SCROLLINFO);
scrollInfo.fMask = SIF_ALL;
GetScrollInfo(SB_VERT, &scrollInfo, SIF_ALL);
int nScrollWidth = 0; //vScrollwidth
if(GetCount() > 1 && ((int)scrollInfo.nMax
>= (int)scrollInfo.nPage))
{
nScrollWidth = GetSystemMetrics(SM_CXVSCROLL);
}
SIZE sSize;
CClientDC myDC(this);
CString lpszItem;
CFont* pListFont = GetFont();
CFont* pOldFont = myDC.SelectObject(pListFont);
int nBoxWidth = lRect.right - lRect.left - nScrollWidth;
m_nMaxWidth = 0;
for (int i = 0 ; i < GetCount(); i ++)
{
CListBox::GetText(i,lpszItem);
GetTextExtentPoint32(myDC.m_hDC,lpszItem,_tcslen(lpszItem.GetBuffer()),&sSize);
lpszItem.ReleaseBuffer();
m_nMaxWidth = max(m_nMaxWidth, (int)sSize.cx);
}
myDC.SelectObject(pOldFont);
if(nBoxWidth < m_nMaxWidth +5)
{
SetHorizontalExtent(m_nMaxWidth + 4);
}else
{
ShowScrollBar(SB_HORZ,FALSE);
SetHorizontalExtent(0);
}
return nRet;
}
void CHScrollbarListBox::ResetContent()
{
CListBox::ResetContent();
m_nMaxWidth = 0 ;
SetHorizontalExtent(0);
}