📄 picture.cpp
字号:
////////////////////////////////////////////////////////////////
// MSDN Magazine -- October 2001
// If this code works, it was written by Paul DiLascia.
// If not, I don't know who wrote it.
// Compiles with Visual C++ 6.0 for Windows 98 and probably Windows 2000 too.
// Set tabsize = 3 in your editor.
//
#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()
{
if(this->m_spIPicture) Free();
}
//////////////////
// Load from resource. Looks for "IMAGE" type.
//
BOOL CPicture::Load(UINT nIDRes,LPCTSTR resType)
{
// find resource in resource file
return Load(MAKEINTRESOURCE(nIDRes),resType);
}
//////////////////
// Load from path name.
//
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;
}
//////////////////
// Load from CFile
//
BOOL CPicture::Load(CFile& file)
{
DWORD dwSize=file.GetLength();
HGLOBAL hglobal=GlobalAlloc(GMEM_MOVEABLE | GMEM_NODISCARD,dwSize);
BYTE *buffer=reinterpret_cast<BYTE*>(GlobalLock(hglobal));;
file.SeekToBegin();
file.ReadHuge(buffer,dwSize);
BOOL hr=Load(hglobal,dwSize);
GlobalUnlock(hglobal);
GlobalFree(hglobal);
return hr;
}
///////////////////
// load from buffer
///////////////////
BOOL CPicture::Load(HGLOBAL hGlobal, DWORD buflen)
{
IStream *istm=NULL;
if (CreateStreamOnHGlobal(hGlobal,FALSE,&istm) != S_OK)
return FALSE;
BOOL bRet = Load(istm);
istm->Release();
return bRet;
}
//////////////////
// Load from stream (IStream). This is the one that really does it: call
// OleLoadPicture to do the work.
//
BOOL CPicture::Load(IStream* pstm)
{
Free();
HRESULT hr = OleLoadPicture(pstm, 0, FALSE,
IID_IPicture, (void**)&m_spIPicture);
return (SUCCEEDED(hr) && m_spIPicture);
}
//////////////////
// Render to device context. Covert to HIMETRIC for IPicture.
//
BOOL CPicture::Render(CDC* pDC, CRect rc,int zoomtype) const
{
ASSERT(pDC);
CSize sz = GetImageSize(pDC);
if (rc.IsRectNull()) {
rc.right = sz.cx;
rc.bottom = sz.cy;
}
long hmWidth,hmHeight; // HIMETRIC units
GetHIMETRICSize(hmWidth, hmHeight);
int wid,hei;
CRect rect=rc;
switch(zoomtype)
{
case 0:
rect.left=rc.left+(rc.Width()-sz.cx)/2;
rect.right=rc.right-(rc.Width()-sz.cx)/2;
rect.top=rc.top+(rc.Height()-sz.cy)/2;
rect.bottom =rc.bottom -(rc.Height()-sz.cy)/2;
break;
case 1:
wid=rc.Width(),hei=hmHeight*wid/hmWidth;
if(hei>rc.Height()) hei=rc.Height(),wid=hmWidth*hei/hmHeight;
if(wid==rc.Width()) rect.SetRect(rc.left,rc.top+(rc.Height()-hei)/2,rc.right,rc.bottom-(rc.Height()-hei)/2);
else rect.SetRect(rc.left+(rc.Width()-wid)/2,rc.top,rc.right-(rc.Width()-wid)/2,rc.bottom);
break;
default:
break;
}
m_spIPicture->Render(*pDC, rect.left, rect.top, rect.Width(), rect.Height(),
0, hmHeight, hmWidth, -hmHeight, NULL);
return TRUE;
}
//////////////////
// Get image size in pixels. Converts from HIMETRIC to device coords.
//
CSize CPicture::GetImageSize(CDC* pDC) const
{
if (!m_spIPicture)
return CSize(0,0);
LONG hmWidth, hmHeight; // HIMETRIC units
m_spIPicture->get_Width(&hmWidth);
m_spIPicture->get_Height(&hmHeight);
CSize sz(hmWidth,hmHeight);
if (pDC==NULL) {
CWindowDC dc(NULL);
dc.HIMETRICtoDP(&sz); // convert to pixels
} else {
pDC->HIMETRICtoDP(&sz);
}
return sz;
}
BOOL CPicture::Load(LPCTSTR resstr, LPCTSTR restype)
{
HINSTANCE hInst = AfxGetResourceHandle();
HRSRC hRsrc = ::FindResource(hInst,
resstr,
restype); // type
if (!hRsrc)
return FALSE;
// load resource into memory
DWORD len = SizeofResource(hInst, hRsrc);
BYTE* lpRsrc = (BYTE*)LoadResource(hInst, hRsrc);
if (!lpRsrc)
return FALSE;
// create memory file and load it
CMemFile file(lpRsrc, len);
BOOL bRet = Load(file);
FreeResource(hRsrc);
GlobalFree(lpRsrc);
return bRet;
}
BOOL CPicture::Render(CDC *pDC,
int x, int y,int nWidth, int nHeight,
int xSrc, int ySrc, int nSrcWidth, int nSrcHeight)
{
CSize lefttop,rightbottom;
lefttop.cx=xSrc,lefttop.cy=ySrc;
rightbottom.cx=nSrcWidth,rightbottom.cy=nSrcHeight;
pDC->DPtoHIMETRIC(&lefttop);
pDC->DPtoHIMETRIC(&rightbottom);
LONG hmWidth, hmHeight; // HIMETRIC units
m_spIPicture->get_Width(&hmWidth);
m_spIPicture->get_Height(&hmHeight);
m_spIPicture->Render(*pDC, x,y, nWidth,nHeight,lefttop.cx, hmHeight-lefttop.cy, rightbottom.cx, -rightbottom.cy, NULL);
return TRUE;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -