003--VS2013 C++ 多边形绘制

时间:2022-12-27 21:02:41

003--VS2013 C++ 多边形绘制

//全局变量
HPEN hPen;
HBRUSH hBru;
POINT poly1[6], poly2[5], poly3[5];

//--------------------------------------------InitInstance() 函数----------------------------------------------
//加载资源
//-------------------------------------------------------------------------------------------------------------
BOOL InitInstance(HINSTANCE hInstance, int nShowCmd)
{
HWND hwnd;
HDC hdc;
const double pi = 3.1415926535;

//hInst = hInstance;

//创建窗口第三步:正式创建窗口
//创建窗口函数
hwnd = CreateWindow("GameClass", WINDOW_TITLE, WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT, 0, CW_USEDEFAULT, 0, NULL, NULL, hInstance, NULL);
if (!hwnd)
{
return FALSE;
}
//创建窗口第四步:窗口的移动,显示和更新
MoveWindow(hwnd, 250, 80, WINDOW_WIDTH, WINDOW_HEIGHT, true);//调整窗口显示时的位置及窗口的大小
ShowWindow(hwnd, nShowCmd);//设定显示窗口时的状态
UpdateWindow(hwnd);//将窗口绘制于显示设备上

for (int i = 0; i <= 4; i++)
{
//.........点起始坐标...........角度........
//第一个图形
poly1[i].x = 200 + 100 * sin(i * 72 * pi / 180);
poly1[i].y = 200 + 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;

}
poly1[5].x = 200;
poly1[5].y = 300;
hPen = CreatePen(PS_SOLID, 5, RGB(rand() % 255, rand() % 255, rand() % 255)); //建立画笔
hBru = CreateHatchBrush(HS_BDIAGONAL, RGB(rand() % 255, rand() % 255, rand() % 255)); //建立画刷

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

return TRUE;
}

//--------------------------------------------MyPaint() 函数---------------------------------------------------
//绘制图形
//-------------------------------------------------------------------------------------------------------------
void MyPaint(HDC hdc)
{
SelectObject(hdc, hPen);
SelectObject(hdc, hBru);
PolylineTo(hdc, poly1, 6);
Polyline(hdc, poly2, 5);//绘制多边线条
Polygon(hdc, poly3, 5);//绘制封闭多边形
}

如需整体运行框架,请点击: 游戏框架

游戏开发的梦想

梦开始的地方