133 lines
3.1 KiB
C++
133 lines
3.1 KiB
C++
#include "stdafx.h"
|
|
#include "CPictureCtrl.h"
|
|
BEGIN_MESSAGE_MAP(CPictureCtrl, CStatic)
|
|
//{{AFX_MSG_MAP(CPictureCtrl)
|
|
ON_WM_DRAWITEM()
|
|
ON_WM_PAINT()
|
|
//}}AFX_MSG_MAP
|
|
END_MESSAGE_MAP()
|
|
|
|
|
|
void CPictureCtrl::OnDrawItem(int nIDCtl, LPDRAWITEMSTRUCT lpDrawItemStruct)
|
|
{
|
|
CStatic::OnDrawItem(nIDCtl, lpDrawItemStruct);
|
|
return;
|
|
/*
|
|
CDC* pDC = CDC::FromHandle(lpDrawItemStruct->hDC);
|
|
CRect rect;
|
|
GetClientRect(&rect);
|
|
|
|
if (m_pBitmap)
|
|
{
|
|
Graphics graphics(pDC->m_hDC);
|
|
graphics.SetInterpolationMode(InterpolationModeHighQualityBicubic); // 设置高质量缩放
|
|
|
|
int imageWidth = m_pBitmap->GetWidth();
|
|
int imageHeight = m_pBitmap->GetHeight();
|
|
|
|
float scaleX = static_cast<float>(rect.Width()) / imageWidth;
|
|
float scaleY = static_cast<float>(rect.Height()) / imageHeight;
|
|
float scale = min(scaleX, scaleY);
|
|
int scaledWidth = static_cast<int>(imageWidth * scale);
|
|
int scaledHeight = static_cast<int>(imageHeight * scale);
|
|
|
|
int xPos = (rect.Width() - scaledWidth) / 2;
|
|
int yPos = (rect.Height() - scaledHeight) / 2;
|
|
|
|
graphics.DrawImage(m_pBitmap, xPos, yPos, scaledWidth, scaledHeight);
|
|
}
|
|
*/
|
|
|
|
}
|
|
|
|
CPictureCtrl::CPictureCtrl() :
|
|
m_gdiplusToken(0),
|
|
m_pBitmap(NULL)
|
|
{
|
|
|
|
// 初始化GDI+
|
|
GdiplusStartupInput gdiplusStartupInput;
|
|
GdiplusStartup(&m_gdiplusToken, &gdiplusStartupInput, NULL);
|
|
|
|
}
|
|
|
|
CPictureCtrl::~CPictureCtrl()
|
|
{
|
|
FreeData();
|
|
|
|
// 关闭GDI+
|
|
GdiplusShutdown(m_gdiplusToken);
|
|
}
|
|
|
|
BOOL CPictureCtrl::LoadFromStream(IStream* piStream)
|
|
{
|
|
FreeData();
|
|
m_pBitmap = Bitmap::FromStream(piStream);
|
|
return m_pBitmap != NULL;
|
|
}
|
|
|
|
BOOL CPictureCtrl::LoadFromReSource(DWORD dwID)
|
|
{
|
|
FreeData();
|
|
|
|
|
|
|
|
//加载位图资源
|
|
HBITMAP hBitmap = LoadBitmap(AfxGetInstanceHandle(), MAKEINTRESOURCE(dwID));
|
|
if (hBitmap)
|
|
{
|
|
// 创建GDI+ Bitmap对象
|
|
m_pBitmap = Bitmap::FromHBITMAP(hBitmap, NULL);
|
|
|
|
// 释放资源
|
|
DeleteObject(hBitmap);
|
|
}
|
|
|
|
|
|
return m_pBitmap != NULL;
|
|
}
|
|
|
|
void CPictureCtrl::FreeData()
|
|
{
|
|
|
|
if (m_pBitmap)
|
|
{
|
|
delete m_pBitmap;
|
|
m_pBitmap = NULL;
|
|
}
|
|
}
|
|
|
|
|
|
void CPictureCtrl::OnPaint()
|
|
{
|
|
CPaintDC dc(this); // device context for painting
|
|
|
|
|
|
CDC* pDC = CDC::FromHandle(dc.GetSafeHdc());
|
|
CRect rect;
|
|
GetClientRect(&rect);
|
|
|
|
if (m_pBitmap)
|
|
{
|
|
Graphics graphics(pDC->m_hDC);
|
|
graphics.SetInterpolationMode(InterpolationModeHighQualityBicubic); // 设置高质量缩放
|
|
|
|
int imageWidth = m_pBitmap->GetWidth();
|
|
int imageHeight = m_pBitmap->GetHeight();
|
|
|
|
float scaleX = static_cast<float>(rect.Width()) / imageWidth;
|
|
float scaleY = static_cast<float>(rect.Height()) / imageHeight;
|
|
float scale = min(scaleX, scaleY);
|
|
int scaledWidth = static_cast<int>(imageWidth * scale);
|
|
int scaledHeight = static_cast<int>(imageHeight * scale);
|
|
|
|
int xPos = (rect.Width() - scaledWidth) / 2;
|
|
int yPos = (rect.Height() - scaledHeight) / 2;
|
|
|
|
//保持比例显示
|
|
graphics.DrawImage(m_pBitmap, xPos, yPos, scaledWidth, scaledHeight);
|
|
}
|
|
|
|
|
|
}
|