主要函数为WM_MOUSEWHEEL的函数OnMouseWheel与OnDraw。
缩放使用了GDI的仿射变换实现。代码如下,并附上效果图。
这两个函数中除了m_Scale是成员变量外,其余都是局部变量,
m_Scale你需要自己加入view类中,并在构造函数中初始化为1.0f.
void CScaleObjectView::OnDraw(CDC* pDC)
{
static CPen penStatic(PS_SOLID, 3, RGB(0,255,0));
static CPen penScaled(PS_SOLID, 3, RGB(0,0,255));
static CRect rcDraw(-50, -50, 50, 50);
XFORM xForm = //the affine tranform matrix which GDI adapted.
{
1.0f, 0.0f,
0.0f, 1.0f,
0.0f, 0.0f
};
RECT rcClient;
this->GetClientRect(&rcClient);
CScaleObjectDoc* pDoc = GetDocument();
ASSERT_VALID(pDoc);
if (!pDoc) return;
pDC->SetGraphicsMode(GM_ADVANCED);
pDC->SelectObject(::GetStockObject(NULL_BRUSH));
xForm.eM11 = m_Scale;
xForm.eM22 = m_Scale;
xForm.eDx = rcClient.right / 2.0f;
xForm.eDy = rcClient.bottom / 2.0f;
pDC->SetWorldTransform(&xForm);
pDC->SelectObject(penScaled);
pDC->Rectangle(rcDraw);
xForm.eM11 = 1.0f;
xForm.eM22 = 1.0f;
pDC->SetWorldTransform(&xForm);
pDC->SelectObject(penStatic);
pDC->Rectangle(rcDraw);
}
BOOL CScaleObjectView::OnMouseWheel(UINT nFlags, short zDelta, CPoint pt)
{
static float upLimit = 10.0f;
static float downLinit = 0.04f;
static float fUp = 1.1f;
static float fDown = 0.9f;
if(zDelta > 0 && m_Scale <= upLimit)
{
m_Scale *= fUp;
}
else if(zDelta < 0 && m_Scale >= downLinit)
{
m_Scale *= fDown;
}
this->Invalidate();
return CView::OnMouseWheel(nFlags, zDelta, pt);
}
怎么联系?
楼上所写的只是重绘把,并不是真正意义上的放大。恐怕得用OpenGL