Visual C++游戏编程基础之封闭几何图形绘制
作者:互联网
#include "stdafx.h"
HINSTANCE hInst;
HPEN hPen;
HBRUSH hBru[4];//定义4种风格的画刷
int sBru[4] = {HS_VERTICAL,HS_HORIZONTAL,HS_CROSS,HS_DIAGCROSS};
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,600,450,true);
ShowWindow(hWnd, nCmdShow);
UpdateWindow(hWnd);
hPen = CreatePen(PS_SOLID,3,RGB(255,0,0));
for(i=0;i<=3;i++)
{
hBru[i] = CreateHatchBrush(sBru[i],RGB(0,0,255));//建立4种风格的画刷,颜色为蓝色
}
hdc = GetDC(hWnd);
MyPaint(hdc);
ReleaseDC(hWnd,hdc);
return TRUE;
}
void MyPaint(HDC hdc)
{
SelectObject(hdc,hPen);
SelectObject(hdc,hBru[0]);//它来填充椭圆
Ellipse(hdc,20,20,270,150); //绘制椭圆,4个参数:左上角(x,y)及右下角(x,y)
TextOut(hdc,120,160,"椭圆",strlen("椭圆"));
SelectObject(hdc,hBru[1]);
RoundRect(hdc,300,20,550,150,30,30); //绘制圆角矩形,参数:外围矩形左上角(x,y)、右下角(x,y)
//圆角上椭圆长和高均为30
TextOut(hdc,400,160,"圆角矩形",strlen("圆角矩形"));
SelectObject(hdc,hBru[2]);
Pie(hdc,20,210,270,340,50,50,300,300);//外围矩形的左上及右下坐标;起点及终点坐标,起点与终点与圆心相连
TextOut(hdc,120,350,"扇形",strlen("扇形"));
SelectObject(hdc,hBru[3]);
Chord(hdc,300,210,550,340,50,50,600,300); //起点与终点相连
TextOut(hdc,400,350,"弓形",strlen("弓形"));
}
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:
DeleteObject(hPen);
for(i=0;i<=3;i++)
DeleteObject(hBru[i]);
PostQuitMessage(0);
break;
default:
return DefWindowProc(hWnd, message, wParam, lParam);
}
return 0;
}
标签:几何图形,int,hWnd,C++,HINSTANCE,Visual,hInstance,wcex,NULL 来源: https://blog.csdn.net/Sruggle/article/details/90272836