一个图片加载类

简介:
#include <vector>

class CImageLoader  
{
  protected:
    /// Full path were the images and icons are stored
    CString m_sPath;
    /// List with all the Bmp found in m_sPath
    std::vector<CString> m_BmpNameList;
    /// List with all the ico found in m_sPath
    std::vector<CString> m_IcoNameList;

    /**
     * If fills the a_fileList with all the files with extension a_sExtension
     * found int the path m_sPath
     * @param a_fileList: std::vector<CString>& where the file names will be stored
     * @param a_sExtension: CString with the extension of the files to found
     * @return bool true if all ok
     */
    bool GetFiles(std::vector<CString>& a_fileList, CString a_sExtension);

  public:
    CImageLoader();
    ~CImageLoader();

    /** 
     * Load a bitmap into the static
     * @param a_Static: CStatic* in which the bitmap will be loaded
     * @param a_iIndex: int with the index of bitmap to load. If it is -1
     *  then it will take one bmp randomly. Default value: -1
     * @return bool indicating if it load the bitmap correctly.
     */
    bool SetBitmap(CStatic* a_Static, int a_iIndex = -1);

    /**
     * Change the actual cursor for a new ico that is in the
     * @param a_iIndex: int position in the array. If it is -1 then it will take one
     * bmp randomly. Default value -1.
     */
    void NewCursor(int a_iIndex = -1);

    /**
     * It returns a name of a Bmp.
     * @param a_iIndex: int position of the BMP name to return in the array. 
     *  If it is -1 then it will return a name randomly. Default value -1.
     * @return CString with the BMP name
     */
    CString GetBmpName(int a_iIndex = -1);

    /**
     * It returns a name of a Ico.
     * @param a_iIndex: int position of the ICO name to return in the array. 
     *  If it is -1 then it will return a name randomly. Default value -1.
     * @return CString with the ICO name
     */
    CString GetIcoName(int a_iIndex = -1);

    /**
     * It return the number of BMP names stored in the attribute m_BmpNameListt
     * @return int with the lengtg of m_BmpNameList
     */
    int GetBmpListSize();

    /**
     * It return the number of BMP names stored in the attribute m_IcoNameList
     * @return int with the lengtg of m_IcoNameList
     */
    int GetIcoListSize();

    // Accesors
    /// Write Accesor for m_sPath attribute
    void SetPath(CString a_sPath);
    /// Read Accesor for m_sPath attribute
    CString GetPath();
};

复制代码

复制代码
CImageLoader::CImageLoader()
{
}

CImageLoader::~CImageLoader()
{

}

bool CImageLoader::GetFiles(std::vector<CString>& a_fileList, CString a_sExtension)
{
    bool btmpOk;

    try
    {
        a_fileList.clear();
        WIN32_FIND_DATA tmpFileFindData;

        // Found for Path\*.Extension
        CString stmpFiles = m_sPath + CString("*.") + a_sExtension;

        HANDLE tmpHdFind = FindFirstFile(stmpFiles, &tmpFileFindData);
        bool btmpFi = (tmpHdFind == INVALID_HANDLE_VALUE);
        while (!btmpFi)
        {
            // If we found a file and it is not a directory, we put it into the vector.
            CString stmpFileName = 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;
    }

    return btmpOk;
}

bool CImageLoader::SetBitmap(CStatic* a_Static, int a_iIndex /* = -1 */)
{
    bool btmpOk;

    try
    {
        CString stmpBMP = GetBmpName(a_iIndex);

        HANDLE tmphBMP1 = LoadImage(NULL, stmpBMP, IMAGE_BITMAP, 0, 0, LR_LOADFROMFILE | LR_DEFAULTSIZE);
        HANDLE tmphBMP2 = a_Static->SetBitmap((HBITMAP)tmphBMP1);
        if (tmphBMP2 != NULL)
            DeleteObject(tmphBMP2), tmphBMP2 = NULL;
        a_Static->CenterWindow();
    }
    catch()
    {
        btmpOk = false;
    }

    return btmpOk;
}

void CImageLoader::NewCursor(int a_iIndex /* = -1 */)
{
    CString stmpICO = GetIcoName(a_iIndex);

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


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

    return stmpRes;
}

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

    return stmpRes;
}


int CImageLoader::GetBmpListSize()
{
    return m_BmpNameList.size();
}


int CImageLoader::GetIcoListSize()
{
    return m_IcoNameList.size();
}

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

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

CString CImageLoader::GetPath() 
    return m_sPath; 
}


复制代码




本文转自Phinecos(洞庭散人)博客园博客,原文链接:http://www.cnblogs.com/phinecos/archive/2008/07/06/1237049.html,如需转载请自行联系原作者
目录
相关文章
|
8月前
|
前端开发 算法
一个有趣的图片加载效果
一个有趣的图片加载效果
68 0
|
JavaScript UED
Day24 - 图片懒加载的原理
Day24 - 图片懒加载的原理
109 0
|
JavaScript API
原生JS实现最简单的图片懒加载
原生JS实现最简单的图片懒加载
258 0
|
缓存 JavaScript 前端开发
图片预加载与懒加载
前面做了个招聘页面,里面有大量的图片需要加载。 一开始都是全部写在页面中,在测试环境还看不出很慢,一放到正式环境就不对了。
图片预加载与懒加载
|
JavaScript UED 缓存
图片预加载和懒加载的实现方法
图片预加载,即图片提前加载,可以保证图片快速、无缝的发布,用户需要查看时可直接从本地缓存中渲染,适用于图片占据很大比例的网站。 一、懒加载 将图片src赋值为一张默认的图片,当用户滚动到可视区域的时候,再去加载真正的图片; 代码实现: html代码 js代码 vue中实现懒加载 对于图片过多的页面,为了加速页面加载速度,所以很多时候我们需要将页面内未出现在可视区域内的图片先不做加载, 等到滚动到可视区域后再去加载。
2123 0
|
移动开发 JavaScript 前端开发
Picasso图片加载器简单封装
Picasso是一款当下好用并且流行的图片加载器,在这里分享一下我的简单封装,小巧玲珑、方便使用。废话不多讲直接上代码。 首先Picasso在github上面的显示地址:https://github.com/square/picasso 然后根据当下的依赖版本,添加依赖在你的Module的build.gradle内部。
896 0