标签:des style blog http color 使用
//======================================= //程序名称:GDI几何绘图 //2014年4月30日 Created by 图图 //Copyright (c) 图图 //======================================= #include<windows.h> #include<time.h> //全局变量 HDC g_hDc = NULL; //全局设备环境句柄 HPEN g_hPen[7] = {0}; //定义画笔句柄的数组 HBRUSH g_hBrush[7] = {0}; //定义画刷句柄的.数组 int g_penStyle[7] = {PS_SOLID, PS_DASH, PS_DOT, PS_DASHDOT, PS_DASHDOTDOT, PS_NULL, PS_INSIDEFRAME}; //定义画笔样式数组 int g_brushStyle[6] = {HS_VERTICAL, HS_HORIZONTAL, HS_CROSS, HS_DIAGCROSS, HS_FDIAGONAL, HS_BDIAGONAL}; //定义画刷样式数组 //函数原型 bool InitResource(HWND hWnd); void Render(HWND hWnd); void Shutdown(HWND hWnd); //宏定义 #define WINDOW_WIDTH 800 #define WINDOW_HEIGHT 600 #define WINDOW_CLASS "GameDevelop" #define WINDOW_NAME "GDI几何绘图" HRESULT CALLBACK WindowProc( HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam ) { switch(uMsg) { case WM_KEYDOWN: if(wParam == VK_ESCAPE) PostQuitMessage(0); break; case WM_DESTROY: PostQuitMessage(0); break; default: return DefWindowProc(hwnd, uMsg, wParam, lParam); } return 0; } int WINAPI WinMain( HINSTANCE hInstance, HINSTANCE hPreInstance, LPSTR lpCmdLine, int nCmdShow ) { //设计窗口类 WNDCLASSEX wcex; wcex.cbClsExtra = 0; wcex.cbSize = sizeof(WNDCLASSEX); wcex.cbWndExtra = 0; wcex.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH); wcex.hCursor = LoadCursor(NULL, IDC_ARROW); wcex.hIcon = LoadIcon(NULL, IDI_APPLICATION); wcex.hIconSm = NULL; wcex.hInstance = hInstance; wcex.lpfnWndProc = WindowProc; wcex.lpszClassName = WINDOW_CLASS; wcex.lpszMenuName = NULL; wcex.style = CS_HREDRAW | CS_VREDRAW; if(!RegisterClassEx(&wcex)) MessageBox(NULL, "窗口类注册失败", "图图的消息窗口", MB_OK); //创建窗口 HWND hWnd; hWnd = CreateWindow(WINDOW_CLASS, WINDOW_NAME, WS_OVERLAPPEDWINDOW, 0, 0, WINDOW_WIDTH, WINDOW_HEIGHT, NULL, NULL, hInstance, NULL); //对窗口位置和大小作调整,然后显示很更新窗口 MoveWindow(hWnd, 250, 80, WINDOW_WIDTH, WINDOW_HEIGHT, TRUE); ShowWindow(hWnd, nCmdShow); UpdateWindow(hWnd); if(!InitResource(hWnd)) { MessageBox(hWnd, "游戏资源初始化失败", "图图的消息窗口", MB_OK); return false; } MSG msg; ZeroMemory(&msg, sizeof(msg)); while(msg.message != WM_QUIT) { if(PeekMessage(&msg, NULL, 0, 0, PM_REMOVE)) { TranslateMessage(&msg); DispatchMessage(&msg); } else Render(hWnd); } Shutdown(hWnd); UnregisterClass(WINDOW_CLASS, wcex.hInstance); return 0; } bool InitResource(HWND hWnd) { g_hDc = GetDC(hWnd); //获得设备环境句柄 srand((unsigned)time(NULL)); //初始化随机数种子 //随机初始化画笔和画刷的颜色值 for(int i = 0; i < 7; i++) { g_hPen[i] = CreatePen(g_penStyle[i], 1, RGB(rand()%256, rand()%256, rand()%256)); if(i == 6) g_hBrush[i] = CreateSolidBrush(RGB(rand()%256, rand()%256, rand()%256)); else g_hBrush[i] = CreateHatchBrush(g_brushStyle[i], RGB(rand() %256, rand()%256, rand()%256)); } return true; } void Render(HWND hWnd) { int y; for(int i = 0; i <= 6; i++) { y = (i+1) * 70; SelectObject(g_hDc, g_hPen[i]); //选好对应画笔 MoveToEx(g_hDc, 30, y, NULL); //将光标移到(30, y)处 LineTo(g_hDc, 100, y); //从(30, y)到(100, y)处画一条线段 } int x1 = 120; int x2 = 190; for(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; } } void Shutdown(HWND hWnd) { for(int i = 0; i < 7; i++) { DeleteObject(g_hPen[i]); DeleteObject(g_hBrush[i]); } ReleaseDC(hWnd, g_hDc); }
标签:des style blog http color 使用
原文地址:http://blog.csdn.net/u011106520/article/details/24790749