如何用c++编一个简单画图程序

2025-06-27 10:30:27
推荐回答(5个)
回答1:

============================================================
//在构造函数里添加以下两句:
//加载十字光标
m_hCrossCur = AfxGetApp()->LoadStandardCursor(IDC_CROSS);
//加载箭头光标
m_hArrowCur = AfxGetApp()->LoadStandardCursor(IDC_ARROW);
============================================================

============================================================
//以下是画直线的代码:
void CMyProject4View::OnLButtonDown(UINT nFlags, CPoint point)
{
// TODO: Add your message handler code here and/or call default
m_bLButtonDown = TRUE; //鼠标左键按下
m_ptStart = point; //直线的起点
m_ptEnd = point; //直线的临时端点
SetCapture();
SetCursor(m_hCrossCur);//设置鼠标捕捉
CView::OnLButtonDown(nFlags, point);
}

void CMyProject4View::OnLButtonUp(UINT nFlags, CPoint point)
{
// TODO: Add your message handler code here and/or call default
if(m_bLButtonDown)
{
m_bLButtonDown = FALSE; //鼠标左键释放
ReleaseCapture(); //释放鼠标捕捉
CClientDC dc(this);//创建客户区设备环境
dc.MoveTo(m_ptStart);
dc.LineTo(point);
SetCursor(m_hArrowCur);
CView::OnLButtonUp(nFlags, point);
}
}
void CMyProject4View::OnMouseMove(UINT nFlags, CPoint point)
{
// TODO: Add your message handler code here and/or call default
if(m_bLButtonDown)
{
CClientDC dc(this);
dc.SetROP2(R2_NOT);
dc.MoveTo(m_ptStart); //擦除从起点到上个鼠标移动点间的直线
dc.LineTo(m_ptEnd);
dc.MoveTo(m_ptStart); //绘制从起点到当前点间的直线
dc.LineTo(point);
m_ptEnd = point; //保存当前鼠标位置
}
CView::OnMouseMove(nFlags, point);

}
============================================================

============================================================
以下是画圆的代码:
void CMyProject4View::OnPaint()
{
CPaintDC dc(this); // device context for painting

// TODO: Add your message handler code here
CRect rcClient;
GetClientRect (&rcClient);
int cx=rcClient.Width() /2;
int cy=rcClient.Height() /2;
CRect rc(cx-45,cy-45,cx+45,cy+45);
CBrush brush(RGB(0,250,250));

CBrush *poldbrush=dc.SelectObject(&brush);
dc.Ellipse(rc);
}
// Do not call CView::OnPaint() for painting messages
}
============================================================

===========================================================

回答2:

孙鑫的vc视频教程第4、10、11集有演示这个内容。

回答3:

快速点的话用mfc的GDI绘图
封装的控件和windows自带的画板差不多。

回答4:

这是画不了的,你说的这就不是编程了,是画图平台了

回答5:

可以用MFC来做,比较简单!需要的话留下邮箱,给你发一份源代码~