查看完整版本: 一个简单封装IPicture的类

luoshayu 2006-4-22 17:28

一个简单封装IPicture的类

//picture.h

#ifndef        __PICTURE_CLASS_H__
#define        __PICTURE_CLASS_H__

class        CPicture
{
public:
        CPicture();
        ~CPicture();

        HBITMAP        GetBmp()                                                                                                ;
        int                GetWidth()                                                                                                ;
        int                GetHeight()                                                                                                ;
        bool        Load(LPCTSTR pFile)                                                                                ;
        bool        Load(LPVOID pMem,UINT uSize)                                                        ;
        bool        Load(HINSTANCE hInst,UINT uID,LPCTSTR lpType)                        ;

private:
        int                                        m_iWidth        ;
        int                                        m_iHeight        ;
        OLE_XSIZE_HIMETRIC        m_hmWidth        ;
        OLE_XSIZE_HIMETRIC        m_hmHeight        ;
        IPicture*                        m_pIPicture        ;
};

#endif//__PICTURE_CLASS_H__

//picture.cpp

#include "resource.h"
#include "stdafx.h"
#include "olectl.h"
#include "picture.h"
#include "fileclass.h"

CPicture::CPicture()
{
        m_iHeight        = 0;
        m_iWidth        = 0;
        m_pIPicture = NULL;
}

CPicture::~CPicture()
{
        if(m_pIPicture)
                m_pIPicture->Release(),m_pIPicture = NULL;
}

HBITMAP        CPicture::GetBmp()
{
        HDC                hDC                = NULL;
        HBITMAP        hRet        = NULL;
        int                width        = GetWidth();
        int                height        = GetHeight();
        HDC                hMemDC        = NULL;
        HBITMAP        hOldBmp        = NULL;
        HWND        hDesktop= GetDesktopWindow();

        hDC                = GetDC(hDesktop);

        hMemDC        = CreateCompatibleDC(hDC);

        hRet        = CreateCompatibleBitmap(hDC,width,height);
       
        ReleaseDC(hDesktop,hDC);
        hDC = NULL;

       
        hOldBmp        = (HBITMAP)SelectObject(hMemDC,hRet);

        m_pIPicture->Render(hMemDC,0,0,width,height,0,m_hmHeight,m_hmWidth,-m_hmHeight,NULL);

        (HBITMAP)SelectObject(hMemDC,hOldBmp);

        DeleteDC(hMemDC);
        hMemDC = NULL;

        return hRet;
}

int                CPicture::GetWidth()
{
        if(!m_pIPicture)
                return 0;
        return m_iWidth;
}

int                CPicture::GetHeight()
{
        if(!m_pIPicture)
                return 0;
        return m_iHeight;
}

bool        CPicture::Load(LPCTSTR pFile)
{
        bool                bRet        = false;
        DWORD                dwLen        = 0;
        LPVOID                pData        = NULL;
        HGLOBAL                hGlobal        = NULL;
        CFileClass        fileCls        ;


        if(!pFile)
                return false;

        fileCls.Open(pFile,CFileClass::modeRead);
        if(!fileCls.IsOpen())
                return false;

        dwLen = fileCls.GetLength();

        hGlobal = GlobalAlloc(GMEM_MOVEABLE, dwLen);
       
        if(hGlobal == NULL)
                return false;
        if((pData = GlobalLock(hGlobal))==NULL)//锁定分配内存块
        {
                GlobalFree(hGlobal);
                hGlobal = NULL;
                return false;
        }

        fileCls.Read(pData,dwLen);//把文件读入内存缓冲区

        bRet = Load(pData,dwLen);

        GlobalUnlock(hGlobal);

        GlobalFree(hGlobal);
        hGlobal = NULL;
        pData        = NULL;

        return bRet;
}

bool        CPicture::Load(LPVOID pMem,UINT uSize)
{
        HRESULT                                hr                ;
        HDC                                        hDC                = NULL;
        bool                                bRet        = false;
        IStream*                        pStream        = NULL;
        HWND                                hDesktop= NULL;

        if(m_pIPicture)
                m_pIPicture->Release();
        hr = CreateStreamOnHGlobal(pMem,FALSE,&pStream);
        if(SUCCEEDED(hr))
        {
                hr = OleLoadPicture(pStream,uSize,TRUE,IID_IPicture,(void**)&m_pIPicture);
                if(SUCCEEDED(hr))
                        bRet = true;
                pStream->Release();
                if(!bRet)
                        return false;

                m_pIPicture->get_Width(&m_hmWidth);
                m_iWidth = m_hmWidth;
               
                m_pIPicture->get_Height(&m_hmHeight);
                m_iHeight = m_hmHeight;

                hDesktop = GetDesktopWindow();
                hDC                 = GetDC(hDesktop);
                m_iWidth = MulDiv(m_iWidth, GetDeviceCaps(hDC, LOGPIXELSX),HIMETRIC_PER_INCH);
                m_iHeight= MulDiv(m_iHeight,GetDeviceCaps(hDC, LOGPIXELSY),HIMETRIC_PER_INCH);
                ReleaseDC(hDesktop,hDC);
                hDC                 = NULL;

        }
        return bRet;
}

bool        CPicture::Load(HINSTANCE hInst ,UINT uID,LPCTSTR lpType)
{
        bool                bRet        = false;
        DWORD                dwLen        = 0;
        LPVOID                pData        = NULL;
        LPVOID                lpRes        = GetResAddr(hInst,uID,lpType,&dwLen);
        HGLOBAL                hGlobal        = NULL;

        if(!lpRes)
                return false;
        hGlobal = GlobalAlloc(GMEM_MOVEABLE, dwLen);
        if((pData = GlobalLock(hGlobal))==NULL)//锁定分配内存块
        {
                GlobalFree(hGlobal);
                hGlobal = NULL;
                return false;
        }
        CopyMemory(pData,lpRes,dwLen);
        bRet        = Load(pData,dwLen);

        GlobalUnlock(hGlobal);

        GlobalFree(hGlobal);
        hGlobal = NULL;
        pData        = NULL;

        return bRet;
}
页: [1]
查看完整版本: 一个简单封装IPicture的类