| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081 |
- #include "pch.h"
- #include "CImageStatic.h"
- IMPLEMENT_DYNAMIC(CImageStatic, CStatic)
- CImageStatic::CImageStatic()
- {
- }
- CImageStatic::~CImageStatic()
- {
- }
- BEGIN_MESSAGE_MAP(CImageStatic, CStatic)
- END_MESSAGE_MAP()
- BOOL CImageStatic::SetImage(LPCTSTR szImage)
- {
- CString szPath = Utils::GetExecDir();
- szPath.Append(_T("\\"));
- szPath.Append(szImage);
- BOOL bRet = FALSE;
- m_Image.Destroy();
- bRet = (m_Image.Load(szPath) == S_OK);
- if (m_hWnd)
- {
- Invalidate(FALSE);
- }
- return bRet;
- }
- LRESULT CImageStatic::WindowProc(UINT message, WPARAM wParam, LPARAM lParam)
- {
- switch (message)
- {
- case(WM_ERASEBKGND):
- return 1;
- case(WM_PAINT):
- {
- PAINTSTRUCT ps;
- CDC *pDstDC = BeginPaint(&ps);
- //客户区位置
- CRect rcClient;
- GetClientRect(&rcClient);
- //双缓冲绘图
- {
- CMemDC memDC(*pDstDC, rcClient);
- CDC *pDC = &memDC.GetDC();
- //填充背景色
- pDC->FillSolidRect(&rcClient, RGB(100, 100, 100));
- pDC->SetStretchBltMode(HALFTONE);
- if (!m_Image.IsNull())
- {
- //原图大小
- CRect rcSrc(0, 0, m_Image.GetWidth(), m_Image.GetHeight());
- //锁定原图比例
- double fZoomRate = min((double)rcClient.Width() / (double)rcSrc.Width(),
- (double)rcClient.Height() / (double)rcSrc.Height());
- int cx = (int)(rcSrc.Width()*fZoomRate), cy = (int)(rcSrc.Height()*fZoomRate);
- CRect rcDst(0, 0, cx, cy); //目标大小
- rcDst.OffsetRect((rcClient.Width() - cx) / 2, (rcClient.Height() - cy) / 2);//居中
- //绘图
- m_Image.Draw(pDC->m_hDC, rcDst, rcSrc);
- }
- }
- EndPaint(&ps);
- return 0;
- }
- }
- return CStatic::WindowProc(message, wParam, lParam);
- }
|