📄 mylist.cpp
字号:
// MyList.cpp: implementation of the CMyList class.
//
//////////////////////////////////////////////////////////////////////
#include "stdafx.h"
#include "MyLabel.h"
#include "MyList.h"
#include "front.h"
#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[]=__FILE__;
#define new DEBUG_NEW
#endif
//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////
CMyList::CMyList()
{
}
CMyList::~CMyList()
{
CleanItems();
delete UpButton;
delete DownButton;
}
CMyList::CMyList(CWnd *parentWnd,UINT id,bool enable)
{
iGap=23;
iStartTop=0;
iTopMargin=5;
iLeftMargin=8;
pParentWnd=parentWnd;
col=RGB(243, 243, 243);
bordercol=col;
nID=id;
bEnable=enable;
iFlag=-1;//用于区分一个父窗口中有多个列表框,当是-2时为选择框
bAnimate=false; //不动画出现
UpButton=new CMyLabel(NULL,1,5,true,false,nID,12345,pParentWnd);
UpButton->OpenBitmap("res/UP.bmp");
UpButton->SetEnable(false);
DownButton=new CMyLabel(NULL,1,6,true,false,nID,54321,pParentWnd);
DownButton->OpenBitmap("res/DOWN.bmp");
DownButton->SetEnable(false);
}
void CMyList::AddItem(char *name, int imageindex,bool fromfile,int act,char *filename)
{
CMyLabel *label;
if(iFlag!=-2)
label=new CMyLabel(name,3,imageindex,true, true,nID,iFlag,pParentWnd,fromfile);
else
label=new CMyLabel(name,3,imageindex,false, true,nID,iFlag,pParentWnd,fromfile);
label->iAct=act;
if(label->bIsFile)
{
if(imageindex==0)label->OpenBitmap("res/boy.bmp");
if(imageindex==1)label->OpenBitmap("res/girl.bmp");
if(imageindex==2)label->OpenBitmap(filename);
label->StretchWidth=1;
}
arItems.Add((void *)label);
}
void CMyList::OnMouseMove(CPoint point)
{
if(bEnable)
{
UpButton->OnMouseMove(point);
DownButton->OnMouseMove(point);
if(!(UpButton->LabelRect.PtInRect(point)||DownButton->LabelRect.PtInRect(point)))
{
CMyLabel *label;
for(int i=0;i<arItems.GetSize();i++)
{
label=(CMyLabel *)arItems.GetAt(i);
//label->iAct=i;
label->OnMouseMove(point);
}
}
}
}
void CMyList::OnLButtonUp(CPoint point)
{
if(bEnable)
{
UpButton->OnLButtonUp(point);
DownButton->OnLButtonUp(point);
if(!(UpButton->LabelRect.PtInRect(point)||DownButton->LabelRect.PtInRect(point)))
{
CMyLabel *label;
for(int i=0;i<arItems.GetSize();i++)
{
label=(CMyLabel *)arItems.GetAt(i);
//label->iAct=i;
label->OnLButtonUp(point);
}
}
}
}
void CMyList::OnLButtonDown(CPoint point)
{
if(bEnable)
{
UpButton->OnLButtonDown(point);
DownButton->OnLButtonDown(point);
if(!(UpButton->LabelRect.PtInRect(point)||DownButton->LabelRect.PtInRect(point)))
{
CMyLabel *label;
for(int i=0;i<arItems.GetSize();i++)
{
label=(CMyLabel *)arItems.GetAt(i);
//label->iAct=i;
label->OnLButtonDown(point);
}
}
}
}
void CMyList::OnRButtonDown(CPoint point)
{
if(bEnable)
{
UpButton->OnRButtonDown(point);
DownButton->OnRButtonDown(point);
if(!(UpButton->LabelRect.PtInRect(point)||DownButton->LabelRect.PtInRect(point)))
{
CMyLabel *label;
for(int i=0;i<arItems.GetSize();i++)
{
label=(CMyLabel *)arItems.GetAt(i);
//label->iAct=i;
label->OnRButtonDown(point);
}
}
}
}
void CMyList::draw(CDC *pDC)
{
CPen *pOldPen,pen;
CBrush *pOldBrush,brush;
brush.CreateSolidBrush(col);
pen.CreatePen(PS_SOLID | PS_COSMETIC, 1, bordercol);
pOldBrush = pDC->SelectObject(&brush);
pOldPen = pDC->SelectObject(&pen);
pDC->RoundRect(ListRect,CPoint(2,2));
pDC->SelectObject(pOldPen);
pDC->SelectObject(pOldBrush);
int AllCount=arItems.GetSize();
int start=iStartTop,end=iStartTop+ListRect.Height(),mid;
CMyLabel *label;
for(int i=0,k=0;i<AllCount;i++)
{
label=(CMyLabel *)arItems.GetAt(i);
mid=i*iGap;
if(mid>=start&&mid<end-10)
{
label->setPos(ListRect.left+iLeftMargin,ListRect.top+k*iGap+iTopMargin,ListRect.right-2,ListRect.top+k*iGap+iTopMargin+18);
label->DrawLabel(pDC);
k++;
label->SetEnable(true);
continue;
}
if(mid>=end)
{
DownButton->SetEnable(true);
DownButton->DrawLabel(pDC);
}
else
DownButton->SetEnable(false);
label->SetEnable(false); //不在窗口中的使他们不能用
}
if(iStartTop>0)
{
UpButton->SetEnable(true);
UpButton->DrawLabel(pDC);
}
else
UpButton->SetEnable(false);
}
void CMyList::listup()
{
if(iStartTop!=0)iStartTop-=iGap;
}
void CMyList::listdown()
{
int a=(arItems.GetSize()-1)*iGap;
if(iStartTop<a)
iStartTop+=iGap;
}
void CMyList::setPos(CRect rc)
{
ListRect=rc;
UpButton->setPos(ListRect.right-25,ListRect.top+3,ListRect.right-5,ListRect.top+21);
DownButton->setPos(ListRect.right-25,ListRect.bottom-21,ListRect.right-5,ListRect.bottom-3);
}
void CMyList::OnTimer()
{
if(DownButton->bPressed&&DownButton->bPass)listdown();
if(UpButton->bPressed&&UpButton->bPass)listup();
}
void CMyList::OutRect()
{
UpButton->OutRect();
DownButton->OutRect();
CMyLabel *label;
for(int i=0;i<arItems.GetSize();i++)
{
label=(CMyLabel *)arItems.GetAt(i);
label->OutRect();
}
}
void CMyList::AnimateOut()
{
if(!bAnimate)return;
CClientDC dc(pParentWnd);
CDC memDC;
memDC.CreateCompatibleDC(&dc);
CBitmap bmpOut;
CBitmap * obmp;
CDC * pDC = &memDC;
CFont * of = pDC->SelectObject(CFont::FromHandle((HFONT)GetStockObject(DEFAULT_GUI_FONT)));
int obk = pDC->SetBkMode(TRANSPARENT);
bmpOut.CreateCompatibleBitmap(&dc, ListRect.Width(),ListRect.Height()-1);
obmp = pDC->SelectObject(&bmpOut);
CRect rc;
rc=ListRect;
ListRect.SetRect(0,0,ListRect.Width(),ListRect.Height());
draw(pDC);
ListRect=rc;
for(int y=1;y<ListRect.Height();y+=3)
{
dc.BitBlt(ListRect.left,ListRect.top,ListRect.Width(),y,&memDC,0,ListRect.Height()-y,SRCCOPY);
Sleep(1);
}
pDC->SelectObject(of);
pDC->SelectObject(obmp);
pDC->SetBkMode(obk);
memDC.DeleteDC();
bAnimate=false;
}
void CMyList::CleanItems()
{
int i=0,AllCount=arItems.GetSize();
CMyLabel *temp;
for(i=0;i<AllCount;i++)
{
temp=(CMyLabel *)arItems.GetAt(i);
if(temp)
delete temp;
}
if(AllCount>0)arItems.RemoveAll();
}
int CMyList::GetSize()
{
return arItems.GetSize();
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -