源代码;
#include "stdafx.h"
#include "GDIDemoWindowsOfBrush.h"
#include <time.h> // 新增:时间函数,用于产生随机数种子
#pragma comment(lib,"winmm.lib") // 新增:调用PlaySound函数所需库文件
#define WINDOW_WIDTH 800
#define WINDOW_HEIGHT 600
#define WINDOW_TITLE L"【Windows窗口测试——GDI练习】"
//-----------------------------------【全局变量声明部分】-------------------------------------
// 描述:全局变量的声明
//------------------------------------------------------------------------------------------------
HDC g_hdc = NULL; // 新增:全局设备环境句柄
HPEN g_hPen[7] = { 0 }; // 新增:定义画笔句柄的数组
HBRUSH g_hBrush[7] = { 0 }; // 新增:定义画刷句柄的数组
int g_iPenStyle[7] = { PS_SOLID,PS_DASH,PS_DOT,PS_DASHDOT,PS_DASHDOTDOT,PS_NULL,PS_INSIDEFRAME }; // 新增:定义画笔样式数组并初始化
int g_iBrushStyle[6] = { HS_VERTICAL,HS_HORIZONTAL,HS_CROSS,HS_DIAGCROSS,HS_FDIAGONAL,HS_BDIAGONAL }; // 新增:定义画刷样式数组并初始化
LRESULT CALLBACK WndProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam);
BOOL Game_Init(HWND hwnd);
VOID Game_Paint(HWND hwnd);
BOOL Game_CleanUp(HWND hwnd);
//-----------------------------------【WinMain( )函数】--------------------------------------
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nShowCmd)
{
WNDCLASSEX wndClass = { 0 };
wndClass.cbSize = sizeof(WNDCLASSEX);
wndClass.style = CS_HREDRAW | CS_VREDRAW;
wndClass.lpfnWndProc = WndProc;
wndClass.cbClsExtra = 0;
wndClass.cbWndExtra = 0;
wndClass.hInstance = hInstance;
wndClass.hIcon = (HICON)::LoadImage(NULL, L"icon.ico", IMAGE_ICON, 0, 0, LR_DEFAULTSIZE | LR_LOADFROMFILE);
wndClass.hCursor = LoadCursor(NULL, IDC_ARROW);
wndClass.hbrBackground = (HBRUSH)GetStockObject(GRAY_BRUSH);
wndClass.lpszMenuName = NULL;
wndClass.lpszClassName = L"ForTheDreamOfGameDevelop";
if (!RegisterClassEx(&wndClass))
return -1;
HWND hwnd = CreateWindow(L"ForTheDreamOfGameDevelop",
WINDOW_TITLE,
WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT,
CW_USEDEFAULT,
WINDOW_WIDTH,
WINDOW_HEIGHT,
NULL,
NULL,
hInstance,
NULL);
MoveWindow(hwnd, 250, 80, WINDOW_WIDTH, WINDOW_HEIGHT, true);
ShowWindow(hwnd, nShowCmd);
UpdateWindow(hwnd);
if (!Game_Init(hwnd))
{
MessageBox(hwnd, L"资源初始化失败", L"消息窗口", 0);
return FALSE;
}
MSG msg = { 0 };
while (msg.message != WM_QUIT)
{
if (PeekMessage(&msg, 0, 0, 0, PM_REMOVE))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
}
UnregisterClass(L"ForTheDreamOfGameDevelop", wndClass.hInstance);
return 0;
}
//-----------------------------------【WndProc( )函数】--------------------------------------
LRESULT CALLBACK WndProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{
PAINTSTRUCT paintStruct; //定义一个PAINTSTRUCT结构体来记录一些绘制信息
switch (message) //switch语句开始
{
case WM_PAINT: // 若是客户区重绘消息
g_hdc = BeginPaint(hwnd, &paintStruct); //指定窗口进行绘图工作的准备,并用将和绘图有关的信息填充到paintStruct结构体中。
Game_Paint(hwnd);
EndPaint(hwnd, &paintStruct); //EndPaint函数标记指定窗口的绘画过程结束
ValidateRect(hwnd, NULL); // 更新客户区的显示
break; //跳出该switch语句
case WM_KEYDOWN:
if (wParam == VK_ESCAPE)
DestroyWindow(hwnd);
break;
case WM_DESTROY: //若是窗口销毁消息
Game_CleanUp(hwnd); //调用自定义的资源清理函数Game_CleanUp()进行退出前的资源清理
PostQuitMessage(0); //向系统表明有个线程有终止请求。用来响应WM_DESTROY消息
break; //跳出该switch语句
default:
return DefWindowProc(hwnd, message, wParam, lParam);
}
return 0;
}
//-----------------------------------【Game_Init( )函数】--------------------------------------
// 描述:初始化函数,进行一些简单的初始化
//------------------------------------------------------------------------------------------------
BOOL Game_Init(HWND hwnd) // 新增:环境句柄和随机函数,用于随机随机初始化画笔和画刷的颜色值
{
g_hdc = GetDC(hwnd); //获取设备环境句柄
srand((unsigned)time(NULL)); //用系统时间初始化随机种子
//随机初始化画笔和画刷的颜色值
for (int i = 0; i <= 6; i++)
{
g_hPen[i] = CreatePen(g_iPenStyle[i], 1, RGB(rand() % 256, rand() % 256, rand() % 256)); // 使用传统取余方法来进行RGB的选取
if (i == 6)
g_hBrush[i] = CreateSolidBrush(RGB(rand() % 256, rand() % 256, rand() % 256));
else
g_hBrush[i] = CreateHatchBrush(g_iBrushStyle[i], RGB(rand() % 256, rand() % 256, rand() % 256));
}
Game_Paint(hwnd);
ReleaseDC(hwnd, g_hdc);
return TRUE;
}
//-----------------------------------【Game_Paint( )函数】--------------------------------------
// 描述:绘制函数,在此函数中进行绘制操作
//--------------------------------------------------------------------------------------------------
VOID Game_Paint(HWND hwnd)
{
//定义一个y坐标值
int y = 0;
//一个for循环,用7种不同的画笔绘制线条
for (int i = 0; i <= 6; i++)
{
y = (i + 1) * 70;
SelectObject(g_hdc, g_hPen[i]); //将对应的画笔选好
MoveToEx(g_hdc, 1, y, NULL); //“光标”移动到对应的(30,y)坐标处
LineTo(g_hdc, 100, y); //从(30,y)坐标处向(100,y)绘制线段
}
/*注意上面画完后y=420,下面画矩形的时候还有用*/
//定义两个x坐标值
int x1 = 120;
int x2 = 190;
//用7种不同的画刷填充矩形
for (int i = 0; i <= 6; i++)
{
SelectObject(g_hdc, g_hBrush[i]); //选用画刷
Rectangle(g_hdc, x1, 70, x2, y); //画出一个封闭的矩形,矩形左上角坐标为(x1,50),右下角坐标为(x2,y)
x1 += 90;
x2 += 90;
}
}
//-----------------------------------【Game_CleanUp( )函数】--------------------------------
// 描述:资源清理函数,在此函数中进行程序退出前资源的清理工作
//---------------------------------------------------------------------------------------------------
BOOL Game_CleanUp(HWND hwnd)
{
//一个for循环,释放掉所有的画笔和画刷句柄
for (int i = 0; i <= 6; i++)
{
DeleteObject(g_hPen[i]);
DeleteObject(g_hBrush[i]);
}
return TRUE;
}
网友评论