#include "stdafx.h"
#include "ItemList.h"
#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[]=__FILE__;
#define new DEBUG_NEW
#endif
/****************************************************************************
绘制水平颜色渐变区域
DrawGradientV( HDC hdc //绘图刷子
COLORREF co1 //左端颜色
COLORREF co2 //右端颜色
RECT& DrawRect) //颜色渐变区域
****************************************************************************/
//设置渐变参数,GRADLEVEL越小,颜色渐变越细腻,过度效果越好,但速度比较慢
#define GRADLEVEL 1
void DrawGradientH( HDC hdc, COLORREF co1, COLORREF co2, RECT& DrawRect )
{
int r = GetRValue( co1 );
int g = GetGValue( co1 );
int b = GetBValue( co1 );
int r2 = GetRValue( co2 );
int g2 = GetGValue( co2 );
int b2 = GetBValue( co2 );
//计算宽,高
int DrawRectWidth=DrawRect.right-DrawRect.left;
int DrawRectHeight=DrawRect.bottom-DrawRect.top;
if ( DrawRectHeight<=0)
return;
//初始化rect
RECT rect={0,0,GRADLEVEL,DrawRectHeight};
//准备GDI
HDC hMemDC=CreateCompatibleDC(hdc); //创建内存DC
HBITMAP hBitmap=::CreateCompatibleBitmap(hdc,DrawRectWidth,DrawRectHeight);//创建位图
::SelectObject(hMemDC,hBitmap); //把位图选进内存DC
HBRUSH hbr;
for(int i = DrawRectWidth; i > 0; i -= GRADLEVEL )
{
//创建刷子
hbr = CreateSolidBrush( RGB( r, g, b ) );
FillRect( hMemDC, &rect, hbr );
DeleteObject( hbr );
//改变小正方体的位置
rect.left += GRADLEVEL;
rect.right += GRADLEVEL;
//判断小正方体是否超界
if( rect.right > DrawRect.right )
rect.right = DrawRect.right;
//改变颜色
r += ( r2 - r + i / 2 ) / i * GRADLEVEL;
g += ( g2 - g + i / 2 ) / i * GRADLEVEL;
b += ( b2 - b + i / 2 ) / i * GRADLEVEL;
}
//内存DC映射到屏幕DC
BitBlt(hdc,DrawRect.left,DrawRect.top,DrawRectWidth,DrawRectHeight,hMemDC,0,0,SRCCOPY);
//删除
::DeleteDC(hMemDC) ;
::DeleteObject(hBitmap);
}
/***********************************************************************
获取图标函数 if failed, the return value is zero
************************************************************************/
HICON GetSmallIcon(const TCHAR* FilePath)
{
SHFILEINFO stFileInfo={0};
:: SHGetFileInfo(FilePath,0, &stFileInfo,sizeof(stFileInfo),SHGFI_ICON);
return stFileInfo.hIcon;
}
CItemList::~CItemList()
{
for(int i=0;i<item.size();i++)
DestroyIcon(item[i].hIcon);
}
#ifdef MOUSE_MOVE_ITEM
int CItemList::ItemToMoveIndex=SELECT_NONE;
#endif
CItemList::CItemList()
{
ActiveItem=SELECT_NONE;
#ifdef MOUSE_MOVE_ITEM
IsInFilstPart=false;
IsInSecondPart=false;
#endif
}
void CItemList::Paint(HDC hdc)
{
ASSERT(hdc);
//Draw Background
RECT rect={item.front().rect.left,item.front().rect.top,item.back().rect.right,item.back().rect.bottom};
// FillRect(hdc,&rect,(HBRUSH)GetStockObject(WHITE_BRUSH));
DrawGradientH(hdc,RGB(49,68,212),RGB(147,255,255),rect);
//Draw All Item
for (int i=0;i<item.size();i++) //draw every Item
{
if (item[i].bSelected==true)
{
DrawItem(hdc,item[i],SELECT_BRUSH_COLOR);
}
else
{
DrawItemForeground(hdc,item[i]);
// DrawItem(hdc,item[i],BACKGROUND_BRUSH_COLOR);
}
}
}
void CItemList::DrawItemForeground(HDC hdc,ITEM& item)
{
//Draw Icon,垂直方向能自动调整
::DrawIconEx(hdc,item.rect.left+ICON_OFFSET,(item.rect.top+item.rect.bottom-ICON_CY)/2,
item.hIcon,ICON_CX,ICON_CY,NULL,NULL,DI_NORMAL);
//Draw ItemTitle
int nMode=::SetBkMode(hdc,TRANSPARENT);
RECT rect={item.rect.left+TITLE_OFFSET,item.rect.top,item.rect.right,item.rect.bottom};
::DrawText (hdc,item.title, -1,&rect,DT_SINGLELINE | DT_LEFT | DT_VCENTER |DT_VCENTER|DT_END_ELLIPSIS ) ;
::SetBkMode(hdc,nMode);
}
void CItemList::DrawItem(HDC hdc,ITEM& item,COLORREF BrushColor)
{
//画背景长方体
HBRUSH brush=CreateSolidBrush(BrushColor);
HBRUSH OldBrush=(HBRUSH)::SelectObject (hdc,brush) ;
::Rectangle(hdc,item.rect.left,item.rect.top,item.rect.right,item.rect.bottom);
::SelectObject (hdc,OldBrush) ;
::DeleteObject(brush);
//画前景
DrawItemForeground(hdc,item);
}
int CItemList::LButtonDown()
{
if (ActiveItem!=SELECT_NONE)
{
HDC hdc=::GetDC(hParentWnd);
DrawItem(hdc,item[ActiveItem],LBUTTON_DOWN_BRUSH_COLOR);
::ReleaseDC(hParentWnd,hdc);
}
return ActiveItem;
}
int CItemList::LButtonUp()
{
if (ActiveItem!=SELECT_NONE)
SetSelectItem(ActiveItem);
return ActiveItem;
}
int CItemList::RButtonDown()
{
if (ActiveItem!=SELECT_NONE)
{
HDC hdc=::GetDC(hParentWnd);
DrawItem(hdc,item[ActiveItem],RBUTTON_DOWN_BRUSH_COLOR);
::ReleaseDC(hParentWnd,hdc);
}
return ActiveItem;
}
int CItemList::RButtonUp()
{
if (ActiveItem!=SELECT_NONE)
SetSelectItem(ActiveItem);
return ActiveItem;
}
int CItemList::MButtonDown()
{
return ActiveItem;
}
int CItemList::MButtonUp()
{
return ActiveItem;
}
void CItemList::MouseMove(UINT nFlags,POINT point)
{
//begin find item
int i;
for(i=0; i<item.size() && !::PtInRect(&item[i].rect,point) ; i++) ; //注意这行的分号
//if found item(即鼠标在List里面)
if(i!=item.size())
{
#ifdef MOUSE_MOVE_ITEM
if (nFlags&MK_MBUTTON && ItemToMoveIndex!=SELECT_NONE)
{
//改变鼠标形状
::SetCursor(LoadCursor(AfxGetInstanceHandle(),MAKEINTRESOURCE(IDC_MOVEITEMCUR)));
//在最后一项移动
if (i==item.size()-1)
{
//是否第一次移入此Item
if (ActiveItem!=i)
{
//假如需要,更新原来的ItemRect
if (ActiveItem!=SELECT_NONE)
::InvalidateRect(hParentWnd,&item[ActiveItem].rect,false);
//保存当前焦点的按钮索引
ActiveItem=i;
}
if (VERTICAL_LIST ==ItemListStyle)
{
//在最后一个item的上半部分上移动
if ( point.y<(item[ActiveItem].rect.top+item[ActiveItem].rect.bottom)/2 )
{
//第一次进入最后一个item的前半部分
if (IsInFilstPart==false)
{
trace("第一次进入最后一个item的前半部分");
IsInFilstPart=true;
IsInSecondPart=false;
::InvalidateRect(hParentWnd,&item[ActiveItem].rect,false);
::UpdateWindow(hParentWnd);
DrawLine(hParentWnd,item[ActiveItem].rect.left+2,item[ActiveItem].rect.top+2,
item[ActiveItem].rect.right-2,item[ActiveItem].rect.top+2);
::PostMessage(hParentWnd,WM_FROM_ITEMLIST,ITEM_CHANGE,ActiveItem);
}
}
//在最后一个item的后半部分上移动
else
{
//第一次进入最后一个item的后半部分
if (IsInSecondPart==false)
{
trace("第一次进入最后一个item的后半部分");
IsInSecondPart=true;
IsInFilstPart=false;
::InvalidateRect(hParentWnd,&item[ActiveItem].rect,false);
::UpdateWindow(hParentWnd);
DrawLine(hParentWnd,item[ActiveItem].rect.left+2,item[ActiveItem].rect.bottom-2,
item[ActiveItem].rect.right-2,item[ActiveItem].rect.bottom-2);
::PostMessage(hParentWnd,WM_FROM_ITEMLIST,ITEM_CHANGE,ActiveItem+1);
}
}
}
//水平List
else
{
//在最后一个item的上半部分上移动
if ( point.x<(item[ActiveItem].rect.left+item[ActiveItem].rect.right)/2 )
{
//第一次进入最后一个item的前半部分
if (IsInFilstPart==false)
{
trace("第一次进入最后一个item的前半部分");
IsInFilstPart=true;
IsInSecondPart=false;
::InvalidateRect(hParentWnd,&item[ActiveItem].rect,false);
::UpdateWindow(hParentWnd);
DrawLine(hParentWnd,item[ActiveItem].rect.left+2,item[ActiveItem].rect.top+2,
item[ActiveItem].rect.left+2,item[ActiveItem].rect.bottom-2);
::PostMessage(hParentWnd,WM_FROM_ITEMLIST,ITEM_CHANGE,ActiveItem);
}
}
//在最后一个item的后半部分上移动
else
{
//第一次进入最后一个item的后半部分
if (IsInSecondPart==false)
{
trace("第一次进入最后一个item的后半部分");
IsInSecondPart
评论0
最新资源