IPicture

 1 #pragma once
 2 
 3 
 4 class CPicture
 5 {
 6 public:
 7     CPicture();
 8     virtual ~CPicture();
 9 
10 public:
11     BOOL LoadPictureFromRes( UINT nPicID, LPCTSTR lpszResType );
12     BOOL LoadPictureFromFile( LPCTSTR lpszFile );
13     BOOL Draw( HDC hDC, LPCRECT lprcDest, LPCRECT lprcSrc = NULL );
14     void Release();
15     inline operator bool() const;
16     BOOL GetSize( LPSIZE lpSize, HDC hDC );
17 
18 protected:
19     IPicture* m_pPic;
20     OLE_XSIZE_HIMETRIC m_nWidth, m_nHeight;
21 };
  1 #include "Picture.h"
  2 
  3 CPicture::CPicture() : m_pPic( NULL )
  4 {
  5 }
  6 
  7 CPicture::~CPicture()
  8 {
  9     if ( m_pPic != NULL )
 10     {
 11         m_pPic->Release();
 12     }
 13 }
 14 
 15 CPicture::operator bool() const
 16 {
 17     return m_pPic != NULL ? true : false;
 18 }
 19 
 20 void CPicture::Release()
 21 {
 22     if ( m_pPic != NULL )
 23     {
 24         m_pPic->Release();
 25         m_pPic = NULL;
 26     }
 27 }
 28 
 29 
 30 BOOL CPicture::LoadPictureFromRes( UINT nPicID, LPCTSTR lpszResType )
 31 {
 32     HINSTANCE hInst;
 33     HRSRC hRes;
 34     HGLOBAL hGlobal, hMem;
 35     LPVOID lpData, lpBuf;
 36     DWORD dwSize;
 37     HRESULT hr;
 38     IStream* pStm;
 39 
 40     if ( m_pPic )
 41     {
 42         m_pPic->Release();
 43         m_pPic = NULL;
 44     }
 45 
 46     hInst = ::GetModuleHandle( NULL );
 47 
 48     hRes = ::FindResource( hInst, MAKEINTRESOURCE( nPicID ), lpszResType );
 49     if ( hRes == NULL )
 50     {
 51         return NULL;
 52     }
 53 
 54     hGlobal = ::LoadResource( hInst, hRes );
 55     if ( hGlobal == NULL )
 56     {
 57         return NULL;
 58     }
 59 
 60     dwSize = ::SizeofResource( hInst, hRes );
 61 
 62     hMem = ::GlobalAlloc( GMEM_MOVEABLE, dwSize );
 63     if ( hMem == NULL )
 64     {
 65         ::FreeResource( hGlobal );
 66         return FALSE;
 67     }
 68 
 69     lpBuf = ::GlobalLock( hMem );
 70     lpData = ::LockResource( hGlobal );
 71     memcpy( lpBuf, lpData, dwSize );
 72     ::GlobalUnlock( hMem );
 73 
 74     //UnlockResource
 75     ::FreeResource( hGlobal );
 76 
 77     hr = ::CreateStreamOnHGlobal( hMem, TRUE, &pStm );
 78     if ( FAILED( hr ) )
 79     {
 80         ::GlobalFree( hMem );
 81         return FALSE;
 82     }
 83 
 84     hr = ::OleLoadPicture( pStm, dwSize, TRUE, IID_IPicture, ( LPVOID* )&m_pPic );
 85     if ( hr != S_OK )
 86     {
 87         //m_pPic = NULL;
 88         pStm->Release();
 89         return FALSE;
 90     }
 91 
 92     m_pPic->get_Width( &m_nWidth );
 93     m_pPic->get_Height( &m_nHeight );
 94 
 95     pStm->Release();
 96     return TRUE;
 97 }
 98 
 99 BOOL CPicture::LoadPictureFromFile( LPCTSTR lpszFile )
