www.pudn.com > TestIconPicker.zip > IconPicker.cpp, change:2004-04-01,size:3109b


// IconPicker.cpp : implementation file 
// 
 
#include "stdafx.h" 
#include "TestIconPicker.h" 
#include "IconPicker.h" 
 
#ifdef _DEBUG 
#define new DEBUG_NEW 
#undef THIS_FILE 
static char THIS_FILE[] = __FILE__; 
#endif 
 
///////////////////////////////////////////////////////////////////////////// 
// CIconPicker 
CIconPicker::CIconPicker() 
{ 
	m_iCurIndex=0; 
	m_pIconContainer=NULL; 
	m_bState=FALSE; 
} 
 
CIconPicker::~CIconPicker() 
{ 
	CBitmap* pBitmap; 
	for(int i=0;i<m_BitmapArray.GetSize();i++) 
	{ 
		pBitmap=m_BitmapArray.GetAt(i); 
		pBitmap->DeleteObject(); 
	} 
} 
 
 
BEGIN_MESSAGE_MAP(CIconPicker, CButton) 
	//{{AFX_MSG_MAP(CIconPicker) 
	ON_WM_LBUTTONDOWN() 
	ON_MESSAGE(WM_USER+101,OnSelectICON) 
	//}}AFX_MSG_MAP 
END_MESSAGE_MAP() 
 
///////////////////////////////////////////////////////////////////////////// 
// CIconPicker message handlers 
 
////左键按下的时候在本按钮下方创建一个CIconContainer 
////在CIconContainer中显示所有的小位图 
void CIconPicker::OnLButtonDown(UINT nFlags, CPoint point)  
{ 
	if(m_bState) return ; 
	m_bState=TRUE; 
	this->SetState(TRUE); 
	 
 
	RECT rect; 
	this->GetWindowRect(&rect); 
	 
	POINT pt; 
	pt.x=rect.left;pt.y=rect.bottom; 
 
	m_pIconContainer=new CIconContainer; 
	if(m_pIconContainer->Create(pt,this,&m_BitmapArray)) 
	{ 
		m_pIconContainer->ShowWindow(SW_SHOW); 
		m_pIconContainer->UpdateWindow(); 
		m_pIconContainer->SetFocus(); 
	} 
} 
 
 
///收到来自CIconContainer的消息,返回用户选中的图片标号 
///-3表示用户没有选中图片 
void CIconPicker::OnSelectICON(int index) 
{ 
	 
	if(index>=0 && index<m_BitmapArray.GetSize()) 
	{ 
		///如果用户选择了图片,则发送WM_COMMAND给父窗口,表示本按钮被单击 
	//	this->SetBitmap(m_BitmapArray.GetAt(index)); 
		//this->SetMouseOnBitmap(m_BitmapArray.GetAt(index)); 
		m_iCurIndex=index; 
 
		::SendMessage(GetParent()->m_hWnd,WM_COMMAND,GetDlgCtrlID(),0); 
	} 
	///否则的话,不发送WM_COMMAND消息 
	this->SetState(FALSE); 
	this->Invalidate(); 
	m_bState=FALSE; 
 
} 
 
///以下四个函数设置本按钮的前景图片 
void CIconPicker::AddBitmap(UINT BITMAP_ID) 
{ 
	CBitmap *pBitmap; 
	pBitmap=new CBitmap; 
	pBitmap->LoadBitmap(BITMAP_ID); 
	m_BitmapArray.Add(pBitmap); 
} 
 
void CIconPicker::AddBitmap(HBITMAP hBitmap) 
{ 
	if(hBitmap==NULL) 
		return; 
	CBitmap *pBitmap; 
	pBitmap=new CBitmap; 
	pBitmap->Attach(hBitmap); 
	m_BitmapArray.Add(pBitmap); 
} 
 
void CIconPicker::AddBitmap(CBitmap* pBitmap) 
{ 
	ASSERT(pBitmap); 
	m_BitmapArray.Add(pBitmap); 
} 
 
void CIconPicker::AddBitmap(CString strFileName) 
{ 
	if(!strFileName.IsEmpty()) 
	{ 
		HBITMAP hBitmap; 
		hBitmap=HBITMAP(::LoadImage(AfxGetInstanceHandle(),strFileName, 
			IMAGE_BITMAP,0,0,LR_LOADFROMFILE)); 
		this->AddBitmap(hBitmap); 
	} 
} 
 
////根据标号返回图片列表中的图片 
CBitmap* CIconPicker::GetBitmapAt(int index) 
{ 
	if(index<0 && index>=m_BitmapArray.GetSize()) 
		return NULL; 
	return m_BitmapArray.GetAt(index); 
} 
 
///返回图片列表中图片的个数 
int CIconPicker::GetBitmapCount() 
{ 
	return m_BitmapArray.GetSize(); 
} 
 
///返回当前图片的标号 
int CIconPicker::GetCurrentBitmapIndex() 
{ 
	return m_iCurIndex; 
}