创建一个单档MFC应用
//-----------------------------------------------------------------------------------------------------------------------
// 利用SDK全局函数 HDC 画线
// 鼠标左键按下
void CDrawView::OnLButtonDown(UINT nFlags, CPoint point)
{
m_ptOrigin = point; // 保留鼠标左键按下的点的坐标
CView::OnLButtonDown(nFlags, point);
}
// 鼠标左键弹起
void CDrawView::OnLButtonUp(UINT nFlags, CPoint point)
{
HDC hdc;
hdc = ::GetDC(m_hWnd);
MoveToEx(hdc,m_ptOrigin.x,m_ptOrigin.y,NULL);
LineTo(hdc,point.x,point.y);
::ReleaseDC(m_hWnd,hdc); // 释放hdc
CView::OnLButtonUp(nFlags, point);
}
//-----------------------------------------------------------------------------------------------------------------------
// 利用 MFC 的 CDC 类实现画线
// 鼠标左键按下
void CDrawView::OnLButtonDown(UINT nFlags, CPoint point)
{
m_ptOrigin = point; // 保留鼠标左键按下的点的坐标
CView::OnLButtonDown(nFlags, point);
}
// 鼠标左键弹起
void CDrawView::OnLButtonUp(UINT nFlags, CPoint point)
{
CDC *pDC = GetDC();
pDC->MoveTo(m_ptOrigin);
pDC->LineTo(point);
ReleaseDC(pDC);
CView::OnLButtonUp(nFlags, point);
}
//-----------------------------------------------------------------------------------------------------------------------
// 利用 CClientDC 进行画线
// 鼠标左键按下
void CDrawView::OnLButtonDown(UINT nFlags, CPoint point)
{
m_ptOrigin = point; // 保留鼠标左键按下的点的坐标
CView::OnLButtonDown(nFlags, point);
}
// 鼠标左键弹起
void CDrawView::OnLButtonUp(UINT nFlags, CPoint point)
{
CClientDC dc(this);
dc.MoveTo(m_ptOrigin);
dc.LineTo(point);
CView::OnLButtonUp(nFlags, point);
}
//-----------------------------------------------------------------------------------------------------------------------
// 利用 CWindowDC 进行画线
// 鼠标左键按下
void CDrawView::OnLButtonDown(UINT nFlags, CPoint point)
{
m_ptOrigin = point; // 保留鼠标左键按下的点的坐标
CView::OnLButtonDown(nFlags, point);
}
// 鼠标左键弹起
void CDrawView::OnLButtonUp(UINT nFlags, CPoint point)
{
CWindowDC dc(this);
dc.MoveTo(m_ptOrigin);
dc.LineTo(point);
CView::OnLButtonUp(nFlags, point);
}