100 {
101     BOOL bRet;
102     HANDLE hFile;
103     DWORD dwSize, dwSizeHigh, dwReads;
104     HGLOBAL hMem;
105     LPVOID lpBuf;
106     HRESULT hr;
107     IStream* pStm;
108 
109     if ( m_pPic )
110     {
111         m_pPic->Release();
112         m_pPic = NULL;
113     }
114 
115     hFile = ::CreateFile( lpszFile, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL );
116     if ( hFile == INVALID_HANDLE_VALUE )
117     {
118         return FALSE;
119     }
120 
121     dwSize = ::GetFileSize( hFile, &dwSizeHigh );
122     if ( dwSize == INVALID_FILE_SIZE || dwSizeHigh != 0 )
123     {
124         ::CloseHandle( hFile );
125         return FALSE;
126     }
127 
128     hMem = ::GlobalAlloc( GMEM_MOVEABLE, dwSize );
129     if ( hMem == NULL )
130     {
131         ::CloseHandle( hFile );
132         return FALSE;
133     }
134 
135     lpBuf = ::GlobalLock( hMem );
136 
137     bRet = ::ReadFile( hFile, lpBuf, dwSize, &dwReads, NULL );
138     if ( !bRet || dwReads != dwReads )
139     {
140         ::CloseHandle( hFile );
141         ::GlobalUnlock( hMem );
142         ::GlobalFree( hMem );
143         return FALSE;
144     }
145 
146     ::CloseHandle( hFile );
147     ::GlobalUnlock( hMem );
148 
149     hr = ::CreateStreamOnHGlobal( hMem, TRUE, &pStm );
150     if ( FAILED( hr ) )
151     {
152         ::GlobalFree( hMem );
153         return FALSE;
154     }
155 
156     hr = ::OleLoadPicture( pStm, dwSize, TRUE, IID_IPicture, ( LPVOID* )&m_pPic );
157     if ( hr != S_OK )
158     {
159         //m_pPic = NULL;
160         pStm->Release();
161         return FALSE;
162     }
163 
164     m_pPic->get_Width( &m_nWidth );
165     m_pPic->get_Height( &m_nHeight );
166 
167     pStm->Release();
168     return TRUE;
169 }
170 
171 BOOL CPicture::GetSize( LPSIZE lpSize, HDC hDC )
172 {
173     int cxPerInch, cyPerInch;
174 
175     if ( lpSize == NULL || hDC == NULL )
176     {
177         return FALSE;
178     }
179 
180     cxPerInch = ::GetDeviceCaps( hDC, LOGPIXELSX );
181     cyPerInch = ::GetDeviceCaps( hDC, LOGPIXELSY );
182 
183     if ( cxPerInch == 0 || cyPerInch == 0 )
184     {
185         return FALSE;
186     }
187 
188     lpSize->cx = ::MulDiv( m_nWidth, cxPerInch, HIMETRIC_INCH );
189     lpSize->cy = ::MulDiv( m_nHeight, cyPerInch, HIMETRIC_INCH );
190 
191     return TRUE;
192 }
193 
194 
195 BOOL CPicture::Draw( HDC hDC, LPCRECT lprcDest, LPCRECT lprcSrc )
196 {
197     POINT ptOri;
198     SIZE szSize;
199     int cxPerInch, cyPerInch;
200     HRESULT hr;
201 
202     if ( hDC == NULL || lprcDest == NULL || m_pPic == NULL )
203     {
204         return FALSE;
205     }
206 
207     if ( lprcSrc == NULL )
208     {
209         hr = m_pPic->Render( hDC, lprcDest->left, lprcDest->top, \
210                              lprcDest->right - lprcDest->left, \
211                              lprcDest->bottom - lprcDest->top, \
212                              0, m_nHeight, m_nWidth, -m_nHeight, \
213                              NULL );
214     }
215     else
216     {
217         cxPerInch = ::GetDeviceCaps( hDC, LOGPIXELSX );
218         cyPerInch = ::GetDeviceCaps( hDC, LOGPIXELSY );
219 
220         if ( cxPerInch == 0 || cyPerInch == 0 )
221         {
222             return FALSE;
223         }
224 
225         ptOri.x = ::MulDiv( lprcSrc->left, HIMETRIC_INCH, cxPerInch );
226         ptOri.y = ::MulDiv( lprcSrc->top, HIMETRIC_INCH, cyPerInch );
227 
228         szSize.cx = ::MulDiv( lprcSrc->right - lprcSrc->left, HIMETRIC_INCH, cxPerInch );
229         szSize.cy = ::MulDiv( lprcSrc->bottom - lprcSrc->top, HIMETRIC_INCH, cyPerInch );
230 
231         hr = m_pPic->Render( hDC, lprcDest->left, lprcDest->top, \
232                              lprcDest->right - lprcDest->left, \
233                              lprcDest->bottom - lprcDest->top, \
234                              ptOri.x, m_nHeight - ptOri.y, szSize.cx, -szSize.cy, \
235                              NULL );
236     }
237 
238     return hr == S_OK ? TRUE : FALSE;
239 }

 

posted on 2012-11-27 22:48  郁郁思扬  阅读(423)  评论(0编辑  收藏  举报

导航