利用MFC進行簡單繪圖

一、利用幾個函數繪製直線

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);
				}
			}
		}
}

電腦壞了送去修了,修好了再把詳細的過程寫上,現在是基本代碼。

留個坑。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章