📄 olepicture.cpp
字号:
// OlePicture.cpp: implementation of the OlePicture class.
//
//////////////////////////////////////////////////////////////////////
#include "stdafx.h"
#include "LCDSample.h"
#include "OlePicture.h"
#include "memory.h"
#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[]=__FILE__;
#define new DEBUG_NEW
#endif
//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////
OlePicture::OlePicture()
{
}
OlePicture::~OlePicture()
{
}
BOOL OlePicture::IsValidPicture()
{
if (m_picture)
return TRUE;
else
return FALSE;
}
BOOL OlePicture::LoadPicture(HMODULE hModule, UINT nIDResource, LPCTSTR lpszType)
{
Close();
// 把图片从资源中读入内存
HRSRC hRes = ::FindResource(hModule, MAKEINTRESOURCE(nIDResource), lpszType);
if (hRes == NULL)
return FALSE;
HGLOBAL hData = ::LoadResource(hModule, hRes);
int size = ::SizeofResource(hModule, hRes);
HGLOBAL hMem = ::GlobalAlloc(GMEM_MOVEABLE, size);
//char *hMem = malloc(size);
memcpy(::GlobalLock(hMem), ::LockResource(hData), size);
::GlobalUnlock(hMem);
// 创建流对象
IStream* stream;
::CreateStreamOnHGlobal(hMem, TRUE, &stream);
// 从流对象创建图像对象
if (::OleLoadPicture(stream, size, TRUE, IID_IPicture, (void**)&m_picture) != S_OK)
m_picture = NULL;
// 释放资源
stream->Release();
::GlobalFree(hMem);
if (m_picture)
return TRUE; // 创建成功
else
return FALSE; // 创建失败
}
BOOL OlePicture::LoadPicture(LPCTSTR lpszFileName)
{
Close();
// 把图片文件读入内存
FILE* fp = fopen(lpszFileName, "rb");
if (fp == NULL)
return FALSE; // 文件打不开,创建失败
fseek(fp, 0, SEEK_END);
int fsize = ftell(fp);
fseek(fp, 0, SEEK_SET);
if (fsize == 0)
{
// 文件尺寸为0,创建失败
fclose(fp);
return FALSE;
}
HGLOBAL hMem = ::GlobalAlloc(GMEM_MOVEABLE, fsize);
fread(::GlobalLock(hMem), fsize, 1, fp);
fclose(fp);
::GlobalUnlock(hMem);
// 创建流对象
IStream* stream;
::CreateStreamOnHGlobal(hMem, TRUE, &stream);
// 从流对象创建图像对象
if (::OleLoadPicture(stream, fsize, TRUE, IID_IPicture, (void**)&m_picture) != S_OK)
m_picture = NULL;
// 释放资源
stream->Release();
::GlobalFree(hMem);
if (m_picture)
return TRUE; // 创建成功
else
return FALSE; // 创建失败
}
SIZE OlePicture::GetPictureSize()
{
if (m_picture)
{
// 获取图片尺寸
OLE_XSIZE_HIMETRIC hmWidth;
OLE_YSIZE_HIMETRIC hmHeight;
m_picture->get_Width(&hmWidth);
m_picture->get_Height(&hmHeight);
SIZE sizePicture = {hmWidth, hmHeight};
CDC dc;
dc.CreateCompatibleDC(NULL);
dc.HIMETRICtoDP(&sizePicture);
return sizePicture;
}
else
return CSize(0,0);
}
void OlePicture::DrawPicture(HDC hDC, int x, int y, int cx /*= 0*/, int cy /*= 0*/)
{
if (m_picture)
{
SIZE sizeDevice = GetPictureSize();
if (cx == 0)
{
// 把实际的宽度从像素单位(设备坐标)转换成逻辑坐标
RECT rect = {0, 0, sizeDevice.cx, 0};
CDC::FromHandle(hDC)->DPtoLP(&rect);
cx = rect.right - rect.left;
}
if (cy == 0)
{
// 把实际的高度从像素单位(设备坐标)转换成逻辑坐标
RECT rect = {0, 0, 0, sizeDevice.cy};
CDC::FromHandle(hDC)->DPtoLP(&rect);
cy = rect.bottom - rect.top;
}
// 绘制图片
OLE_XSIZE_HIMETRIC hmWidth;
OLE_YSIZE_HIMETRIC hmHeight;
m_picture->get_Width(&hmWidth);
m_picture->get_Height(&hmHeight);
m_picture->Render(hDC, x, y, cx, cy, 0, hmHeight, hmWidth, -hmHeight, NULL);
}
}
void OlePicture::Close()
{
if (m_picture)
{
m_picture->Release();
m_picture = NULL;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -