关于CPictureEx类,网上很多文章了。但基于CPictureEx展示的GIF图片,却无法缩放,有些不方便。因项目中用到了这个类,而且要实现缩放的功能。于是研究了一下,现将相关代码写下来,仅供参考。
分两步:
1、主要是修改OnPaint函数,这个方法里使用了BitBlt函数,这是一个不能缩放图片的函数,我们使用StretchBlt来实现缩放。
2、使用SetPaintRect函数,此函数在CPictureEx类中。
看核心代码:
CPictureEx *m_Picture; void CAnimationPic::Draw(CDC *pDC) //绘制GIF动画 { CRect rect;//定义矩形大小 rect.TopLeft().x = m_nOrgX; rect.TopLeft().y = m_nOrgY; rect.BottomRight().x = m_nDestX; rect.BottomRight().y = m_nDestY; m_Picture = new CPictureEx(); ASSERT_VALID(m_Picture); m_Picture->Create(_T("GIF"),WS_CHILD|WS_VISIBLE|SS_NOTIFY,rect,pDC->GetWindow(),21234); m_Picture->Load(_T(m_AnimationPicName));//加载GIF路径 m_Picture->SetBkColor(RGB(255,255,255)); // m_Picture->SetBgMode(CPictureEx::BackgroundMode::TransparentBg, RGB(0, 0, 0)); pDC->SetBkColor(RGB(255,255,255)); m_Picture->SetPaintRect(&rect); m_Picture->Draw(); } void CPictureEx::OnPaint() { CPaintDC dc(this); // device context for painting LONG nPaintWidth = m_PaintRect.right-m_PaintRect.left; if (nPaintWidth > 0) { LONG nPaintHeight = m_PaintRect.bottom - m_PaintRect.top; // ::BitBlt(dc.m_hDC, 0, 0, nPaintWidth, nPaintHeight, // m_hMemDC, m_PaintRect.left, m_PaintRect.top, SRCCOPY); ::StretchBlt(dc.m_hDC, 0, 0, nPaintWidth,nPaintHeight, m_hMemDC, 0,0,m_PictureSize.cx, m_PictureSize.cy,SRCCOPY); } else { ::BitBlt(dc.m_hDC, 0, 0, m_PictureSize.cx, m_PictureSize.cy, m_hMemDC, 0, 0, SRCCOPY); } } //此函数供放大缩小按钮调用 void CAnimationPic::AdjustPositon(CRect rect) { if (m_Picture != NULL) { m_Picture->SetPaintRect(&rect); m_Picture->MoveWindow(&rect, TRUE); } }