VC++开发GIS系统(176)复制线之类的设计及实现

前文《VC++开发GIS系统(174)移动线之类的设计及实现》和《VC++开发GIS系统(175)移动线之菜单响应》已经实现了移动一条线,本文介绍复制线类的定义及实现。其实复制线和移动线的操作相同,不过是复制线最后调用的不是更新线属性的函数,而是增加线的函数。

类的定义

在_malaLines.h中定义复制线的类,代码如下:

/*
* 复制线
*/
class malalinesdll  CmalaLinesCopy :public CmalaMouseAction
{
public:
    CmalaLinesCopy(CView* mView, malaScreen *pScreen, CString &fileFullPath);
    ~CmalaLinesCopy();
    void LButtonDown(UINT nFlags, malaPoint point);
    void LButtonUp(UINT nFlags, malaPoint point);
    void MouseMove(UINT nFlags, malaPoint point);
private:
    bool m_Selected;
    vector<malaPoint> mSLine;//选择的线
    malaLinePro mSLinePro;//选择的点的属性
    CmalaLinesSelect m_SelectLine;

    CString mPath;//文件路径

    bool m_bDraw;
    malaPoint m_ptOrigin;//起点
    vector<malaPoint> m_perLine;//临时线
    malaScreen *m_Screen;
};

m_Selected用来定义线是否被选中,mSLine记录选择的线,mSLinePro记录选择线的属性,m_SelectLine一个选择线对象,用来选择线。m_bDraw用来标记橡皮筋绘图,m_ptOrigin用来计算移动的偏移量。m_perLine一条临时的线,用于橡皮筋绘图。

类的实现

在_malaLines.cpp中实现上述类,代码如下:

/*
* 复制线实现
*/
CmalaLinesCopy::CmalaLinesCopy(CView* mView, malaScreen *pScreen, CString &fileFullPath)
{
    mBaseView = mView;
    mPath = fileFullPath;
    m_Screen = pScreen;
    CmalaLinesSelect obj(mView, pScreen, fileFullPath);
    m_SelectLine = obj;
    m_Selected = FALSE;
    m_bDraw = FALSE;
}

CmalaLinesCopy::~CmalaLinesCopy()
{
    if (mSLine.size())
        mSLine.clear();
}

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

void CmalaLinesCopy::LButtonUp(UINT nFlags, malaPoint point)
{
    if (!m_Selected)
        m_SelectLine.LButtonUp(nFlags, point);
    else
    {
        CLineIO lio;
        lio.lineAdd(m_perLine, mSLinePro, mPath);
        mBaseView->Invalidate(TRUE);
        m_bDraw = FALSE;
        m_Selected = FALSE;
        m_SelectLine.m_Selected = FALSE;
        mSLine.clear();
    }

    m_Selected = m_SelectLine.m_Selected;
    if (m_Selected)
    {
        this->mSLine = m_SelectLine.mLine;
        this->mSLinePro = m_SelectLine.mLinePro;
        m_perLine = mSLine;
    }

}

void CmalaLinesCopy::MouseMove(UINT nFlags, malaPoint point)
{
    if (!m_Selected)
        m_SelectLine.MouseMove(nFlags, point);
    else if (m_bDraw)
    {
        malaCDC dc(mBaseView, *m_Screen);
        dc.lineDrawAllX(m_perLine, mSLinePro);
        for (size_t i = 0; i < m_perLine.size(); i++)
        {
            m_perLine[i].x = mSLine[i].x + point.x - m_ptOrigin.x;
            m_perLine[i].y = mSLine[i].y + point.y - m_ptOrigin.y;
        }
        dc.lineDrawAllX(m_perLine, mSLinePro);
        dc.lineDrawAll(mSLine, mSLinePro);
    }
}

文中调用的lineAdd函数可以参考文章《VC++开发GIS系统(157)输入线之保存线函数

详细代码可以参考:GitHub

VC++开发GIS系统(175)移动线之菜单响应 VC++开发GIS系统(177)复制线之菜单响应

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

发表评论