概述
1. 图形类中有数据成员,开始点,结束点,一个包含可参与矩阵运算的点数组
2. 使用一个矩阵,调用矩阵的设置函数,读入图形类中点数组的地址
3. 为矩阵输入平移、缩放参数
4. 运算矩阵并把数据保存到图形类中的点数组
5. 绘图时,取点数组中数据绘图,
public:
CPoint pt1;
CPoint pt0;
CPoint p[3];
BOOL bLBDown ;//左键按下状态初始化
bool bMWDown;
int nCount ;//顶点计数器初始化
double m_Scale;
CPoint m_ptCur;
CLine2 line2 ;
CTransfrom2 Transfrom ;
void CTestView::OnDraw(CDC* pDC)
{
CTestDoc* pDoc = GetDocument();
ASSERT_VALID(pDoc);
// TODO: add draw code for native data here
if (!pDoc)
return;
// TODO: 在此处为本机数据添加绘制代码
//DoubleBuffer(pDC);
line2.Draw(pDC);
CString str;
str.Format("m_Scale %0.2f m_ptCur x=%d,y=%d", m_Scale , m_ptCur.x, m_ptCur.y);
pDC->TextOut(500, 50, str);//输出顶点实时坐标
}
void CTestView::OnMouseMove(UINT nFlags, CPoint point)
{
// TODO: Add your message handler code here and/or call default
m_ptCur = point ;
CView::OnMouseMove(nFlags, point);
}
void CTestView::OnLButtonDown(UINT nFlags, CPoint point)
{
// TODO: Add your message handler code here and/or call default
bLBDown = true;//鼠标左键按下,记为真
CView::OnLButtonDown(nFlags, point);
}
void CTestView::OnLButtonUp(UINT nFlags, CPoint point)
{
// TODO: Add your message handler code here and/or call default
bLBDown = false;//鼠标左键抬起,记为假
CView::OnLButtonUp(nFlags, point);
}
BOOL CTestView::OnMouseWheel(UINT nFlags, short zDelta, CPoint pt)
{
// TODO: Add your message handler code here and/or call default
int nDelta = zDelta;
switch (nDelta)
{
case -120:
m_Scale += 0.1;
bMWDown = true ;
if (m_Scale >= 1.0) m_Scale = 1.0;
break;
case 120:
m_Scale -= 0.1;
bMWDown = false ;
if (m_Scale <= 0.1) m_Scale = 0.1;
break;
default: break;
}
CPoint tmp = m_ptCur ;
CDC* pDC = GetDC();
pDC->DPtoLP(&tmp);
ReleaseDC(pDC);
CP2 *ptCen = new CP2;
ptCen-> x = (double)tmp.x ;
ptCen-> y = (double)tmp.y ;
Transfrom.SetMatrix(line2.P ,2);
if(bMWDown)
{
Transfrom.Scale(0.5f, 0.5f ,*ptCen);
}
else
{
Transfrom.Scale(2.0f, 2.0f ,*ptCen);
}
delete ptCen ;
Invalidate();
return true ;
//return CView::OnMouseWheel(nFlags, zDelta, pt);
}
void CTestView::OnMButtonDown(UINT nFlags, CPoint point)
{
// TODO: Add your message handler code here and/or call default
CDC* pDC = GetDC();
pDC->DPtoLP(&point);
pt0 = point ;
CView::OnMButtonDown(nFlags, point);
}
void CTestView::OnMButtonUp(UINT nFlags, CPoint point)
{
// TODO: Add your message handler code here and/or call default
CDC* pDC = GetDC();
pDC->DPtoLP(&point);
pt1 = point ;
double x = pt1.x - pt0.x ;
double y = pt1.y - pt0.y ;
Transfrom.SetMatrix(line2.P ,2);
Transfrom.Translate(x,y);
Invalidate();
CView::OnMButtonUp(nFlags, point);
}
----------------------------------------------------------------------------------------
pDC->TextOut(0,0,_T("SetWorldTransform!"));
pDC->MoveTo(100,50); pDC->LineTo(200,50);
XFORM xform;
ZeroMemory(&xform, sizeof(XFORM));
xform.eM11 = 1;
xform.eM22 = 1;
xform.eDx = 100;
xform.eDy = 100;
HDC hDC = pDC->GetSafeHdc();
SetGraphicsMode(hDC , GM_ADVANCED);
pDC->SetMapMode(MM_ANISOTROPIC);
SetWorldTransform(hDC, &xform);
pDC->TextOut(0,0,_T("SetWorldTransform!Translate!"));//平移到(100,100)
pDC->MoveTo(100,50); pDC->LineTo(200,50);
xform.eM11 = 5.0f;
xform.eM22 = 5.0f;
SetWorldTransform(hDC, &xform);
pDC->TextOut(0,0,_T("SetWorldTransform!Scale!"));//放大
pDC->MoveTo(100,50); pDC->LineTo(200,50);
xform.eM11 = 0.866f;
xform.eM22 = 0.866f;
xform.eM12 = 0.5f;
xform.eM21 = -0.5f;
SetWorldTransform(hDC, &xform);
pDC->TextOut(0,0,_T("SetWorldTransform!Rotate!"));//旋转30°
pDC->MoveTo(100,50); pDC->LineTo(200,50);
ZeroMemory(&xform, sizeof(XFORM));
xform.eM11 = 1;
xform.eM22 = 1;
SetWorldTransform(hDC, &xform);//恢复默认
SetGraphicsMode(hDC , GM_COMPATIBLE);//恢复默认
pDC->MoveTo(100,60); pDC->LineTo(200,60);
----------------------------------------------------------------------------------------------------------
public:
CPoint pt1;
CPoint pt0;
int sc;
CTestDxfView::CTestDxfView()
{
// TODO: add construction code here
sc = 5;
}
void CTestDxfView::OnDraw(CDC* pDC)
{
CTestDxfDoc* pDoc = GetDocument();
ASSERT_VALID(pDoc);
// TODO: add draw code for native data here
//取得视口矩形
CRect rect;
GetClientRect(&rect);
//
pDC->SetMapMode(MM_ANISOTROPIC); //逻辑单元将转换为具有任意缩放轴的任意单位
pDC->SetViewportExt (rect.right,rect.bottom);
pDC->SetViewportOrg(rect.right/2 + (pt1.x-pt0.x),rect.bottom/2 + (pt1.y-pt0.y));
pDC->SetWindowExt (rect.right*sc,-rect.bottom*sc);
pDC->SetWindowOrg(0,0);
pDC->Rectangle(-3000,-1300,3000,1300);
pDC->Rectangle(-3000+50,-1300+50,3000-50,1300-50);
}
BOOL CTestDxfView::OnMouseWheel(UINT nFlags, short zDelta, CPoint pt)
{
// TODO: Add your message handler code here and/or call default
switch (zDelta)
{
case -120:
if (sc >= 20)
{
sc = 20;
}
else
{
sc += 1;
}
break;
case 120:
if (sc <= 1)
{
sc = 1;
}
else
{
sc -= 1;
}
break;
default:
break;
}
Invalidate();
return CView::OnMouseWheel(nFlags, zDelta, pt);
}
///手动添加消息映射
ON_WM_MBUTTONDOWN()
ON_WM_MBUTTONUP()
afx_msg void OnMButtonDown(UINT nFlags, CPoint point);
afx_msg void OnMButtonUp(UINT nFlags, CPoint point);
void CTestDxfView::OnMButtonDown(UINT nFlags, CPoint point)
{
// TODO: Add your message handler code here and/or call default
CDC* pDC = GetDC();
pDC->DPtoLP(&point);
pt0 = point ;
CView::OnMButtonDown(nFlags, point);
}
void CTestDxfView::OnMButtonUp(UINT nFlags, CPoint point)
{
// TODO: Add your message handler code here and/or call default
CDC* pDC = GetDC();
pDC->DPtoLP(&point);
pt1 = point ;
Invalidate();
CView::OnMButtonUp(nFlags, point);
}
最后
以上就是明理期待为你收集整理的MFC-VIEW 缩放 平移的全部内容,希望文章能够帮你解决MFC-VIEW 缩放 平移所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
发表评论 取消回复