1.利用 HDC类绘制直线。
void COOPEx5View::OnLButtonDown(UINT nFlags, CPoint point)
{
m_point = point;
CView::OnLButtonDown(nFlags, point);
}
void COOPEx5View::OnLButtonUp(UINT nFlags, CPoint point)
{
HDC hdc;
hdc = ::GetDC(m_hWnd);
MoveToEx(hdc, m_point.x, m_point.y, NULL);
LineTo(hdc, point.x, point.y);
::ReleaseDC(m_hWnd, hdc);
CView::OnLButtonUp(nFlags, point);
}
void COOPEx5View::OnPaint()
{
CPaintDC dc(this);
}
2.利用CDC类绘制直线。
void COOPEx5View::OnLButtonUp(UINT nFlags, CPoint point)
{
CDC* pDC = GetDC();
pDC->MoveTo(m_point);
pDC->LineTo(point);
ReleaseDC(pDC);
CView::OnLButtonUp(nFlags, point);
}
3.利用CWindon在窗口绘制直线。
void COOPEx5View::OnLButtonUp(UINT nFlags, CPoint point)
{ CWindowDC dc(GetParent());
dc.MoveTo(m_point);
dc.LineTo(point);
CView::OnLButtonUp(nFlags, point);
}
void COOPEx5View::OnLButtonUp(UINT nFlags, CPoint point)
{ CBrush brush(RGB(255, 0, 0));
CClientDC dc(this);
dc.FillRect(CRect(m_point, point), &brush);
CView::OnLButtonUp(nFlags, point);
}
void COOPEx5View::OnPaint()
{
CPaintDC dc(this); // device context for painting
CDC *pdc;
pdc = GetDC();
CPen pen(PS_SOLID, 2, RGB(0, 0, 0));
CPen *pOldpen = pdc->SelectObject(&pen);//获取指针
int Ox = 100;
int Oy = 100;
int x = 40;
for (int i = 0; i < 9; i++)//画列线
{
pdc->MoveTo(Ox + x * i, Oy);
pdc->LineTo(Ox + x * i, x*8+100);
}
for (int j = 0; j < 9; j++)//画行线
{
pdc->MoveTo(100, Oy + x * j);
pdc->LineTo(x*8+100, Oy + x * j);
}
CBrush brush2(RGB(0, 0, 0));
for (int p = 0; p < 8; p = p + 1)
{
if (p % 2 == 0)
{
for (int q = 1; q < 8; q = q + 2)
{
CRect rc(Ox + x * q, Oy + p * x, Ox + x + x * q, Oy + x + p * x);
pdc->FillRect(rc, &brush2);
}
}
else
{
for (int q = 0; q < 8; q = q + 2)
{
CRect rc(Ox + x * q, Oy + p * x, Ox + x + x * q, Oy + x + p * x);
pdc->FillRect(rc, &brush2);
}
}
}
}
电脑坏了送去修了,修好了再把详细的过程写上,现在是基本代码。
留个坑。