📄 hlistbox.cpp
字号:
//****************************************************************************
// File:
//
// HListBox.cpp
//
// Purpose:
//
// Implementation file for class CHListBox.
//
// Functions:
//
// CHListBox::CHListBox Constructs a CHListBox
// CHListBox::~CHListBox Destructor
// CHListBox::LockHExtentUpdate Pauses automatic updating of the horizontal scroll bar
// CHListBox::UnlockHExtentUpdate Restores automatic updating of the horizontal scroll bar
// CHListBox::UpdateHExtent Updates the horizontal scroll bar extent after LockHExtentUpdate has been called
// CHListBox::InsertNewExtent Internal utility function used to maintain the extent array
// CHListBox::InitTabStops Internal tab stop init function
// CHListBox::OnAddString Intercepts LB_ADDSTRING msg
// CHListBox::OnInsertString Intercepts LB_INSERTSTRING msg
// CHListBox::OnDeleteString Intercepts LB_DELETESTRING msg
// CHListBox::OnSetTabStops Intercepts LB_SETTABSTOPS msg
//
// Development Team:
//
// James Rhodes
//
// Written by Microsoft Product Support Services, Languages Developer Support
// Copyright (c) 1996 Microsoft Corporation. All rights reserved.
//****************************************************************************
#include "stdafx.h"
#include "HListBox.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
//////////////////////////////////////////////////////////////////////////////
// CHListBox
//****************************************************************************
// Function:
//
// CHListBox::CHListBox()
//
// Purpose:
//
// Constructs a CHListBox object
//
// History:
//
// Date Comment Initials
// ======== ================================================= ========
// 1/31/96 Created JMR
//****************************************************************************
CHListBox::CHListBox()
{
m_bLocked = FALSE; // start out in auto mode
m_nLongestExtent = 0; // tracks longest extent, initially 0
m_nTabStops = 0; // no tab stops
m_lpTabStops = NULL; // array of tab stops
}
//****************************************************************************
// Function:
//
// CHListBox::~CHListBox()
//
// Purpose:
//
// Destructs a CHListBox object
//
// History:
//
// Date Comment Initials
// ======== ================================================= ========
// 1/31/96 Created JMR
//****************************************************************************
CHListBox::~CHListBox()
{
if (m_lpTabStops != NULL)
delete m_lpTabStops;
}
//****************************************************************************
// Function:
//
// CHListBox::LockHExtentUpdate()
//
// Purpose:
//
// Stops auto updating of horizontal extent
//
// History:
//
// Date Comment Initials
// ======== ================================================= ========
// 1/31/96 Created JMR
//****************************************************************************
void CHListBox::LockHExtentUpdate()
{
m_bLocked = TRUE;
}
//****************************************************************************
// Function:
//
// CHListBox::UnlockHExtentUpdate()
//
// Purpose:
//
// Turns auto updating back on
//
// History:
//
// Date Comment Initials
// ======== ================================================= ========
// 1/31/96 Created JMR
//****************************************************************************
void CHListBox::UnlockHExtentUpdate()
{
m_bLocked = FALSE;
}
//****************************************************************************
// Function:
//
// CHListBox::UpdateHExtent()
//
// Purpose:
//
// Updates horizontal extent when auto updating has been turned off
// for some period.
//
// History:
//
// Date Comment Initials
// ======== ================================================= ========
// 1/31/96 Created JMR
//****************************************************************************
void CHListBox::UpdateHExtent()
{
m_arrExtents.RemoveAll();
m_nLongestExtent = 0;
int nCount = GetCount();
CDC* pDC = GetDC();
HFONT hFont = (HFONT)SendMessage(WM_GETFONT);
CFont *pFont = CFont::FromHandle(hFont);
ASSERT (pFont);
CFont *pPrevFont = pDC->SelectObject(pFont);
CString csValue;
for (int i=0; i<nCount; i++)
{
GetText(i, csValue);
InsertNewExtent(i, csValue, pDC);
}
SetHorizontalExtent(m_nLongestExtent);
pDC->SelectObject(pPrevFont);
ReleaseDC(pDC);
}
//****************************************************************************
// Function:
//
// CHListBox::InsertNewExtent() PROTECTED
//
// Purpose:
//
// Used to update the extent array when a new item is added
//
// History:
//
// Date Comment Initials
// ======== ================================================= ========
// 1/31/96 Created JMR
//****************************************************************************
void CHListBox::InsertNewExtent(int nItem, LPCTSTR lpszStr, CDC* pDC)
{
if (NULL==m_lpTabStops)
InitTabStops();
CSize newExtent;
if (GetStyle() & LBS_USETABSTOPS)
newExtent = pDC->GetTabbedTextExtent(lpszStr, strlen(lpszStr), m_nTabStops, m_lpTabStops);
else
newExtent = pDC->GetTextExtent(lpszStr, strlen(lpszStr));
newExtent.cx += 6;
m_arrExtents.InsertAt(nItem, newExtent.cx);
if (newExtent.cx>m_nLongestExtent)
m_nLongestExtent = newExtent.cx;
}
void CHListBox::InsertNewExtent(int nItem, LPCTSTR lpszStr)
{
if (m_bLocked) return;
CDC* pDC = GetDC();
HFONT hFont = (HFONT)SendMessage(WM_GETFONT);
CFont *pFont = CFont::FromHandle(hFont);
ASSERT (pFont);
CFont* pPrevFont = pDC->SelectObject(pFont);
InsertNewExtent(nItem, lpszStr, pDC);
SetHorizontalExtent(m_nLongestExtent);
pDC->SelectObject(pPrevFont);
ReleaseDC(pDC);
}
//****************************************************************************
// Function:
//
// CHListBox::InitTabStops() PROTECTED
//
// Purpose:
//
// Initializes tab stops
//
// History:
//
// Date Comment Initials
// ======== ================================================= ========
// 1/31/96 Created JMR
//****************************************************************************
void CHListBox::InitTabStops()
{
int nDefault = 2;
SetTabStops(1, &nDefault);
}
BEGIN_MESSAGE_MAP(CHListBox, CListBox)
//{{AFX_MSG_MAP(CHListBox)
//}}AFX_MSG_MAP
ON_MESSAGE( LB_ADDSTRING, OnAddString )
ON_MESSAGE( LB_INSERTSTRING, OnInsertString )
ON_MESSAGE( LB_DELETESTRING, OnDeleteString )
ON_MESSAGE( LB_SETTABSTOPS, OnSetTabStops )
END_MESSAGE_MAP()
//////////////////////////////////////////////////////////////////////////////
// CHListBox message handlers
//****************************************************************************
// Function:
//
// CHListBox::OnAddString()
//
// Purpose:
//
// Intercepts the LB_ADDSTRING message to update the horizontal extent
//
// History:
//
// Date Comment Initials
// ======== ================================================= ========
// 1/31/96 Created JMR
//****************************************************************************
LRESULT CHListBox::OnAddString(WPARAM, LPARAM lParam)
{
LRESULT lResult = Default();
if (LB_ERR==lResult || LB_ERRSPACE==lResult)
return lResult;
InsertNewExtent(lResult, (LPCTSTR)lParam);
return lResult;
}
//****************************************************************************
// Function:
//
// CHListBox::OnInsertString()
//
// Purpose:
//
// Intercepts the LB_INSERTSTRING message to update the horizontal extent
//
// History:
//
// Date Comment Initials
// ======== ================================================= ========
// 1/31/96 Created JMR
//****************************************************************************
LRESULT CHListBox::OnInsertString(WPARAM, LPARAM lParam)
{
LRESULT lResult = Default();
if (LB_ERR==lResult || LB_ERRSPACE==lResult)
return lResult;
InsertNewExtent(lResult, (LPCTSTR)lParam);
return lResult;
}
//****************************************************************************
// Function:
//
// CHListBox::OnDeleteString()
//
// Purpose:
//
// Intercepts the LB_DELETESTRING message to update the horizontal extent
//
// History:
//
// Date Comment Initials
// ======== ================================================= ========
// 1/31/96 Created JMR
//****************************************************************************
LRESULT CHListBox::OnDeleteString(WPARAM wParam, LPARAM)
{
LRESULT lResult = Default();
if (LB_ERR==lResult)
return lResult;
if (m_bLocked) return lResult;
int nExtent = m_arrExtents[wParam];
m_arrExtents.RemoveAt(wParam);
if (nExtent>=m_nLongestExtent)
{
// 获取列表框最大长度
m_nLongestExtent = 0;
for (int i = 0; i<lResult; i++)
{
if (m_arrExtents[i]>m_nLongestExtent)
m_nLongestExtent = m_arrExtents[i];
}
}
SetHorizontalExtent(m_nLongestExtent);
return lResult;
}
//****************************************************************************
// Function:
//
// CHListBox::OnSetTabStops()
//
// Purpose:
//
// Intercepts the LB_SETTABSTOPS message to update tab stop array
//
// History:
//
// Date Comment Initials
// ======== ================================================= ========
// 1/31/96 Created JMR
//****************************************************************************
LRESULT CHListBox::OnSetTabStops(WPARAM wParam, LPARAM lParam)
{
LRESULT lResult = Default();
if (!lResult)
return lResult;
m_nTabStops = wParam;
if (NULL != m_lpTabStops)
{
delete []m_lpTabStops;
m_lpTabStops = NULL;
}
if (m_nTabStops>0)
{
m_lpTabStops = new int[m_nTabStops];
memcpy(m_lpTabStops, (void*)lParam, m_nTabStops*sizeof(int));
CDC* pDC = GetDC();
HFONT hFont = (HFONT)SendMessage(WM_GETFONT);
CFont *pFont = CFont::FromHandle(hFont);
ASSERT (pFont);
CFont* pPrevFont = pDC->SelectObject(pFont);
CSize size;
GetTextExtentPoint32(pDC->GetSafeHdc(),
_T("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"),
52, &size);
pDC->SelectObject(pPrevFont);
ReleaseDC(pDC);
int aveCharWidth = (size.cx/26+1)/2;
for (int i=0; i<m_nTabStops; i++)
m_lpTabStops[i] = (m_lpTabStops[i]*aveCharWidth+2)/4;
}
if (!m_bLocked)
UpdateHExtent();
return lResult;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -