📄 coollistbox.cpp
字号:
// CoolListBox.cpp : implementation file
// Author: Zalsoft 98
//
#include "stdafx.h"
#include "DrawList.h"
#include "CoolListBox.h"
#include "math.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
// CoolListBox
CoolListBox::CoolListBox():m_itemheigth(0),
m_down (0),
m_nCurSel(-1),
m_bThisAddData(0)
{
TRACE("CoolListBox object done\r\n");
}
// Function name : CoolListBox::~CoolListBox
// Description :
// Return type :
CoolListBox::~CoolListBox()
{
}
BEGIN_MESSAGE_MAP(CoolListBox, CListBox)
//{{AFX_MSG_MAP(CoolListBox)
ON_CONTROL_REFLECT(LBN_SELCHANGE, OnSelchange)
ON_WM_DESTROY()
ON_WM_CREATE()
ON_WM_LBUTTONUP()
ON_WM_LBUTTONDOWN()
ON_WM_MOUSEMOVE()
ON_WM_CTLCOLOR_REFLECT()
ON_WM_VSCROLL()
//}}AFX_MSG_MAP
ON_MESSAGE(WM_ENABLE,OnEnableDisable)
END_MESSAGE_MAP()
//PUBLIC METHODS //////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////
// Function name : CoolListBox::AddStringEx
// Description : Add String and Image Index
int CoolListBox::AddStringEx(LPCSTR stringText, UINT imgID)
{
int i = AddString(stringText);
S_DATA* pData = new S_DATA();
pData->dwData = 0;
pData->imgIDX = imgID;
m_bThisAddData = TRUE;
SetItemData(i,(DWORD)pData);
m_bThisAddData = FALSE;
return i;
}
//PUBLIC METHODS //////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////
// Function name : CoolListBox::InsertStringEx
// Description :
int CoolListBox::InsertStringEx(int index,LPCSTR stringText, UINT imgID)
{
int i = InsertString(index,stringText);
S_DATA* pData = new S_DATA();
pData->dwData = 0;
pData->imgIDX = imgID;
m_bThisAddData = TRUE;
SetItemData(i,(DWORD)pData);
m_bThisAddData = FALSE;
return i;
}
// Function name : CoolListBox::DrawItem
// Description :
// Return type : void
// Argument : DRAWITEMSTRUCT* pDraw
void CoolListBox::DrawItem(DRAWITEMSTRUCT* pDraw)
{
CString cLocal;
RECT rWork;
int crOldBack,crOldText;
CDC* pDC = CDC::FromHandle( pDraw->hDC );
::CopyRect(&rWork,&pDraw->rcItem);
// Get's the text. LB_HASSTRINGS should be specified on list styles
GetText(pDraw->itemID,cLocal);
// top / bottom auto scroll removed on 22 Aug 98 (no fast scroll suported)
Draw(pDC,cLocal,&rWork,(S_DATA*)pDraw->itemData,
pDraw->itemState & ODS_SELECTED,
m_nCurSel==(int)pDraw->itemID,
/*&*/crOldText,
/*&*/crOldBack);
// restore dc attributes
pDC->SetTextColor( crOldText );
pDC->SetBkColor( crOldBack );
}
// Function name : CoolListBox::MeasureItem
// Description : This does not seems to come if LBS_OWNERDRAWFIXED even
// if the size has changed. should be LBS_OWNERDRAWVARIABLE
// Used to calculate the wide of each item, Bitmap is a square
void CoolListBox::MeasureItem(MEASUREITEMSTRUCT* lpMeasureItemStruct)
{
//
// firs time whan come the bitmap heigth =0 , Get the bitmap height
//
if (m_hWnd &&
0 == m_itemheigth)
{
CBitmap cbm;
// BITMAP ID should match LISTBOX ID
VERIFY(cbm.LoadBitmap(GetDlgCtrlID()));
//
BITMAP bm;
cbm.GetObject(sizeof(bm), &bm);
// item heigth is bitmap height + 2 // for borders
m_itemheigth = bm.bmHeight + 2;
cbm.DeleteObject();
}
lpMeasureItemStruct->itemHeight = m_itemheigth;
}
// Function name : CoolListBox::PreSubclassWindow
// Description :
void CoolListBox::PreSubclassWindow()
{
CListBox::PreSubclassWindow();
ASSERT(GetStyle() & (LBS_OWNERDRAWVARIABLE|LBS_HASSTRINGS));
// Cr4eate the image list. Each elemet is a square L = m_itemheigth
m_imglist.Create( GetDlgCtrlID(),m_itemheigth, 0,RGB(192,192,192));
// create the brush that is returned from CtlColor, avoid blinks when updated
// beneath the item rects, it has the same color as a normal item (COLORBTNFACE)
m_brush = ::CreateSolidBrush(::GetSysColor(COLOR_BTNFACE));
}
// Function name : CoolListBox::OnSelchange
// Description : If you subclass CoolListBox , call CoolListBox::OnSelchange()
// from your ::OnSelChange()
void CoolListBox::OnSelchange()
{
if(m_nCurSel != -1
&& GetCurSel() == -1)
{
RECT rt;
GetItemRect(m_nCurSel ,&rt);
InvalidateRect(&rt);
SetCurSel(m_nCurSel);
}
m_nCurSel = -1;
}
// Function name : CoolListBox::OnDestroy
// Description : Cleans the data
void CoolListBox::OnDestroy()
{
::DeleteObject(m_brush);
int i= GetCount();
S_DATA* pData;
// protect fake data
m_bThisAddData = TRUE;
while(--i>=0)
{
pData = (S_DATA*)GetItemData(i);
delete pData;
pData = 0;
}
m_imglist.DeleteImageList();
// unprotect fake data
m_bThisAddData = FALSE;
CListBox::OnDestroy();
}
// Function name : CoolListBox::OnCreate
// Description : If u create window move the code from Presubclass window Here
int CoolListBox::OnCreate(LPCREATESTRUCT lpCreateStruct)
{
if (CListBox::OnCreate(lpCreateStruct) == -1)
return -1;
return 0;
}
// Function name : Draw's
// Description :
void CoolListBox::Draw(CDC* pDC, LPCSTR string, RECT* prt,
S_DATA* pData, BOOL bSelected,
BOOL bPointed, int& crOldText,
int& crOldBack)
{
BOOL bEnable = IsWindowEnabled();
// origin pt for icon
CPoint cpt(prt->left+3,prt->top+1);
if(bSelected && (m_down || !bPointed))
{
crOldBack = pDC->SetBkColor(bEnable ? 0 : RGB(96,96,96));
crOldText = pDC->SetTextColor(bEnable ? RGB(255,255,255):
GetSysColor(COLOR_GRAYTEXT));
}
else
{
crOldBack = pDC->SetBkColor(GetSysColor(COLOR_BTNFACE));
crOldBack = pDC->SetTextColor(bEnable?GetSysColor(COLOR_BTNTEXT):
GetSysColor(COLOR_GRAYTEXT));
}
// adjust the rect << ?!!
InflateRect(prt,-3,-1);
// fill the rect with brush
pDC->ExtTextOut(prt->left ,prt->top,ETO_OPAQUE,prt,"",0,0);
// only if the item has added AddStringEx(LPCSTR stringText, UINT imgID)
// has the id of image
if(pData->imgIDX != -1)
{
if(bEnable)
{
m_imglist.Draw(pDC, pData->imgIDX, cpt, ILD_TRANSPARENT );
}
else
{
HICON hic;
hic = m_imglist.ExtractIcon(pData->imgIDX);
pDC->DrawState(cpt,CSize(0,0),hic,DSS_DISABLED,(HBRUSH)NULL);
::DestroyIcon(hic);
}
}
// offset a bit the text
prt->left += (m_itemheigth+12);
pDC->DrawText(string,strlen(string),prt,DT_SINGLELINE|DT_VCENTER|DT_LEFT);
prt->left -= (m_itemheigth+12);
// draw pointed states
if(bPointed)
{
if(bSelected)
pDC->Draw3dRect(prt,0,0xFFFFFF);
else
pDC->Draw3dRect(prt,0x00FFFFFF,0);
}
else if( bSelected)
{
pDC->Draw3dRect(prt,0,0xFFFFFF);
}
}
// Function name : CoolListBox::OnLButtonUp
// Description : Invalidate this rect when clickunclick
void CoolListBox::OnLButtonUp(UINT nFlags, CPoint point)
{
m_down = FALSE;
CListBox::OnLButtonUp(nFlags, point);
RECT rt; BOOL b;
int index = ItemFromPoint( point, b) ;
GetItemRect(index,&rt);
InvalidateRect(&rt);
}
// Function name : CoolListBox::OnLButtonDown
// Description : Invalidate this rect when clickunclick
void CoolListBox::OnLButtonDown(UINT nFlags, CPoint point)
{
m_down = TRUE;
CListBox::OnLButtonDown(nFlags, point);
int index = GetCurSel();//ItemFromPoint( point, b) ;
RECT rt;
GetItemRect(index,&rt);
InvalidateRect(&rt);
}
// Function name : CoolListBox::OnMouseMove
// Description :
void CoolListBox::OnMouseMove(UINT nFlags, CPoint point)
{
RECT rt; BOOL b;
int index = ItemFromPoint( point, b) ;
if(index != m_nCurSel)
{
GetItemRect(index,&rt);
InvalidateRect(&rt);
if(m_nCurSel!=-1)
{
GetItemRect(m_nCurSel,&rt);
InvalidateRect(&rt);
}
m_nCurSel = index;
}
CListBox::OnMouseMove(nFlags, point);
}
// Function name : CoolListBox::CtlColor
// Description :
HBRUSH CoolListBox::CtlColor(CDC* pDC, UINT nCtlColor)
{
return m_brush;
return NULL;
}
// Function name : CoolListBox::OnVScroll
// Description : Used in the Autoscroll version
void CoolListBox::OnVScroll(UINT nSBCode, UINT nPos, CScrollBar* pScrollBar)
{
CListBox::OnVScroll(nSBCode, nPos, pScrollBar);
}
// Function name : CoolListBox::OnEnableDisable
// Description :
LRESULT CoolListBox::OnEnableDisable(WPARAM wpIndex,LPARAM lpData)
{
Invalidate();
return Default();
}
int CoolListBox::SetItemData( int nIndex, DWORD dwItemData )
{
if(!m_bThisAddData)
{
S_DATA* pData = 0;
if(0 == (pData = (S_DATA*)CListBox::GetItemData(nIndex)))
{
pData = new S_DATA();
pData->imgIDX = -1;
}
pData->dwData = dwItemData;
dwItemData = (DWORD)pData;
}
return CListBox::SetItemData(nIndex, dwItemData );
}
DWORD CoolListBox::GetItemData( int nIndex ) const
{
if(!m_bThisAddData)
{
const S_DATA* pData = (S_DATA*)CListBox::GetItemData(nIndex);
return pData ? pData->dwData : 0;
}
return CListBox::GetItemData(nIndex);
}
int CoolListBox::DeleteString( UINT nIndex )
{
delete ((S_DATA*)CListBox::GetItemData(nIndex));
return CListBox::DeleteString( nIndex );
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -