天天看點

繪圖基礎--使用畫筆和畫刷繪制網絡

繪圖基礎--使用畫筆和畫刷繪制網絡

繪圖基礎--使用畫筆和畫刷繪制網絡
// rect.cpp

#include <afxwin.h>

// Define the application class
class CApp : public CWinApp
{
public:
	virtual BOOL InitInstance();
};

CApp App;  

// define the window class
class CWindow : public CFrameWnd
{ 
public:
	CWindow(); 
	void OnPaint();
	DECLARE_MESSAGE_MAP()
};

// The window's constructor
CWindow::CWindow()
{ 
	Create(NULL, "Drawing Tests",
		WS_OVERLAPPEDWINDOW,
		CRect(0,0,500,400)); 
}

// The message map
BEGIN_MESSAGE_MAP( CWindow, CFrameWnd )
	ON_WM_PAINT()	
END_MESSAGE_MAP()

// Handle exposures
void CWindow::OnPaint()
{
	CRect rect;

	GetClientRect( rect );
	CPaintDC dc(this);
	
	// 建立畫筆(實線,2像素,藍色)
	CPen pen(PS_SOLID, 2, RGB(0,0,255)), *oldPen;
	oldPen = dc.SelectObject(&pen);
	
	// 建立畫刷(水準和垂直網格,紅色)
	CBrush brush(HS_CROSS,RGB(255,0,0)), *oldBrush;
	oldBrush = dc.SelectObject(&brush);
	
	// 使用目前筆繪制矩形,用目前畫刷填充
	rect.InflateRect(-20, -20);		
	dc.Rectangle(rect);  

	// return old pen and brush
	dc.SelectObject(oldPen);
	dc.SelectObject(oldBrush);
}

// Init the application
BOOL CApp::InitInstance()
{
	m_pMainWnd = new CWindow();
	m_pMainWnd->ShowWindow(m_nCmdShow);
	m_pMainWnd->UpdateWindow();
	return TRUE;
}