WIN32 API
[핵심 API로 배우는 윈도우프로그래밍(강경우, 한빛아카데미)] 10장 4번 풀이
오다기리 박
2018. 1. 24. 11:13
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 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 | #include <Windows.h> #include<tchar.h> #include<string.h> #include<winres.h> #include<process.h> #include<time.h> #include<stdio.h> #include<stdlib.h> #pragma comment(linker,"/entry:WinMainCRTStartup /subsystem:console") LRESULT CALLBACK WndProc(HWND hwnd, UINT iMsg, WPARAM wParam, LPARAM lParam); void ThreadProc(void *); HINSTANCE hInst; HWND hwnd; int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpszCmdLine, int nCmdShow) { MSG msg; WNDCLASS WndClass; hInst = hInstance; WndClass.style = CS_HREDRAW | CS_VREDRAW; WndClass.lpfnWndProc = WndProc; WndClass.cbClsExtra = 0; WndClass.cbWndExtra = 0; WndClass.hInstance = hInstance; WndClass.hIcon = LoadIcon(NULL, IDI_APPLICATION); WndClass.hCursor = LoadCursor(NULL, IDC_ARROW); WndClass.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH); WndClass.lpszMenuName = NULL; WndClass.lpszClassName = _T("Window Class Name"); RegisterClass(&WndClass); hwnd = CreateWindow( _T("Window Class Name"), _T("박정호"), WS_OVERLAPPEDWINDOW, 50, 50, 1000, 700, NULL, NULL, hInstance, NULL); ShowWindow(hwnd, nCmdShow); UpdateWindow(hwnd); while (GetMessage(&msg, NULL, 0, 0)) { TranslateMessage(&msg); DispatchMessage(&msg); } return (int)msg.wParam; } #define THREAD_NUM 10 static RECT rt; HANDLE hEvent; BOOL directionX[THREAD_NUM] = { TRUE, }; BOOL directionY[THREAD_NUM] = { TRUE, }; int xPos[THREAD_NUM], yPos[THREAD_NUM]; static int r[THREAD_NUM], g[THREAD_NUM], b[THREAD_NUM]; LRESULT CALLBACK WndProc(HWND hwnd, UINT iMsg, WPARAM wParam, LPARAM lParam) { HDC hdc; PAINTSTRUCT ps; static HANDLE hThread[THREAD_NUM]; static int count = 0; static BOOL GO = FALSE; static BOOL init = TRUE; int thread_num = 0; switch (iMsg) { case WM_CREATE: GetClientRect(hwnd, &rt); hEvent = CreateEvent(NULL, TRUE, TRUE, NULL); for (int i = 0; i < THREAD_NUM; i++) { xPos[i] = rand() % (rt.right - 200) + 100; yPos[i] = rand() % (rt.bottom - 200) + 100; r[i] = rand() % 256; g[i] = rand() % 256; b[i] = rand() % 256; } break; case WM_LBUTTONDOWN: for (int i = 0; i < THREAD_NUM; i++) { hThread[i] = (HANDLE)_beginthreadex(NULL, 0, (unsigned int(__stdcall *)(void *))ThreadProc, (void*)&i, 0, NULL); Sleep(50); } break; case WM_PAINT: hdc = BeginPaint(hwnd, &ps); for (int i = 0; i < THREAD_NUM; i++) { //SelectObject(hdc, CreateSolidBrush(RGB(r[i], g[i], b[i]))); Ellipse(hdc, xPos[i] - 20, yPos[i] - 20, xPos[i] + 20, yPos[i] + 20); } EndPaint(hwnd, &ps); break; case WM_DESTROY: CloseHandle(hThread); PostQuitMessage(0); break; } return DefWindowProc(hwnd, iMsg, wParam, lParam); } void ThreadProc(void *arg) { HDC hdc; int index = *((int *)arg); hdc = GetDC(hwnd); while (1) { //WaitForSingleObject(hEvent, INFINITE); if (xPos[index] < 20 || xPos[index]>rt.right - 20) directionX[index] = directionX[index] ? FALSE : TRUE; if (yPos[index] < 20 || yPos[index]>rt.bottom - 20) directionY[index] = directionY[index] ? FALSE : TRUE; if (directionX[index]) xPos[index] ++; else xPos[index] --; if (directionY[index]) yPos[index] ++; else yPos[index] --; for (int i = 0; i < THREAD_NUM; i++) { if (sqrt(pow(xPos[index] - xPos[i], 2) + pow(yPos[index] - yPos[i], 2)) < 40 && i != index) //충돌 { xPos[index] = yPos[index] = -30; xPos[i] = yPos[i] = -30; InvalidateRgn(hwnd, NULL, TRUE); return; } //printf("%lf\n", sqrt(pow(xPos[index] - xPos[i], 2) + pow(yPos[index] - yPos[i], 2))); } Sleep(10); InvalidateRgn(hwnd, NULL, TRUE); } ReleaseDC(hwnd, hdc); return; } | cs |