www.pudn.com > ToolBar_ATL.rar > Picture.cpp


//////////////////////////////////////////////////////////////// 
// MSDN Magazine — October 2001 
// 如果代码可行,就是Paul DiLascia写的。如若不行,那我就不知道是谁写的了。// 在Windows 98中用Visual C++ 6.0编译通过,大概在Windows 2000中也可以吧// 在你的编辑器中设置tabsize = 3 
// 
#include "StdAfx.h" 
#include "Picture.h" 
 
#ifdef _DEBUG 
#define new DEBUG_NEW 
#undef THIS_FILE 
static char THIS_FILE[] = __FILE__; 
#endif 
 
//////////////////////////////////////////////////////////////// 
// CPicture implementation 
// 
 
CPicture::CPicture() 
{ 
} 
 
CPicture::~CPicture() 
{ 
} 
 
////////////////// 
// 从资源中加载。寻找"IMAGE" 类型。 
// 
BOOL CPicture::Load(UINT nIDRes) 
{ 
   // 在资源文件中发现资源 
   HINSTANCE hInst = AfxGetResourceHandle(); 
   HRSRC hRsrc = ::FindResource(hInst, 
      MAKEINTRESOURCE(nIDRes), 
      "IMAGE"); // type 
   if (!hRsrc) 
      return FALSE; 
 
   // 将资源加载进存储器 
   DWORD len = SizeofResource(hInst, hRsrc); 
   BYTE* lpRsrc = (BYTE*)LoadResource(hInst, hRsrc); 
   if (!lpRsrc) 
      return FALSE; 
 
   // 创建存储文件并加载它 
   CMemFile file(lpRsrc, len); 
   BOOL bRet = Load(file); 
   FreeResource(hRsrc); 
   return bRet; 
} 
 
////////////////// 
// 从路径名中加载 
// 
BOOL CPicture::Load(LPCTSTR pszPathName) 
{ 
   CFile file; 
   if (!file.Open(pszPathName, CFile::modeRead|CFile::shareDenyWrite)) 
      return FALSE; 
   BOOL bRet = Load(file); 
   file.Close(); 
   return bRet; 
} 
 
////////////////// 
// 从CFile中加载 
// 
BOOL CPicture::Load(CFile& file) 
{ 
   CArchive ar(&file, CArchive::load | CArchive::bNoFlushOnDelete); 
   return Load(ar); 
} 
 
////////////////// 
// Load from archive—create stream and load from stream. 
// 从创建文档的流中和从流中加载 
//  
BOOL CPicture::Load(CArchive& ar) 
{ 
   CArchiveStream arcstream(&ar); 
   return Load((IStream*)&arcstream); 
} 
 
////////////////// 
// 从流中加载(IStream)。 调用OleLoadPicture做真正实现它的工作。 
// 
BOOL CPicture::Load(IStream* pstm) 
{ 
   Free(); 
   HRESULT hr = OleLoadPicture(pstm, 0, FALSE, 
      IID_IPicture, (void**)&m_spIPicture); 
   ASSERT(SUCCEEDED(hr) && m_spIPicture);  
   return TRUE; 
} 
 
////////////////// 
// Render to device context. Covert to HIMETRIC for IPicture. 
// 转换设备环境。为IPicture而将其转换为HIMETRIC。 
// 
BOOL CPicture::Render(CDC* pDC, CRect rc, LPCRECT prcMFBounds) const 
{ 
   ASSERT(pDC); 
 
   if (rc.IsRectNull()) { 
      CSize sz = GetImageSize(pDC); 
      rc.right = sz.cx; 
      rc.bottom = sz.cy; 
   } 
   long hmWidth,hmHeight; // HIMETRIC单位 
   GetHIMETRICSize(hmWidth, hmHeight); 
   m_spIPicture->Render(*pDC, rc.left, rc.top, rc.Width(), rc.Height(), 
      0, hmHeight, hmWidth, -hmHeight, prcMFBounds); 
 
   return TRUE; 
} 
 
////////////////// 
// 获得象素形式的图像尺寸。从HIMETRIC转换为设备坐标。 
// 
CSize CPicture::GetImageSize(CDC* pDC) const 
{ 
   if (!m_spIPicture) 
      return CSize(0,0); 
    
   LONG hmWidth, hmHeight; // HIMETRIC单位 
   m_spIPicture->get_Width(&hmWidth); 
   m_spIPicture->get_Height(&hmHeight); 
   CSize sz(hmWidth,hmHeight); 
   if (pDC==NULL) { 
      CWindowDC dc(NULL); 
      dc.HIMETRICtoDP(&sz); // 转换为象素 
   } else { 
      pDC->HIMETRICtoDP(&sz); 
   } 
   return sz; 
}