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); // <20><><EFBFBD>ø<EFBFBD><C3B8><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|||
|
|
|||
|
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)
|
|||
|
{
|
|||
|
|
|||
|
// <20><>ʼ<EFBFBD><CABC>GDI+
|
|||
|
GdiplusStartupInput gdiplusStartupInput;
|
|||
|
GdiplusStartup(&m_gdiplusToken, &gdiplusStartupInput, NULL);
|
|||
|
|
|||
|
}
|
|||
|
|
|||
|
CPictureCtrl::~CPictureCtrl()
|
|||
|
{
|
|||
|
FreeData();
|
|||
|
|
|||
|
// <20>ر<EFBFBD>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();
|
|||
|
|
|||
|
|
|||
|
|
|||
|
//<2F><><EFBFBD><EFBFBD>λͼ<CEBB><CDBC>Դ
|
|||
|
HBITMAP hBitmap = LoadBitmap(AfxGetInstanceHandle(), MAKEINTRESOURCE(dwID));
|
|||
|
if (hBitmap)
|
|||
|
{
|
|||
|
// <20><><EFBFBD><EFBFBD>GDI+ Bitmap<61><70><EFBFBD><EFBFBD>
|
|||
|
m_pBitmap = Bitmap::FromHBITMAP(hBitmap, NULL);
|
|||
|
|
|||
|
// <20>ͷ<EFBFBD><CDB7><EFBFBD>Դ
|
|||
|
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); // <20><><EFBFBD>ø<EFBFBD><C3B8><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|||
|
|
|||
|
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;
|
|||
|
|
|||
|
//<2F><><EFBFBD>ֱ<EFBFBD><D6B1><EFBFBD><EFBFBD><EFBFBD>ʾ
|
|||
|
graphics.DrawImage(m_pBitmap, xPos, yPos, scaledWidth, scaledHeight);
|
|||
|
}
|
|||
|
|
|||
|
|
|||
|
}
|