读书人

C++实用技巧八

发布时间: 2008-12-11 20:22:13 作者: liuhuituzi

一.画直线:

  步骤一:在视图类中对WM_LBUTTONDOWN和WM_LBUTTONUP消息添加消息响应函数OnLButtonDown和OnLButtonUp

  步骤二:在视图类中利用添加成员向导添加成员变量。名字,例如m_StartPoint,类型为CPoint,访问属性设置为protected

  步骤三:在OnLButtonDown和OnLButtonUp 中写如下代码:

  void CswdfView::OnLButtonDown(UINT nFlags, CPoint point)

  {

  // TODO: Add your message handler code here and/or call default

  m_StartPoint=point;

  CView::OnLButtonDown(nFlags, point);

  }

  void CswdfView::OnLButtonUp(UINT nFlags, CPoint point)

  {

  // TODO: Add your message handler code here and/or call default

  //第一种,使用HDC和API函数

  /*HDC hdc;

  hdc=::GetDC(m_hWnd);

  ::MoveToEx(hdc,m_StartPoint.x,m_StartPoint.y,NULL);

  ::LineTo(hdc,point.x,point.y);

  ::ReleaseDC(m_hWnd,hdc);

  CView::OnLButtonUp(nFlags, point);*/

  //第二种,使用CDC类

  /*CDC *pDC=GetDC();

  pDC->MoveTo(m_StartPoint);

  pDC->LineTo(point);

  ReleaseDC(pDC);*/

  //第三种,使用CClientDC

  CClientDC aDC(this);

  aDC.MoveTo(m_StartPoint);

  aDC.LineTo(point);

  }

  OK,运行程序,可以画直线了。

  二.画曲线

  步骤一:按照画直线中介绍的方法在视图类中添加对WM_MOUSEMOVE消息的响应函数OnMouseMove

  步骤二:在OnMouseMove中写如下代码:

  void CswdfView::OnMouseMove(UINT nFlags, CPoint point)

  {

  // TODO: Add your message handler code here and/or call default

  if(nFlags==MK_LBUTTON) //判断鼠标左键是否按下,如果按下,则移动时画线

  {

  CClientDC aDC(this);

  aDC.MoveTo(m_StartPoint);

  aDC.LineTo(point);

  m_StartPoint=point; //将画线的起点移动到鼠标移动后的点

  }

  CView::OnMouseMove(nFlags, point);

  }

3COME考试频道为您精心整理,希望对您有所帮助,更多信息在http://www.reader8.net/exam/

读书人网 >复习指导

热点推荐