Files

114 lines
2.8 KiB
C++
Raw Permalink Normal View History

2026-10-04 00:04:20 +08:00
#include "stdafx.h"
2026-09-28 15:28:50 +08:00
#include "Resource.h"
#include "KTInstLineListBox.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
IMPLEMENT_DYNCREATE(KTInstLineListBox, CListBox)
KTInstLineListBox::KTInstLineListBox()
: m_pImageList(NULL)
{
}
KTInstLineListBox::~KTInstLineListBox()
{
}
BEGIN_MESSAGE_MAP(KTInstLineListBox, CListBox)
//{{AFX_MSG_MAP(KTInstLineListBox)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
BOOL KTInstLineListBox::Create(DWORD dwStyle, const RECT& rect, CWnd* pParentWnd, UINT nID)
{
dwStyle &= ~(LBS_MULTICOLUMN|LBS_OWNERDRAWVARIABLE);
dwStyle |= (WS_VSCROLL|LBS_OWNERDRAWFIXED|LBS_HASSTRINGS);
return CListBox::Create(dwStyle, rect, pParentWnd, nID);
}
void KTInstLineListBox::MeasureItem(LPMEASUREITEMSTRUCT lpMeasureItemStruct)
{
if (lpMeasureItemStruct->itemID == (UINT) -1)
return;
IMAGEINFO info = {0};
ASSERT(NULL != m_pImageList);
m_pImageList->GetImageInfo(lpMeasureItemStruct->itemID, &info);
CDC* pDC = GetDC();
TEXTMETRIC tm;
pDC->GetTextMetrics(&tm);
lpMeasureItemStruct->itemHeight = max(tm.tmHeight, (info.rcImage.bottom - info.rcImage.top));
ReleaseDC(pDC);
}
void KTInstLineListBox::DrawItem(LPDRAWITEMSTRUCT lpDrawItemStruct)
{
int iIndex = lpDrawItemStruct->itemID;
if (iIndex < 0)
return;
CString sText;
CListBox::GetText(iIndex, sText);
int iImage = CListBox::GetItemData(iIndex);
IMAGEINFO info = {0};
ASSERT(NULL != m_pImageList);
m_pImageList->GetImageInfo(iImage, &info);
CRect rcItem = lpDrawItemStruct->rcItem;
CDC* pDC = CDC::FromHandle(lpDrawItemStruct->hDC);
COLORREF crOldTextColor = pDC->GetTextColor();
COLORREF crOldBkColor = pDC->GetBkColor();
pDC->Draw3dRect(rcItem, crOldBkColor, crOldBkColor);
if ((lpDrawItemStruct->itemAction | ODA_FOCUS) &&
(lpDrawItemStruct->itemState & ODS_FOCUS))
{
pDC->DrawFocusRect(rcItem);
rcItem.DeflateRect(0, 1);
}
rcItem.DeflateRect(1, 0);
if ((lpDrawItemStruct->itemAction | ODA_SELECT) &&
(lpDrawItemStruct->itemState & ODS_SELECTED))
{
pDC->SetTextColor(::GetSysColor(COLOR_HIGHLIGHTTEXT));
pDC->SetBkColor(::GetSysColor(COLOR_HIGHLIGHT));
pDC->FillSolidRect(rcItem, ::GetSysColor(COLOR_HIGHLIGHT));
}
else
{
pDC->FillSolidRect(rcItem, crOldBkColor);
}
m_pImageList->Draw(pDC, iImage, rcItem.TopLeft(), ILD_TRANSPARENT);
rcItem.left += (info.rcImage.right - info.rcImage.left);
rcItem.left += 2;
pDC->DrawText(sText, rcItem, DT_SINGLELINE | DT_LEFT | DT_VCENTER);
pDC->SetTextColor(crOldTextColor);
pDC->SetBkColor(crOldBkColor);
}
int KTInstLineListBox::AddString(LPCTSTR lpszLayer, UINT nImage)
{
int iIndex = CListBox::AddString(lpszLayer);
if (iIndex >= 0)
CListBox::SetItemData(iIndex, nImage);
return iIndex;
}
void KTInstLineListBox::SetImageList(CImageList* pImageList)
{
ASSERT(pImageList != NULL);
m_pImageList = pImageList;
}