工具:
http://blog.csdn.net/bcbobo21cn/article/details/44200205
demo工程:
http://pan.baidu.com/s/1qYCE9rQ
新建一个工程
更改窗口标题;
添加工具栏图标;
添加工具栏按钮的状态栏提示;
添加工具栏按钮的事件函数,此时需要手动添加;
视类头文件手动添加
//{{AFX_MSG(CDzyjView)
afx_msg void OnLButtonDown(UINT nFlags, CPoint point);
afx_msg void OnDrawdz(); //手动添加
afx_msg void OnDrawdr(); //手动添加
afx_msg void OnDrawdg(); //手动添加
afx_msg void OnDrawerjg(); //手动添加
//}}AFX_MSG
视类实现文件添加
BEGIN_MESSAGE_MAP(CDzyjView, CView)
//{{AFX_MSG_MAP(CDzyjView)
ON_WM_LBUTTONDOWN()
//}}AFX_MSG_MAP
// Standard printing commands
ON_COMMAND(ID_FILE_PRINT, CView::OnFilePrint)
ON_COMMAND(ID_FILE_PRINT_DIRECT, CView::OnFilePrint)
ON_COMMAND(ID_FILE_PRINT_PREVIEW, CView::OnFilePrintPreview)
ON_COMMAND(ID_BUTTONdz, CDzyjView::OnDrawdz) //手动添加
ON_COMMAND(ID_BUTTONdr, CDzyjView::OnDrawdr) //手动添加
ON_COMMAND(ID_BUTTONdg, CDzyjView::OnDrawdg) //手动添加
ON_COMMAND(ID_BUTTONerjg, CDzyjView::OnDrawerjg) //手动添加
END_MESSAGE_MAP()
新加工具栏按钮的响应函数中设定绘制元件类别
void CDzyjView::OnDrawdz()
{
//MessageBox("dz","提示",MB_OK);
yjtype=1;
}
视类头文件添加成员变量和函数
private:
CPoint point1;
int yjtype;
CPoint Four_Point[4];
// Operations
public:
void Drawdz(CDC *pDC);
void Drawdr(CDC *pDC);
void Drawdg(CDC *pDC);
void Drawerjg(CDC *pDC);
添加鼠标单击响应函数
鼠标左键按下时,获取绘制点中心坐标;刷新窗口;
void CDzyjView::OnLButtonDown(UINT nFlags, CPoint point)
{
// TODO: Add your message handler code here and/or call default
point1.x=point.x;
point1.y=point.y;
Invalidate();
CView::OnLButtonDown(nFlags, point);
}
画电阻函数
void CDzyjView::Drawdz(CDC *pDC)
{
pDC->MoveTo(point1.x-20,point1.y);
pDC->LineTo(point1.x-12,point1.y);
pDC->MoveTo(point1.x+20,point1.y);
pDC->LineTo(point1.x+12,point1.y);
Four_Point[0].x=point1.x-12;
Four_Point[0].y=point1.y-5;
Four_Point[1].x=point1.x+12;
Four_Point[1].y=point1.y-5;
Four_Point[2].x=point1.x+12;
Four_Point[2].y=point1.y+5;
Four_Point[3].x=point1.x-12;
Four_Point[3].y=point1.y+5;
pDC->Polygon(Four_Point,4);
//pDC->Polyline(Four_Point,4);
/*
pDC->MoveTo(Four_Point[0]);
pDC->LineTo(Four_Point[1]);
pDC->MoveTo(Four_Point[1]);
pDC->LineTo(Four_Point[2]);
pDC->MoveTo(Four_Point[2]);
pDC->LineTo(Four_Point[3]);
pDC->MoveTo(Four_Point[3]);
pDC->LineTo(Four_Point[0]);
*/
}
在OnDraw中调用画电阻函数
void CDzyjView::OnDraw(CDC* pDC)
{
CDzyjDoc* pDoc = GetDocument();
ASSERT_VALID(pDoc);
// TODO: add draw code for native data here
switch(yjtype)
{
case 1:
Drawdz(pDC);
break;
case 2:
break;
default:
break;
}
}
看下效果;
关于 local function definitions are illegal 错误
F:\vc6prj\图形图像\dzyj\dzyjView.cpp(80) : error C2601: 'OnPreparePrinting' : local function definitions are illegal
......
执行 cl.exe 时出错.
编译时出现一堆 local function definitions are illegal 错误;一个可能原因是丢失了反大括号;
void CDzyjView::OnDraw(CDC* pDC)
{
CDzyjDoc* pDoc = GetDocument();
ASSERT_VALID(pDoc);
// TODO: add draw code for native data here
switch(yjtype)
{
case 1:
Drawdz(pDC);
break;
case 2:
break;
default:
break;
}
} // 丢失此大括号会产生 local function definitions are illegal
今天先到这里......