我是菜鸟,有个问题不懂,大家不要笑我哦!第一次发帖子,不懂规矩,不好意思。
有一个小问题,希望学长指点,打扰休息,实在抱歉
一个简单得绘图程序
先用MFC AppWizard[exe]建了个SDI的MyDraw。
在CMyDrawView添加
protected:
CPoint m_ptOrigin; //初始点坐标
bool m_bDragging; //拖拽标记
HCURSOR m_hCross; //光标句柄
再构造函数里设置拖拽标记合十字光标。
CMyDrawView::CMyDrawView()
{
// TODO: add construction code here
m_bDragging=false; //初始化拖拽标记
m_hCross=AfxGetApp()->LoadStandardCursor(IDC_CROSS); //获得十字光标句柄
}
自添加消息处理函数
void CMyDrawView::OnLButtonDown(UINT nFlags, CPoint point)
{
// TODO: Add your message handler code here and/or call default
SetCapture(); //捕捉鼠标
::SetCursor(m_hCross); //设置十字光标
m_ptOrigin=point;
m_bDragging=true; //设置拖拽标记
// CView::OnLButtonDown(nFlags, point);
}
void CMyDrawView::OnMouseMove(UINT nFlags, CPoint point)
{
// TODO: Add your message handler code here and/or call default
if(m_bDragging)
{
CClientDC dc(this);
dc.MoveTo(m_ptOrigin);
dc.LineTo(point); //绘制线段
m_ptOrigin=point; //新的起始点
}
// CView::OnMouseMove(nFlags, point);
}
void CMyDrawView::OnLButtonUp(UINT nFlags, CPoint point)
{
// TODO: Add your message handler code here and/or call default
if(m_bDragging)
{
m_bDragging=false; //清拖拽标记
ReleaseCapture(); //释放鼠标,还原鼠标形状
}
// CView::OnLButtonUp(nFlags, point);
}
初始化中设置标题。
BOOL CMyDrawApp::InitInstance()
{
AfxEnableControlContainer();
......
m_pMainWnd->UpdateWindow();
m_pMainWnd->SetWindowText("刘荻给彭茜画的图。"); //设置标题栏
return TRUE;
}
但问题出现再后面,为了再重绘窗口时能显示已绘制的线段。
新建了类CLine:
class CLine //自填加的线段类
{
private:
//定义成员变量,表示一条直线起点的坐标
CPoint m_pt1;
CPoint m_pt2;
public:
CLine();
virtual ~CLine();
CLine(CPoint pt1,CPoint pt2);//定义线段的构造函数
void DrawLine(CDC *pDC); //定义绘制线段的成员函数
};
CLine::CLine(CPoint pt1,CPoint pt2)
{
m_pt1=pt1;
m_pt2=pt2;
}
自定义画线函数
void CLine::DrawLine(CDC*pDC)
{
pDC->MoveTo(m_pt1);
pDC->LineTo(m_pt2);
}
在CMyDrawDoc中定义变量
#include"Line.h"//添加的头文件
#include<afxtempl.h>//
class CMyDrawDoc : public CDocument
{
......
protected:
CTypedPtrArray<CObArray,CLine*> m_LineArray;//存放线段对象指针的动态数组。
public:
CLine*GetLine(int nIndex); //获取指定序号线段对象的指针
void AddLine(CPoint pt1,CPoint pt2);//向动态数组中添加新的线段对象的指针
int GetNumLines(); //获取线段的数量
......
};
在实现文件中编写了
void CMyDrawDoc::AddLine(CPoint pt1, CPoint pt2)
{
CLine *pLine=new CLine(pt1,pt2); //新建一条线段对象
m_LineArray.Add(pLine); //将该线段对象加到动态数组
}
在编译的时候出现了下面的错误报告,我也不知道为什么会这样,Add后面明明该加CLine*类型的,
可是它怎么会说出错呢?而且pLine时该类型啊?你能帮我看看吗?
--------------------Configuration: MyDraw - Win32 Debug--------------------
Compiling...
Line.cpp
MyDraw.cpp
MyDrawDoc.cpp
e:\microsoft visual studio\vc98\mfc\include\afxtempl.h(1539) : error C2664: 'Add' : cannot convert parameter 1 from 'class CLine *' to 'class CObject *'
Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast
e:\microsoft visual studio\vc98\mfc\include\afxtempl.h(1539) : while compiling class-template member function 'int __thiscall CTypedPtrArray<class CObArray,class CLine *>::Add(class CLine *)'
MyDrawView.cpp
Generating Code...
Error executing cl.exe.