`
dato0123
  • 浏览: 915306 次
文章分类
社区版块
存档分类
最新评论

一个图片加载类

 
阅读更多
#include<vector>

classCImageLoader
{
protected:
/**////Fullpathweretheimagesandiconsarestored
CStringm_sPath;
/**////ListwithalltheBmpfoundinm_sPath
std::vector<CString>m_BmpNameList;
/**////Listwithalltheicofoundinm_sPath
std::vector<CString>m_IcoNameList;

/**//**
*Iffillsthea_fileListwithallthefileswithextensiona_sExtension
*foundintthepathm_sPath
*@parama_fileList:std::vector<CString>&wherethefilenameswillbestored
*@parama_sExtension:CStringwiththeextensionofthefilestofound
*@returnbooltrueifallok
*/

boolGetFiles(std::vector<CString>&a_fileList,CStringa_sExtension);

public:
CImageLoader();
~CImageLoader();

/**//**
*Loadabitmapintothestatic
*@parama_Static:CStatic*inwhichthebitmapwillbeloaded
*@parama_iIndex:intwiththeindexofbitmaptoload.Ifitis-1
*thenitwilltakeonebmprandomly.Defaultvalue:-1
*@returnboolindicatingifitloadthebitmapcorrectly.
*/

boolSetBitmap(CStatic*a_Static,inta_iIndex=-1);

/**//**
*Changetheactualcursorforanewicothatisinthe
*@parama_iIndex:intpositioninthearray.Ifitis-1thenitwilltakeone
*bmprandomly.Defaultvalue-1.
*/

voidNewCursor(inta_iIndex=-1);

/**//**
*ItreturnsanameofaBmp.
*@parama_iIndex:intpositionoftheBMPnametoreturninthearray.
*Ifitis-1thenitwillreturnanamerandomly.Defaultvalue-1.
*@returnCStringwiththeBMPname
*/

CStringGetBmpName(
inta_iIndex=-1);

/**//**
*ItreturnsanameofaIco.
*@parama_iIndex:intpositionoftheICOnametoreturninthearray.
*Ifitis-1thenitwillreturnanamerandomly.Defaultvalue-1.
*@returnCStringwiththeICOname
*/

CStringGetIcoName(
inta_iIndex=-1);

/**//**
*ItreturnthenumberofBMPnamesstoredintheattributem_BmpNameListt
*@returnintwiththelengtgofm_BmpNameList
*/

intGetBmpListSize();

/**//**
*ItreturnthenumberofBMPnamesstoredintheattributem_IcoNameList
*@returnintwiththelengtgofm_IcoNameList
*/

intGetIcoListSize();

//Accesors
/**////WriteAccesorform_sPathattribute
voidSetPath(CStringa_sPath);
/**////ReadAccesorform_sPathattribute
CStringGetPath();
}
;

CImageLoader::CImageLoader()
{
}


CImageLoader::
~CImageLoader()
{

}


boolCImageLoader::GetFiles(std::vector<CString>&a_fileList,CStringa_sExtension)
{
boolbtmpOk;

try
{
a_fileList.clear();
WIN32_FIND_DATAtmpFileFindData;

//FoundforPath/*.Extension
CStringstmpFiles=m_sPath+CString("*.")+a_sExtension;

HANDLEtmpHdFind
=FindFirstFile(stmpFiles,&tmpFileFindData);
boolbtmpFi=(tmpHdFind==INVALID_HANDLE_VALUE);
while(!btmpFi)
{
//Ifwefoundafileanditisnotadirectory,weputitintothevector.
CStringstmpFileName=tmpFileFindData.cFileName;
if(!((tmpFileFindData.dwFileAttributes&FILE_ATTRIBUTE_DIRECTORY)==FILE_ATTRIBUTE_DIRECTORY))
a_fileList.push_back(m_sPath
+stmpFileName);//保存找到的文件
btmpFi=(FindNextFile(tmpHdFind,&tmpFileFindData)==0);
}

btmpOk
=true;
}

catch()
{
btmpOk
=false;
}


returnbtmpOk;
}


boolCImageLoader::SetBitmap(CStatic*a_Static,inta_iIndex/**//*=-1*/)
{
boolbtmpOk;

try
{
CStringstmpBMP
=GetBmpName(a_iIndex);

HANDLEtmphBMP1
=LoadImage(NULL,stmpBMP,IMAGE_BITMAP,0,0,LR_LOADFROMFILE|LR_DEFAULTSIZE);
HANDLEtmphBMP2
=a_Static->SetBitmap((HBITMAP)tmphBMP1);
if(tmphBMP2!=NULL)
DeleteObject(tmphBMP2),tmphBMP2
=NULL;
a_Static
->CenterWindow();
}

catch()
{
btmpOk
=false;
}


returnbtmpOk;
}


voidCImageLoader::NewCursor(inta_iIndex/**//*=-1*/)
{
CStringstmpICO
=GetIcoName(a_iIndex);

HANDLEtmphICO
=LoadImage(NULL,stmpICO,IMAGE_CURSOR,0,0,LR_LOADFROMFILE|LR_DEFAULTSIZE);
if(tmphICO!=NULL)
{
DestroyCursor(GetCursor());
SetCursor((HICON)tmphICO);
}

}



CStringCImageLoader::GetBmpName(
inta_iIndex/**//*=-1*/)
{
CStringstmpRes
="";
if(m_BmpNameList.size()>0)
{
intitmpIndex=a_iIndex;
if((itmpIndex<0)||(itmpIndex>=m_BmpNameList.size()))
itmpIndex
=rand()%(m_BmpNameList.size());//随机
stmpRes=m_BmpNameList[itmpIndex];
}


returnstmpRes;
}


CStringCImageLoader::GetIcoName(
inta_iIndex/**//*=-1*/)
{
CStringstmpRes
="";
if(m_IcoNameList.size()>0)
{
intitmpIndex=a_iIndex;
if((itmpIndex<0)||(itmpIndex>=m_IcoNameList.size()))
itmpIndex
=rand()%(m_IcoNameList.size());
stmpRes
=m_IcoNameList[itmpIndex];
}


returnstmpRes;
}



intCImageLoader::GetBmpListSize()
{
returnm_BmpNameList.size();
}



intCImageLoader::GetIcoListSize()
{
returnm_IcoNameList.size();
}


//ACCESSORS
voidCImageLoader::SetPath(CStringa_sPath)
{
m_sPath
=a_sPath;
//确保最后以'/'结束
if(m_sPath[m_sPath.GetLength()-1]!='//')
m_sPath
+="//";

GetFiles(m_BmpNameList,
"bmp");
GetFiles(m_IcoNameList,
"ico");
}


CStringCImageLoader::GetPath()
{
returnm_sPath;
}



分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics