Visual C++游戏编程基础之多边形绘制

一、解析几何知识

以(0,0)为圆心,半径为100的圆,它的内接五边形就是它均分五等分的点连起来,坐标(x, y)分别为(100*sin(72°), 100*cos(72°))、(100*sin(72°*2), 100*cos(72°*2))……

二、重要函数介绍

1.BOOL Polyline( HDC hdcCONST POINT *lpptint cPoints)

   function:使用当前画笔描绘一系列线段

          hdc:进行绘图的设备场景

          lppt:CONST POINT结构体指针,用来表示X,Y坐标

    cPoints:表示要连接几个点

2.BOOL PolyLineTo(HDC hdc, CONST POINT * apt, DWORD cpt);

   function:使用目前位置作为开始点,并将目前位置设定为最后一根线的终点;如果不设置起点,默认是(0,0)为开始点

3.BOOL POLYGON(HDC HDC, CONST POINT *LPPOINTS, INT NCOUNT);

   function:使用当前画刷填充多边形

三、代码实现

#include "stdafx.h"
#include "stdio.h"

HINSTANCE hInst;
HPEN hPen;
HBRUSH hBru;
POINT poly1[5],poly2[5],poly3[5];

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


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;
	const double pi = 3.1415926535;

	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,600,450,true);
	ShowWindow(hWnd, nCmdShow);
	UpdateWindow(hWnd);

	for(i=0;i<=4;i++)
	{	
		poly1[i].x = 100 + 100 * sin(i*72*pi/180);//初始化顶点数组
		poly1[i].y = 100 + 100 * cos(i*72*pi/180);

		poly2[i].x = poly1[i].x + 300;
		poly2[i].y = poly1[i].y;

		poly3[i].x = poly1[i].x + 180;
		poly3[i].y = poly1[i].y + 200;
	}

	hPen = CreatePen(PS_SOLID,5,RGB(255,0,0));
	hBru = CreateHatchBrush(HS_BDIAGONAL,RGB(0,255,0));

	hdc = GetDC(hWnd);
	MyPaint(hdc);
	ReleaseDC(hWnd,hdc);

	return TRUE;
}

void MyPaint(HDC hdc)
{
	SelectObject(hdc,hPen);
	SelectObject(hdc,hBru);
	Polyline(hdc,poly1,5);
	PolylineTo(hdc,poly2,5);
	Polygon(hdc,poly3,5);
}

void MyPaint1(HDC hdc,LPARAM lParam)
{
	int x,y;
	char str[20] = "";

	x = LOWORD(lParam);		
	y = HIWORD(lParam);		
	
	SetTextColor(hdc,RGB(255,0,0));

	TextOut(hdc,10,300,"鼠标坐标",strlen("鼠标坐标"));
	sprintf(str,"X坐标%d    ",x);//将格式化的数据写入字符串
	TextOut(hdc,30,340,str,strlen(str));//在(30,30)处显示字符串
	sprintf(str,"Y坐标%d    ",y);
	TextOut(hdc,30,360,str,strlen(str));
}

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

	switch (message) 
	{
		case WM_PAINT:						
			hdc = BeginPaint(hWnd, &ps);
			MyPaint(hdc);
			EndPaint(hWnd, &ps);
			break;
			case WM_MOUSEMOVE:					
			hdc = GetDC(hWnd);
			MyPaint1(hdc,lParam);
			ReleaseDC(hWnd,hdc);
			break;
		case WM_DESTROY:					
			DeleteObject(hPen);
			DeleteObject(hBru);
			PostQuitMessage(0);
			break;
		default:							
			return DefWindowProc(hWnd, message, wParam, lParam);
   }
   return 0;
}

  四、效果 

  

猜你喜欢

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