首先我們說下通過鼠標的移動來繪制直線。
這裡要捕獲到鼠標的兩個消息: WM_LBUTTONDOWN, WM_LBUTTONUP。響應WM_LBUTTONDOWN消息記錄直線的起始點,響應 WM_LBUTTONUP消息記錄直線的終點並繪制直線。
好了,我們看看怎麼響應。
void CDrawView::OnLButtonDown(UINT nFlags, CPoint point)
{
//MessageBox("Left Button Clicks DrawView");
m_ptOrigin = point ; //這裡先定義了一個內部變量保存直線的起始點
CView::OnLButtonDown(nFlags, point);
}
下面看看響應WM_LBUTTONUP消息記錄直線的終點並繪制直線。
//方法一
void CDrawView::OnLButtonUp(UINT nFlags, CPoint point)
{
HDC hdc;
hdc = ::GetDC(m_hWnd); //調用全局函數
MoveToEx (hdc,m_ptOrigin.x,m_ptOrigin.y,0);
LineTo(hdc,point.x,point.y);
::ReleaseDC(m_hWnd,hdc);
CView::OnLButtonUp(nFlags, point);
}
//方法二
void CDrawView::OnLButtonUp(UINT nFlags, CPoint point)
{
CDC *pDC = GetDC();
pDC->MoveTo(m_ptOrigin);
pDC->LineTo (point);
ReleaseDC(pDC);
}
//方法三
void CDrawView::OnLButtonUp(UINT nFlags, CPoint point)
{
CClientDC dc (this) ; //CClientDC對象構造的時候就調用了GetDC,析構的時候調用
ReleaseDC ,只能訪問客戶區
CClientDC dc(GetParent());
dc.MoveTo (m_ptOrigin);
dc.LineTo(point);
}
//方法四
void CDrawView::OnLButtonUp(UINT nFlags, CPoint point)
{
CWindowDC dc (this) ; //CWindowDC可以訪問客戶區和非客戶區
dc.MoveTo(m_ptOrigin);
dc.LineTo(point);
}
void CDrawView::OnLButtonUp(UINT nFlags, CPoint point)
{
CWindowDC dc(GetDesktopWindow()) ; //現在可以訪問桌面
dc.MoveTo(m_ptOrigin);
dc.LineTo(point);
}
連續線繪圖:
思路:鼠標移動的信號被捕捉到,然後隨時響應該信號,繪制圖線。
這裡要 設置一個BOOL變量 m_bDraw 來判斷是否鼠標左鍵按下了。
//畫連續的線條
void CDrawView::OnMouseMove(UINT nFlags, CPoint point)
{
if ( m_bDraw )
{
CClientDC dc(this) ;
dc.MoveTo(m_ptOrigin); //移動到原來的點
dc.LineTo(point); //繪制直線
m_ptOrigin = point ; //將現在的點賦值給原來的坐標,以便下次調用
}
CView::OnMouseMove(nFlags, point);
}
//改變畫筆的顏色
void CDrawView::OnMouseMove(UINT nFlags, CPoint point)
{
if ( m_bDraw )
{
CClientDC dc(this) ;
CPen pen( PS_SOLID , 1 , RGB (255,128,128) );
CPen *pOldPen = dc.SelectObject(&pen);
dc.MoveTo(m_ptOrigin);
dc.LineTo(point);
m_ptOrigin = point ;
dc.SelectObject(pOldPen);
}
CView::OnMouseMove(nFlags, point);
}
//畫扇型
void CDrawView::OnMouseMove(UINT nFlags, CPoint point)
{
if ( m_bDraw )
{
CClientDC dc(this) ;
CPen pen( PS_SOLID , 1 , RGB(255,128,128) );
CPen *pOldPen = dc.SelectObject(&pen);
dc.MoveTo(m_ptOrigin);
dc.LineTo (m_ptOld);
dc.MoveTo(m_ptOrigin);
dc.LineTo(point);
m_ptOld = point ;
dc.SelectObject(pOldPen);
}
CView::OnMouseMove(nFlags, point);
}
//畫帶邊線的扇型
void CDrawView::OnMouseMove(UINT nFlags, CPoint point)
{
if ( m_bDraw )
{
CClientDC dc(this) ;
CPen pen( PS_SOLID , 1 , RGB (255,128,128) );
CPen *pOldPen = dc.SelectObject(&pen);
dc.MoveTo(m_ptOrigin);
dc.LineTo(m_ptOld);
dc.MoveTo (m_ptOrigin);
dc.LineTo(point);
dc.MoveTo(m_ptOld);
dc.LineTo(point);
m_ptOld = point ;
dc.SelectObject (pOldPen);
}
CView::OnMouseMove(nFlags, point);
}
//看看 繪圖的模式設置方法
void CDrawView::OnMouseMove(UINT nFlags, CPoint point)
{
if ( m_bDraw )
{
CClientDC dc(this) ;
dc.SetROP2(R2_BLACK); //繪圖的模式設置,始終繪制黑色圖形
CPen pen( PS_SOLID , 1 , RGB(255,128,128) );
CPen *pOldPen = dc.SelectObject (&pen);
dc.MoveTo(m_ptOrigin);
dc.LineTo(m_ptOld);
dc.MoveTo(m_ptOrigin);
dc.LineTo(point);
dc.MoveTo (m_ptOld);
dc.LineTo(point);
m_ptOld = point ;
dc.SelectObject(pOldPen);
}
CView::OnMouseMove(nFlags, point);
}
到此,我們知道了一般地繪圖方法了。