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

本文介绍GIS系统中移动一个区所对应类的设计及实现。先看移动区的效果:

移动区类的定义

在_malaPolygon.h中定义移动区的类,代码如下:

/*
* 移动区
*/
class malapolygondll  CmalaPolysMove :public CmalaMouseAction
{
public:
  CmalaPolysMove(CView* mView, malaScreen *pScreen, CString &fileFullPath);
  ~CmalaPolysMove();
  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;

  CString mPath;//文件路径

  bool mIsDraw;
  malaPoint mPointOri;//起点
  vector<malaPoint> mPerPoly;//临时的区
  malaScreen *mScreen;
};
  • @ mSelected 是否有区被选中
  • @ mSPoly 选中的区的点序列
  • @ mSPolyPro 选中区属性
  • @ mSelectPoly 选中的区
  • @ mPath 区文件路径
  • @ mIsDraw 记录是否按下鼠标左键(用于橡皮筋绘图)
  • @ mPointOri 移动的起点
  • @ mPerPoly 临时区的点序列
  • @ mScreen 屏幕坐标

移动区类实现

在_malaPolygon.cpp中实现上述类的定义

/*
* 移动区实现
*/
CmalaPolysMove::CmalaPolysMove(CView* mView, malaScreen *pScreen, CString &fileFullPath)
{
  mBaseView = mView;
  mPath = fileFullPath;
  mScreen = pScreen;
  CmalaPolysSelect obj(mView, pScreen, fileFullPath);
  mSelectPoly = obj;
  mSelected = FALSE;
  mIsDraw = FALSE;
}

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

void CmalaPolysMove::LButtonDown(UINT nFlags, malaPoint point)
{
  if (!mSelected)
    mSelectPoly.LButtonDown(nFlags, point);
  else
  {
    mIsDraw = TRUE;
    mPointOri = point;
  }
}

void CmalaPolysMove::LButtonUp(UINT nFlags, malaPoint point)
{
  if (!mSelected)
    mSelectPoly.LButtonUp(nFlags, point);
  else
  {
    CPolyIO lio;
    lio.polyUpdate(mPerPoly, mSPolyPro, mPath);
    mBaseView->Invalidate(TRUE);
    mIsDraw = FALSE;
    mSelected = FALSE;
    mSelectPoly.m_Selected = FALSE;
    mSPoly.clear();
  }

  mSelected = mSelectPoly.m_Selected;
  if (mSelected)
  {
    this->mSPoly = mSelectPoly.mSPoly;
    this->mSPolyPro = mSelectPoly.mSPolyPro;
    mPerPoly = mSPoly;
  }

}

void CmalaPolysMove::MouseMove(UINT nFlags, malaPoint point)
{
  if (!mSelected)
    mSelectPoly.MouseMove(nFlags, point);
  else if (mIsDraw)
  {
    malaCDC dc(mBaseView, *mScreen);
    dc.polyDrawAutoX(mPerPoly, mSPolyPro);
    for (size_t i = 0; i < mPerPoly.size(); i++)
    {
      mPerPoly[i].x = mSPoly[i].x + point.x - mPointOri.x;
      mPerPoly[i].y = mSPoly[i].y + point.y - mPointOri.y;
    }
    dc.polyDrawAutoX(mPerPoly, mSPolyPro);
  }
}

其中 polyUpdate 函数参考《VC++开发GIS系统(223)移动区之更新区参数》,polyDrawAutoX 函数参考《VC++开发GIS系统(208)输入区之绘制区的函数(多边形与椭圆)

详细代码可以参考:GitHub

VC++开发GIS系统(223)移动区之更新区参数 VC++开发GIS系统(225)移动区之菜单响应

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

发表评论