VC++开发GIS系统(131)移动点之类的设计及实现

前文《VC++开发GIS系统(130)移动点之更新点函数》已经介绍了移动点的基础文件读写接口,本文将实现一个移动点的类并实现移动点的功能。

移动点类CmalaPointsMove的定义

在_malaPoints.h中定义移动的点的类:CmalaPointsMove,代码如下:

/*
* 移动点
*/
class malapointsdll  CmalaPointsMove :public CmalaMouseAction
{
public:
    CmalaPointsMove(CView* mView, malaScreen *pScreen, CString &fileFullPath);
    ~CmalaPointsMove();
    void LButtonDown(UINT nFlags, malaPoint point);
    void LButtonUp(UINT nFlags, malaPoint point);
    void MouseMove(UINT nFlags, malaPoint point);
private:
    bool m_Selected;
    malaPoint m_Point;//选择的点
    malaPointPro m_PointPro;//选择的点的属性
    CmalaPointsSelect m_SelectPnt;

    CString mPath;//文件路径

    bool m_bDraw;
    malaPoint m_ptOrigin;//起点
    malaPoint m_perPoint;//临时点
    malaScreen *m_Screen;
};

其中m_Point和m_PointPro用于保存的点和属性,m_ptOrigin和m_perPoint用于橡皮筋绘图。

移动点类CmalaPointsMove的实现

/*
* 移动点实现
*/
CmalaPointsMove::CmalaPointsMove(CView* mView, malaScreen *pScreen, CString &fileFullPath)
{
    mBaseView = mView;
    mPath = fileFullPath;
    m_Screen = pScreen;
    CmalaPointsSelect obj(mView, pScreen, fileFullPath);
    m_SelectPnt = obj;
    m_Selected = FALSE;
    m_bDraw = FALSE;
}

CmalaPointsMove::~CmalaPointsMove()
{

}

void CmalaPointsMove::LButtonDown(UINT nFlags, malaPoint point)
{
    if (!m_Selected)
        m_SelectPnt.LButtonDown(nFlags, point);
    else
    {
        m_bDraw = TRUE;
        m_ptOrigin = point;
    }
}

void CmalaPointsMove::LButtonUp(UINT nFlags, malaPoint point)
{
    if (!m_Selected)
        m_SelectPnt.LButtonUp(nFlags, point);
    else
    {
        CPointIO pio;
        pio.pointUpdate(point, m_PointPro, mPath);
        mBaseView->Invalidate(TRUE);
        m_bDraw = FALSE;
        m_Selected = FALSE;
        m_SelectPnt.m_Selected = FALSE;

    }

    m_Selected = m_SelectPnt.m_Selected;
    if (m_Selected)
    {
        this->m_Point = m_SelectPnt.m_pnt;
        this->m_PointPro = m_SelectPnt.m_PntPro;
        m_perPoint = m_Point;
    }

}

void CmalaPointsMove::MouseMove(UINT nFlags, malaPoint point)
{
    if (!m_Selected)
        m_SelectPnt.MouseMove(nFlags, point);
    else if (m_bDraw)
    {
        malaCDC dc(mBaseView, *m_Screen);
        dc.pointDrawAutoX(m_perPoint, m_PointPro);
        m_perPoint.x = m_ptOrigin.x + point.x - m_ptOrigin.x;
        m_perPoint.y = m_ptOrigin.y + point.y - m_ptOrigin.y;
        dc.pointDrawAutoX(m_perPoint, m_PointPro);
        
    }
}

这里的一个关键是通过m_Selected这个变量判断是否有选中的点,如果没有,执行的选择点的操作。如果已经选中了,就进行移动点的操作,移动点的操作过程如下:

  1. 按下鼠标左键,不松开
  2. 鼠标移动,选中的点跟随鼠标移动
  3. 鼠标松开,将选中点的坐标改为当前点的坐标

其中第3步,调用了pointUpdate函数,也就是在《VC++开发GIS系统(130)移动点之更新点函数》所增加的函数。

详细代码可以参考:GitHub

VC++开发GIS系统(130)移动点之更新点文件读写函数 VC++开发GIS系统(132)移动点之菜单响应

作者:,GIS爱好者。
分享本文,请您带上本文链接
分享到:

发表评论