1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116
| #include <windows.h>
void DrawCube(HDC hdc) { int size = 100; int offsetX = 150; int offsetY = 100;
POINT front[4] = { { offsetX, offsetY }, { offsetX + size, offsetY }, { offsetX + size, offsetY + size }, { offsetX, offsetY + size } };
POINT back[4] = { { offsetX + 30, offsetY - 30 }, { offsetX + size + 30, offsetY - 30 }, { offsetX + size + 30, offsetY + size - 30 }, { offsetX + 30, offsetY + size - 30 } };
SelectObject(hdc, CreateSolidBrush(RGB(255, 255, 255))); Polygon(hdc, front, 4); DeleteObject(SelectObject(hdc, GetStockObject(BLACK_BRUSH)));
SelectObject(hdc, CreateSolidBrush(RGB(255, 255, 255))); Polygon(hdc, back, 4); DeleteObject(SelectObject(hdc, GetStockObject(BLACK_BRUSH)));
HPEN hSolidPen = CreatePen(PS_SOLID, 1, RGB(0, 0, 0)); SelectObject(hdc, hSolidPen);
MoveToEx(hdc, front[0].x, front[0].y, NULL); LineTo(hdc, front[1].x, front[1].y); LineTo(hdc, front[2].x, front[2].y); LineTo(hdc, front[3].x, front[3].y);
HPEN hDashPen = CreatePen(PS_DASH, 1, RGB(0, 0, 0)); SelectObject(hdc, hDashPen);
MoveToEx(hdc, front[0].x, front[0].y, NULL); LineTo(hdc, back[0].x, back[0].y); LineTo(hdc, back[1].x, back[1].y); LineTo(hdc, front[1].x, front[1].y); MoveToEx(hdc, front[2].x, front[2].y, NULL); LineTo(hdc, back[2].x, back[2].y); LineTo(hdc, back[3].x, back[3].y); LineTo(hdc, front[3].x, front[3].y);
DeleteObject(hSolidPen); DeleteObject(hDashPen); }
LRESULT CALLBACK WindowProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam) { switch (uMsg) { case WM_PAINT: { PAINTSTRUCT ps; HDC hdc = BeginPaint(hwnd, &ps);
FillRect(hdc, &ps.rcPaint, (HBRUSH)(COLOR_WINDOW + 1));
DrawCube(hdc);
EndPaint(hwnd, &ps); } break;
case WM_DESTROY: PostQuitMessage(0); return 0;
default: return DefWindowProc(hwnd, uMsg, wParam, lParam); } return 0; }
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nShowCmd) { const char CLASS_NAME[] = "Cube Window Class";
WNDCLASS wc = { 0 }; wc.lpfnWndProc = WindowProc; wc.hInstance = hInstance; wc.lpszClassName = CLASS_NAME;
RegisterClass(&wc);
HWND hwnd = CreateWindowEx( 0, CLASS_NAME, "Draw a Cube", WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, CW_USEDEFAULT, 400, 400, NULL, NULL, hInstance, NULL );
ShowWindow(hwnd, nShowCmd);
MSG msg; while (GetMessage(&msg, NULL, 0, 0)) { TranslateMessage(&msg); DispatchMessage(&msg); }
return 0; }
|