VC++开发GIS系统(234)边界移点类的设计及实现

边界加点的相关操作已经完成,本文介绍另外一个类似的操作:边界移点。主要的功能演示如下:

GIS系统边界移点类的定义

在_malaPolygon.h中定义GIS边界移点的类,代码如下:

/*
*边界移点
*/
class malapolygondll CmalaPolysMovePoint :public CmalaMouseAction
{
public:
  CmalaPolysMovePoint(CView* mView, malaScreen *pScreen, CString &fileFullPath);
  virtual ~CmalaPolysMovePoint();
  void LButtonDown(UINT nFlags, malaPoint point);
  void LButtonUp(UINT nFlags, malaPoint point);
  void MouseMove(UINT nFlags, malaPoint point);
private:

  bool mSelected;
  vector<malaPoint> mSPoly;//选择的区
  malaPolyPro mSPolyPro;//选择的区的属性
  CmalaPolysSelect mSelectPoly;
  
  bool mIsDraw;
  int mPos;//标记点位置
  malaPoint mPrePoint;//前一个点
  malaPoint mNexPoint;//后一个点
  malaPoint mPerPoint;//临时点

  malaScreen *mScreen;
  CString mPath;//文件路径
};
  • m_Selected 用来定义区是否被选中
  • mSPoly 记录选择的区的点序列
  • mSPolyPro 记录选择区的属性
  • mSelectPoly 一个选择区对象,用来选择区
  • mIsDraw 标记是否按下鼠标左键
  • mPrePoint 选中点的前一个点
  • mNexPoint 选中点的后一个点
  • mPerPoint 临时的点
  • *m_Screen 屏幕坐标变换变量
  • mPath 文件路径

类的实现

在_malaPolygon.cpp中实现上述的类定义,代码如下:

/*
* 边界移点实现
*/
CmalaPolysMovePoint::CmalaPolysMovePoint(CView* mView, malaScreen *pScreen, CString &fileFullPath)
{
  mBaseView = mView;
  mPath = fileFullPath;
  mScreen = pScreen;
  CmalaPolysSelect obj(mView, pScreen, fileFullPath);
  mSelectPoly = obj;
  mSelected = FALSE;
  mIsDraw = FALSE;
  mPos = 0;
}

CmalaPolysMovePoint::~CmalaPolysMovePoint()
{
  if (mSPoly.size())
    mSPoly.clear();
}

void CmalaPolysMovePoint::LButtonDown(UINT nFlags, malaPoint point)
{
  if (!mSelected)
    mSelectPoly.LButtonDown(nFlags, point);
  else
  {
    malaLogic math;
    mPos = math.getPointPosInLine(point, mSPoly);
    if (mPos >= 0)
    {
      mIsDraw = TRUE;
      if (mPos == 0)
      {
        mPrePoint = mSPoly[mSPoly.size() - 1];
        mNexPoint = mSPoly[1];
      }
      else if (mPos == mSPoly.size() - 1)
      {
        mPrePoint = mSPoly[mPos - 1];
        mNexPoint = mSPoly[0];
      }
      else
      {
        mPrePoint = mSPoly[mPos - 1];
        mNexPoint = mSPoly[mPos + 1];
      }
      mPerPoint = mSPoly[mPos]; 
    }
  }
}

void CmalaPolysMovePoint::LButtonUp(UINT nFlags, malaPoint point)
{
  if (!mSelected)
    mSelectPoly.LButtonUp(nFlags, point);

  if (mIsDraw)
  {
    mIsDraw = FALSE;
    mSPoly[mPos] = point;

    CPolyIO lio;
    lio.polyUpdate(mSPoly, mSPolyPro, mPath);

    mSPoly.clear();
    mSelectPoly.m_Selected = FALSE;
    mBaseView->Invalidate(TRUE);
  }
  mSelected = mSelectPoly.m_Selected;
  if (mSelected)
  {
    mSPoly = mSelectPoly.mSPoly;
    mSPolyPro = mSelectPoly.mSPolyPro;
  }
}

void CmalaPolysMovePoint::MouseMove(UINT nFlags, malaPoint point)
{
  malaCDC dc(mBaseView, *mScreen);
  if (!mSelected)
    mSelectPoly.MouseMove(nFlags, point);
  if (mIsDraw)
  {
    malaLinePro m_linepro;
    m_linepro.lineColor = mSPolyPro.borderColor;
    m_linepro.lineStyle = mSPolyPro.borderStyle;
    m_linepro.lineWidth = mSPolyPro.borderWidth;
    dc.lineDrawX(mPrePoint, mPerPoint, m_linepro);
    dc.lineDrawX(mPrePoint, point, m_linepro);

    dc.lineDrawX(mNexPoint, mPerPoint, m_linepro);
    dc.lineDrawX(mNexPoint, point, m_linepro);
    mPerPoint = point;
  }
}

lineDrawX函数用于橡皮筋绘图,参考文章《VC++开发GIS系统(152)输入线之画直线的函数》。函数 polyUpdate 用于实现更新一个区的属性,可以参考文章《VC++开发GIS系统(223)移动区之更新区参数

详细代码可以参考:GitHub

VC++开发GIS系统(233)边界加点之菜单响应 VC++开发GIS系统(235)边界移点之菜单响应

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

发表评论