在进行图形方面编程时,绘图窗口的放大缩小平移是必不可少的功能,当前例程也实现这一功能,效果如图。点着中键移动鼠标可任意移动圆,鼠标滚动前面滚动,可放大缩小圆。点击工具条上四个按钮还可实现放大缩小,全屏显示圆,以及全屏显示鼠标绘制的矩形区域
例程缩放平移的要点:显示在视窗上图元的几何坐标(如直线的两端点坐标)是经过转换后显示在窗口上的。通过函数实现转换 void CMy123View: PtoVP(float x,float y,int *X,int *Y) { *X=(int)((x-m_xStart)/blc); if(m_MapMode==MM_TEXT) *Y=m_hScreen-(int)((y-m_yStart)/blc); else *Y=(int)((y-m_yStart)/blc)-m_hScreen;}
Blc为缩放比例尺,m_hScreen, m_hScreen为平移XY和偏移量,很明显只要修改这两个参数,刷新界面重新绘制就可实现图元的平移与缩放 - 1.新建基于Cview的文档程序。
- 2.在视窗类中添加成员函数与变量
- enum ACTION_MODE{
- ACTION_OFF,
- ACTION_AREA_FILL,//框选区域全屏显示
- ACTION_SCREEN_MOVE,//移动图元窗口
- ACTION_ZOOM_IN,//放大图元窗口
- ACTION_ZOOM_OUT,//缩小图元窗口
- };
- private:
- ACTION_MODE m_ActionMode;//操作模式
- CPoint m_FirstPt;//保存鼠标左键第一次点击
- short m_nNum;//保存鼠标左键点击次数
- float m_xStart,m_yStart,m_xStartTemp,m_yStartTemp,blc; //绘制图形时的起点坐标和比例尺
- int m_hScreen,m_wScreen; //当前视图的高度和宽度
- int m_MapMode;//当前映射模式
- void DPtoVP(float x,float y,int *X,int *Y);//设备点转视窗点
- void VPtoDP(int x,int y,float *X,float *Y);
- void FitScreen();//全屏显示全部图元
- void SetMoveScreenBasePt(CPoint pt);//设置移动基点
- void MoveScreen(CPoint pt);//设置移动偏移点
- void ZoomIn(CPoint point,float delta=1.25);//以基点位置放大
- void ZoomIn(CRect rc);//全屏显示矩形内图元放大
- void ZoomOut(CPoint point,float delta=1.25);//以基点位置缩小
-
复制代码- 3.初始化变量
- CMy123View::CMy123View()
- {
- m_ActionMode =ACTION_OFF;
- m_xStart=0;
- m_yStart=0;
- blc=1.0;
- m_MapMode=MM_TEXT;
- m_nNum=0;
- }
- 在OnDraw函数中添加绘制图元的测试代码
- void CMy123View::OnDraw(CDC* pDC){
- CMy123Doc* pDoc = GetDocument();
- ASSERT_VALID(pDoc);
- // TODO: add draw code for native data here
- float x0 = 0, y0 = 0;
- float x1 = 300, y1 = 300;
- int StartX,StartY,EndX,EndY;
- DPtoVP(x0,y0,&StartX,&StartY);
- DPtoVP(x1,y1,&EndX,&EndY);
- CPen pen(PS_SOLID,1,RGB(255,0,0));
- CPen *pOldPen = pDC->SelectObject(&pen);
- pDC->Ellipse(StartX,StartY,EndX,EndY);
- pDC->SelectObject(pOldPen);}
复制代码- 4.添加鼠标中键点击,放开,左键点击放开,鼠标移动,鼠标滑轮消息
- void CMy123View::OnLButtonDown(UINT nFlags, CPoint point)
- {
- m_FirstPt = point;
- CView::OnLButtonDown(nFlags, point);
- }
- void CMy123View::OnMouseMove(UINT nFlags, CPoint point)
- {
- static CPoint OldPt = point;
- CClientDC dc(this);
- MoveScreen(point);//鼠标中键平移操作
- switch(m_ActionMode)
- {
- case ACTION_SCREEN_MOVE:
- break;
- case ACTION_AREA_FILL://框选区域全屏显示
- if(nFlags&MK_LBUTTON) {
- int OldMode = dc.SetROP2(R2_NOT);
- dc.SelectStockObject(NULL_BRUSH); //设置不填充状态
- dc.Rectangle(m_FirstPt.x,m_FirstPt.y,OldPt.x,OldPt.y);
- dc.Rectangle(m_FirstPt.x,m_FirstPt.y,point.x,point.y);
- dc.SetROP2(OldMode);
- }
-
复制代码- break;
- default:
- break;
- }
- OldPt = point;
- CView::OnMouseMove(nFlags, point);
- }
- void CMy123View::OnMButtonDown(UINT nFlags, CPoint point)
- {
- SetMoveScreenBasePt(point);
- CView::OnMButtonDown(nFlags, point);
- }
- void CMy123View::OnMButtonUp(UINT nFlags, CPoint point)
- {
- if(ACTION_SCREEN_MOVE == m_ActionMode)
- {
- m_ActionMode = ACTION_OFF;
- }
- CView::OnMButtonUp(nFlags, point);
- }
- BOOL CMy123View::OnMouseWheel(UINT nFlags, short zDelta, CPoint pt)
- {
- if(zDelta>0)//zDelta 120倍数,负滚轮滚向面,反之反
- {
- ZoomIn(pt);
- }
- else
- {
- ZoomOut(pt);
- }
- return CView::OnMouseWheel(nFlags, zDelta, pt);
- }
复制代码
上位机VC MFC图形视窗缩放平移
如果您认可,可联系功能定制! 如果您着急,充值会员可直接联系发您资料! |