Visual C++游戏编程基础之画笔、画刷的使用

一、概念

画笔:线条的样式

画刷:封闭图形内部填充的样式

二、重要函数介绍

1.HPEN CreatePen(int nPenStyle, int nWidth, COLORREF crColor);

   function:指定的样式、宽度和颜色创建画笔

   nPenStyle:实线、虚线、点线等等

   nWidth:线宽

   crColor:颜色

2.HBRUSH CreateHatchBrush(int fnStyle, COLORREF clrref);

   function:建立阴影画刷

   fnStyle:指定刷子阴如式,如----、++++等等

   cirref:指定用于阴影刷子的前景色

3.CreateSolidBrush(COLORREF clrref);

   function:创建一个具有指定颜色的逻辑刷子

4.HGDIOBJ SelectObject(HDC hdc, HGDIOBJ hgdiobj);

   function:选择一对象到指定的设备上下文环境中,该新对象替换先前的相同类型的对象

5.WINGDIAPI BOOL WINAPI MoveToEx(HDC hdc,int X,int Y,LPPOINT lpPoint);

  function:将当前绘图位置移动到某个具体的点

  hdc:设备上下文句柄

  x,y:新位置的坐标

  lpPoint:存放上一个点的位置

6.WINGDIAPI BOOL WINAPI LineTo(HDChdc,intX,intY,);

   function:用当前画笔画一条线,从当前位置连到一个指定的点

7.BOOL Rectangle(HDC hdc, int nLeftRect, int nTopRect, int nRightRect, int nBottomRect);

   function:画一个矩形

三、实现代码


#include "stdafx.h"


HINSTANCE hInst;
HPEN hPen[7];//定义画笔
HBRUSH hBru[7];//定义画刷
//画笔数组包含7种样式
int sPen[7] = {PS_SOLID,PS_DASH,PS_DOT,PS_DASHDOT,PS_DASHDOTDOT,PS_NULL,PS_INSIDEFRAME};
//阴影画刷数组包含6种样式
int sBru[6] = {HS_VERTICAL,HS_HORIZONTAL,HS_CROSS,HS_DIAGCROSS,HS_FDIAGONAL,HS_BDIAGONAL};

ATOM				MyRegisterClass(HINSTANCE hInstance);
BOOL				InitInstance(HINSTANCE, int);
LRESULT CALLBACK	WndProc(HWND, UINT, WPARAM, LPARAM);
void				MyPaint(HDC hdc);


int APIENTRY WinMain(HINSTANCE hInstance,
                     HINSTANCE hPrevInstance,
                     LPSTR     lpCmdLine,
                     int       nCmdShow)
{
	MSG msg;

	MyRegisterClass(hInstance);


	if (!InitInstance (hInstance, nCmdShow)) 
	{
		return FALSE;
	}


	while (GetMessage(&msg, NULL, 0, 0)) 
	{
		TranslateMessage(&msg);
		DispatchMessage(&msg);
	}

	return msg.wParam;
}


ATOM MyRegisterClass(HINSTANCE hInstance)
{
	WNDCLASSEX wcex;

	wcex.cbSize = sizeof(WNDCLASSEX); 
	wcex.style			= CS_HREDRAW | CS_VREDRAW;
	wcex.lpfnWndProc	= (WNDPROC)WndProc;
	wcex.cbClsExtra		= 0;
	wcex.cbWndExtra		= 0;
	wcex.hInstance		= hInstance;
	wcex.hIcon			= NULL;
	wcex.hCursor		= NULL;
	wcex.hCursor		= LoadCursor(NULL, IDC_ARROW);
	wcex.hbrBackground	= (HBRUSH)(COLOR_WINDOW+1);
	wcex.lpszMenuName	= NULL;
	wcex.lpszClassName	= "canvas";
	wcex.hIconSm		= NULL;

	return RegisterClassEx(&wcex);
}


BOOL InitInstance(HINSTANCE hInstance, int nCmdShow)
{
	HWND hWnd;
	HDC hdc;
	int i;

	hInst = hInstance;

	hWnd = CreateWindow("canvas", "绘图窗口" , WS_OVERLAPPEDWINDOW,
		CW_USEDEFAULT, 0, CW_USEDEFAULT, 0, NULL, NULL, hInstance, NULL);

	if (!hWnd)  
	{
		return FALSE;
	}

	MoveWindow(hWnd,10,10,650,350,true);
	ShowWindow(hWnd, nCmdShow);
	UpdateWindow(hWnd);

	for(i=0;i<=6;i++)
	{
		hPen[i] = CreatePen(sPen[i],1,RGB(255,0,0));//颜色为红色
		if(i==6)
			hBru[i] = CreateSolidBrush(RGB(0,255,0));//建立绿色画刷
		else
			hBru[i] = CreateHatchBrush(sBru[i],RGB(0,255,0));//建立6种阴影画刷
	}

	hdc = GetDC(hWnd);//成功,返回指定窗口客户区的设备上下文环境;失败,返回值为Null
	MyPaint(hdc);
	ReleaseDC(hWnd,hdc);

	return TRUE;
}


void MyPaint(HDC hdc)
{
	int i,x1,x2,y;
	

	for(i=0;i<=6;i++)
	{
		y = (i+1) * 30;

		SelectObject(hdc,hPen[i]);//该函数选择一对象到指定的设备上下文环境中,该新对象替换先前的相同类型的对象
		MoveToEx(hdc,30,y,NULL);	 //移动画笔至线条起点
		LineTo(hdc,100,y);			 //绘制线条至指定坐标
	}
	
	x1 = 120;
	x2 = 180;

	for(i=0;i<=6;i++)
	{
		SelectObject(hdc,hBru[i]);
		Rectangle(hdc,x1,30,x2,y);	 //绘制矩形区域,阴影画刷填充该矩形区域
		x1 += 70;
		x2 += 70;
	}
}


LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
	PAINTSTRUCT ps;
	HDC hdc;
	int i;

	switch (message) 
	{
		case WM_PAINT:						
			hdc = BeginPaint(hWnd, &ps);
			MyPaint(hdc);
			EndPaint(hWnd, &ps);
			break;
		case WM_DESTROY:				
			for(i=0;i<=6;i++)
			{
				DeleteObject(hPen[i]);		
				DeleteObject(hBru[i]);		
			}
			PostQuitMessage(0);
			break;
		default:							
			return DefWindowProc(hWnd, message, wParam, lParam);
   }
   return 0;
}

 四、效果

猜你喜欢

转载自blog.csdn.net/Sruggle/article/details/90145717