Шрифт:
// ODListBox.h : header file
//
/////////////////////////////////////////////////////////////////////////////
// CODListBox window
#include "TitleTipListBox.h"
class CODListBox : public CTitleTipListBox { // Construction public:
CODListBox;
// Overrides
// ClassWizard generated virtual function overrides
//{{AFX_VIRTUAL(CODListBox)
public:
virtual void DrawItem(LPDRAWITEMSTRUCT lpDrawItemStruct);
virtual void MeasureItem(LPMEASUREITEMSTRUCT lpMeasureItemStruct);
//}}AFX_VIRTUAL
// Implementation
public:
virtual ~CODListBox;
protected:
const int m_nEdgeSpace; // Дополнительное пространство вокруг текста
const int m_nFontHeight; // Высота шрифта
CFont m_Font; // Шрифт для отображения строк
virtual int GetIdealItemRect(int nIndex, LPRECT lpRect);
// Generated message map functions protected:
//{{AFX_MSG(CODListBox)
// NOTE – the ClassWizard will add and remove member functions here.
//}}AFX_MSG
DECLARE_MESSAGE_MAP
};
///////////////////////////////////////////////////////////////////////////// // ODListBox.cpp : implementation file //
#include "stdafx.h"
#include "TTDemo.h"
#include "ODListBox.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
// CODListBox
CODListBox::CODListBox : m_nEdgeSpace(4), m_nFontHeight(20) {
VERIFY(m_Font.CreateFont(m_nFontHeight, 0, 0, 0, FW_BOLD, 0, 0, 0, ANSI_CHARSET, OUT_TT_PRECIS, CLIP_DEFAULT_PRECIS, DEFAULT_QUALITY, DEFAULT_PITCH | FF_DONTCARE, "Arial"));
}
CODListBox::~CODListBox { }
int CODListBox::GetIdealItemRect(int nIndex, LPRECT lpRect) {
ASSERT(nIndex >= 0);
int nResult = GetItemRect(nIndex, lpRect);
if (nResult != LB_ERR) {
CClientDC DC(this);
CFont* pOldFont = DC.SelectObject(&m_Font);
// Calculate the text length.
CString strItem;
GetText(nIndex, strItem);
CSize TextSize = DC.GetTextExtent(strItem);
// Взять максимум от обычной ширины и идеальной ширины.
lpRect->right = max(lpRect->right, lpRect->left + TextSize.cx + (m_nEdgeSpace * 2));
DC.SelectObject(pOldFont);
}
return nResult;
}
BEGIN_MESSAGE_MAP(CODListBox, CTitleTipListBox)
//{{AFX_MSG_MAP(CODListBox)
// NOTE – the ClassWizard will add and remove mapping macros here.
//}}AFX_MSG_MAP
END_MESSAGE_MAP
/////////////////////////////////////////////////////////////////////////////
// CODListBox message handlers
void CODListBox::DrawItem(LPDRAWITEMSTRUCT lpDrawItemStruct) {
CDC* pDC = CDC::FromHandle(lpDrawItemStruct->hDC);
ASSERT_VALID(pDC);
int nSavedDC = pDC->SaveDC;
CString strItem;
if (lpDrawItemStruct->itemID != –1) {