📄 picture1.cpp
字号:
// Picture1.cpp: implementation of the CPicture class.
//
//////////////////////////////////////////////////////////////////////
#include "stdafx.h"
#include "test1.h"
#include "Picture1.h"
#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[]=__FILE__;
#define new DEBUG_NEW
#endif
#define HIMETRIC_INCH 2540
#define ERROR_TITLE "CPicture Error" // Error Title (Related To This Class)...
//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////
CPicture::CPicture()
//=============================================================================
{
m_IPicture = NULL;
m_Height = 0;
m_Weight = 0;
m_Width = 0;
}
CPicture::~CPicture()
//=============================================================================
{
if(m_IPicture != NULL) FreePictureData(); // Important - Avoid Leaks...
}
void CPicture::FreePictureData()
//=============================================================================
{
if(m_IPicture != NULL)
{
m_IPicture->Release();
m_IPicture = NULL;
m_Height = 0;
m_Weight = 0;
m_Width = 0;
}
}
BOOL CPicture::Load(UINT ResourceName, LPCSTR ResourceType)
//=============================================================================
{
BOOL bResult = FALSE;
HGLOBAL hGlobal = NULL;
HRSRC hSource = NULL;
LPVOID lpVoid = NULL;
int nSize = 0;
if(m_IPicture != NULL) FreePictureData(); // Important - Avoid Leaks...
hSource = FindResource(AfxGetResourceHandle(), MAKEINTRESOURCE(ResourceName), ResourceType);
if(hSource == NULL)
{
HWND hWnd = AfxGetApp()->GetMainWnd()->m_hWnd;
MessageBoxEx(hWnd, "FindResource() Failed\t", ERROR_TITLE, MB_OK | MB_ICONSTOP, LANG_ENGLISH);
return(FALSE);
}
hGlobal = LoadResource(AfxGetResourceHandle(), hSource);
if(hGlobal == NULL)
{
HWND hWnd = AfxGetApp()->GetMainWnd()->m_hWnd;
MessageBoxEx(hWnd, "LoadResource() Failed\t", ERROR_TITLE, MB_OK | MB_ICONSTOP, LANG_ENGLISH);
return(FALSE);
}
lpVoid = LockResource(hGlobal);
if(lpVoid == NULL)
{
HWND hWnd = AfxGetApp()->GetMainWnd()->m_hWnd;
MessageBoxEx(hWnd, "LockResource() Failed\t", ERROR_TITLE, MB_OK | MB_ICONSTOP, LANG_ENGLISH);
return(FALSE);
}
nSize = (UINT)SizeofResource(AfxGetResourceHandle(), hSource);
if(LoadPictureData((BYTE*)hGlobal, nSize)) bResult = TRUE;
UnlockResource(hGlobal); // 16Bit Windows Needs This
FreeResource(hGlobal); // 16Bit Windows Needs This (32Bit - Automatic Release)
m_Weight = nSize; // Update Picture Size Info...
if(m_IPicture != NULL) // Do Not Try To Read From Memory That Is Not Exist...
{
m_IPicture->get_Height(&m_Height);
m_IPicture->get_Width(&m_Width);
// Calculate Its Size On a "Standard" (96 DPI) Device Context
m_Height = MulDiv(m_Height, 96, HIMETRIC_INCH);
m_Width = MulDiv(m_Width, 96, HIMETRIC_INCH);
}
else // Picture Data Is Not a Known Picture Type
{
m_Height = 0;
m_Width = 0;
bResult = FALSE;
}
return(bResult);
}
BOOL CPicture::Load(CString sFilePathName)
//=============================================================================
{
BOOL bResult = FALSE;
CFile PictureFile;
CFileException e;
int nSize = 0;
if(m_IPicture != NULL) FreePictureData(); // Important - Avoid Leaks...
if(PictureFile.Open(sFilePathName, CFile::modeRead | CFile::typeBinary, &e))
{
nSize = PictureFile.GetLength();
BYTE* pBuffer = new BYTE[nSize];
if (PictureFile.Read(pBuffer, nSize) > 0 ) //从文件读到pBuffer
{ if(LoadPictureData(pBuffer, nSize)) bResult = TRUE; }//接作调用函数读pBuffer
PictureFile.Close();
delete [] pBuffer;
}
else // Open Failed...
{
TCHAR szCause[255];
e.GetErrorMessage(szCause, 255, NULL);
HWND hWnd = AfxGetApp()->GetMainWnd()->m_hWnd;
MessageBoxEx(hWnd, szCause, ERROR_TITLE, MB_OK | MB_ICONSTOP, LANG_ENGLISH);
bResult = FALSE;
}
m_Weight = nSize; // Update Picture Size Info...
if(m_IPicture != NULL) // Do Not Try To Read From Memory That Is Not Exist...
{
m_IPicture->get_Height(&m_Height);
m_IPicture->get_Width(&m_Width);
// Calculate Its Size On a "Standard" (96 DPI) Device Context
m_Height = MulDiv(m_Height, 96, HIMETRIC_INCH);
m_Width = MulDiv(m_Width, 96, HIMETRIC_INCH);
}
else // Picture Data Is Not a Known Picture Type
{
m_Height = 0;
m_Width = 0;
bResult = FALSE;
}
return(bResult);
}
BOOL CPicture::LoadPictureData(BYTE *pBuffer, int nSize)
//=============================================================================
{
BOOL bResult = FALSE;
HGLOBAL hGlobal = GlobalAlloc(GMEM_MOVEABLE, nSize); //分配内存空间
if(hGlobal == NULL)
{
HWND hWnd = AfxGetApp()->GetMainWnd()->m_hWnd;
MessageBoxEx(hWnd, "Can not allocate enough memory\t", ERROR_TITLE, MB_OK | MB_ICONSTOP, LANG_ENGLISH);
return(FALSE);
}
void* pData = GlobalLock(hGlobal); //锁存内存空间并返回地址
memcpy(pData, pBuffer, nSize); //拷贝内存地址
GlobalUnlock(hGlobal); //解除锁定
IStream* pStream = NULL;
if(CreateStreamOnHGlobal(hGlobal, TRUE, &pStream) == S_OK)
{
HRESULT hr;
if((hr = OleLoadPicture(pStream, nSize, FALSE, IID_IPicture, (LPVOID *)&m_IPicture)) == E_NOINTERFACE)
{
HWND hWnd = AfxGetApp()->GetMainWnd()->m_hWnd;
MessageBoxEx(hWnd, "IPicture interface is not supported\t", ERROR_TITLE, MB_OK | MB_ICONSTOP, LANG_ENGLISH);
return(FALSE);
}
else // S_OK
{
pStream->Release();
pStream = NULL;
bResult = TRUE;
}
}
FreeResource(hGlobal); // 16Bit Windows Needs This (32Bit - Automatic Release)
return(bResult);
}
//-----------------------------------------------------------------------------
BOOL CPicture::Show(CDC *pDC, CRect DrawRect)
//=============================================================================
{
if (pDC == NULL || m_IPicture == NULL) return FALSE;
long Width = 0;
long Height = 0;
m_IPicture->get_Width(&Width);
m_IPicture->get_Height(&Height);
HRESULT hrP = NULL;
hrP = m_IPicture->Render(pDC->m_hDC,
DrawRect.left, // Left
DrawRect.top, // Top
DrawRect.right - DrawRect.left, // Right
DrawRect.bottom - DrawRect.top, // Bottom
0,
Height,
Width,
-Height,
&DrawRect);
if (SUCCEEDED(hrP)) return(TRUE);
HWND hWnd = AfxGetApp()->GetMainWnd()->m_hWnd;
MessageBoxEx(hWnd, "Can not allocate enough memory\t", ERROR_TITLE, MB_OK | MB_ICONSTOP, LANG_ENGLISH);
return(FALSE);
}
//-----------------------------------------------------------------------------
BOOL CPicture::Show(CDC *pDC, CPoint LeftTop, CPoint WidthHeight, int MagnifyX, int MagnifyY)
//=============================================================================
{
if (pDC == NULL || m_IPicture == NULL) return FALSE;
long Width = 0;
long Height = 0;
m_IPicture->get_Width(&Width);
m_IPicture->get_Height(&Height);
if(MagnifyX == NULL) MagnifyX = 0;
if(MagnifyY == NULL) MagnifyY = 0;
MagnifyX = int(MulDiv(Width, pDC->GetDeviceCaps(LOGPIXELSX), HIMETRIC_INCH) * MagnifyX);
MagnifyY = int(MulDiv(Height,pDC->GetDeviceCaps(LOGPIXELSY), HIMETRIC_INCH) * MagnifyY);
CRect DrawRect(LeftTop.x, LeftTop.y, MagnifyX, MagnifyY);
HRESULT hrP = NULL;
hrP = m_IPicture->Render(pDC->m_hDC,
LeftTop.x, // Left
LeftTop.y, // Top
WidthHeight.x +MagnifyX, // Width
WidthHeight.y +MagnifyY, // Height
0,
Height,
Width,
-Height,
&DrawRect);
if(SUCCEEDED(hrP)) return(TRUE);
HWND hWnd = AfxGetApp()->GetMainWnd()->m_hWnd;
MessageBoxEx(hWnd, "Can not allocate enough memory\t", ERROR_TITLE, MB_OK | MB_ICONSTOP, LANG_ENGLISH);
return(FALSE);
}
BOOL CPicture::UpdateSizeOnDC(CDC *pDC)
{
if(pDC == NULL || m_IPicture == NULL) { m_Height = 0; m_Width = 0; return(FALSE); };
m_IPicture->get_Height(&m_Height);
m_IPicture->get_Width(&m_Width);
// Get Current DPI - Dot Per Inch
int CurrentDPI_X = pDC->GetDeviceCaps(LOGPIXELSX);
int CurrentDPI_Y = pDC->GetDeviceCaps(LOGPIXELSY);
// Use a "Standard" Print (When Printing)
if(pDC->IsPrinting())
{
CurrentDPI_X = 200;
CurrentDPI_Y = 200;
}
m_Height = MulDiv(m_Height, CurrentDPI_Y, HIMETRIC_INCH);
m_Width = MulDiv(m_Width, CurrentDPI_X, HIMETRIC_INCH);
return(TRUE);
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -