如何在 MFC 单文档中存储坐标

How to store coordinates in MFC-single-document?

本文关键字:文档 存储 坐标 MFC 单文档      更新时间:2023-10-16

我正在做一个绘图工具的小项目。

我使用线来绘制多边形,所以我使用CList<CPoint,CPoint> m_Points来存储每条线的终点。

这是我的代码:

void CDrawToolView::OnLButtonUp(UINT nFlags, CPoint point)
{
   .
   .
   .
   CList<CPoint,CPoint> m_Points; 
   m_Points.AddTail(point);
   . 
   .
   .
}

我想把这些点传递给一个对话框。在调用函数中:

void CDrawToolView::OnEditProperty()
{
    CPropertyDlg dlg;  
    dlg.Points = m_Points;
    if (dlg.DoModal() == IDOK)
    {   
        m_Points = dlg.Points;
    }
}

然后在对话框中,单击"确定"时,读取CList<CPoint,CPoint> Points中的所有点:

void CPropertyDlg::OnBnClickedOk()
{
    CList<CPoint,CPoint> Points; 
    Points.AddTail(polypoint);
    POSITION pos = Points.GetHeadPosition();
    while( pos != NULL )
    {
       int i = 0;
       element = Points.GetNext(pos);
       polygon_x[i] = element.x;
       polygon_y[i] = element.y;
       i ++;
    }
}

运行程序时,CObject::operator =' : cannot access private member declared in class 'CObject',如何解决这个问题?

此外,我可以使用此方法将点传递给对话框吗?

m_Points将 CPropertyDlg 的成员声明为 CList<CPoint,CPoint>* 并将指针传递给此对话框:

void CDrawToolView::OnEditProperty()
{
    CPropertyDlg dlg;  
    dlg.Points = &m_Points;
    if (dlg.DoModal() == IDOK)
    {   
        //m_Points = dlg.Points;   // not necessary because Points is changed in-place
    }
}

现有代码中的问题,即您尝试按值传递CList,这需要复制整个对象。MFC 作者不允许这样做,方法是将operator=设为私有。

顺便说一句,如果您正在尝试实现绘图功能,请查看 MFC 示例 DRAWCLI。