Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <windows.h>
- LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);
- void DoPaintMain(HWND hWnd);
- void DoCreateMain(HWND hWnd);
- DWORD WINAPI Thread(PVOID pArg);
- HINSTANCE g_hInst;
- HWND hWndMain;
- LPCTSTR lpszClass = TEXT("Bounce");
- HBITMAP hBit;
- #define R 20 // 반지름
- #define BOUNCE 1
- int x[5];
- int y[5];
- float xv[5];
- float yv[5];
- int APIENTRY WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpszCmdParam, int nCmdShow){
- HWND hWnd;
- MSG Message;
- WNDCLASS WndClass;
- g_hInst = hInstance;
- WndClass.cbClsExtra = 0;
- WndClass.cbWndExtra = 0;
- WndClass.hbrBackground = (HBRUSH)(COLOR_WINDOW + 1);
- WndClass.hCursor = LoadCursor(NULL, IDC_ARROW);
- WndClass.hIcon = LoadIcon(NULL, IDI_APPLICATION);
- WndClass.hInstance = hInstance;
- WndClass.lpfnWndProc = (WNDPROC)WndProc;
- WndClass.lpszClassName = lpszClass;
- WndClass.lpszMenuName = NULL;
- WndClass.style = CS_HREDRAW | CS_VREDRAW;
- RegisterClass(&WndClass);
- hWnd = CreateWindow(lpszClass, lpszClass, WS_BORDER | WS_CAPTION | WS_SYSMENU,
- CW_USEDEFAULT, CW_USEDEFAULT, 740, 700,
- NULL, (HMENU)NULL, hInstance, NULL);
- ShowWindow(hWnd, nCmdShow);
- hWndMain = hWnd;
- while (GetMessage(&Message, 0, 0, 0)) {
- TranslateMessage(&Message);
- DispatchMessage(&Message);
- }
- return (int)Message.wParam;
- }
- LRESULT CALLBACK WndProc(HWND hWnd, UINT iMessage, WPARAM wParam, LPARAM lParam){
- switch (iMessage) {
- case WM_CREATE:
- DoCreateMain(hWnd);
- x[0] = 50; y[0] = 50;
- xv[0] = 4; yv[0] = 5; //속도
- break;
- case WM_PAINT:
- //InvalidateRect(hWnd, NULL, TRUE);
- DoPaintMain(hWnd);
- break;
- case WM_LBUTTONDOWN:
- InvalidateRect(hWnd, NULL, TRUE);
- break;
- case WM_DESTROY:
- PostQuitMessage(0);
- return 0;
- }
- return(DefWindowProc(hWnd, iMessage, wParam, lParam));
- }
- void DoPaintMain(HWND hWnd){
- HANDLE hThread = CreateThread(NULL, 0, Thread, (PVOID)hWnd, 0, NULL);
- CloseHandle(hThread);
- HDC hdc, hMemDC;
- PAINTSTRUCT ps;
- HBITMAP OldBit;
- RECT crt;
- hdc = BeginPaint(hWnd, &ps);
- GetClientRect(hWnd, &crt);
- hMemDC = CreateCompatibleDC(hdc);
- OldBit = (HBITMAP)SelectObject(hMemDC, hBit);
- BitBlt(hdc, 0, 0, crt.right, crt.bottom, hMemDC, 0, 0, SRCCOPY);
- SelectObject(hMemDC, OldBit);
- DeleteDC(hMemDC);
- EndPaint(hWnd, &ps);
- return;
- }
- void DoCreateMain(HWND hWnd){
- HANDLE hThread = CreateThread(NULL, 0, Thread, (PVOID)hWnd, 0, NULL);
- CloseHandle(hThread);
- }
- DWORD WINAPI Thread(PVOID pArg){
- HDC hdc, hMemDC;
- HBITMAP OldBit;
- RECT crt;
- GetClientRect(hWndMain, &crt);
- // 공 그리기 필요 변수
- HPEN hPen, OldPen;
- HBRUSH hBrush, OldBrush;
- hdc = GetDC(hWndMain);
- GetClientRect(hWndMain, &crt);
- if (hBit == NULL) {
- hBit = CreateCompatibleBitmap(hdc, crt.right, crt.bottom);
- }
- hMemDC = CreateCompatibleDC(hdc);
- OldBit = (HBITMAP)SelectObject(hMemDC, hBit);
- FillRect(hMemDC, &crt, GetSysColorBrush(COLOR_WINDOW));
- for (int i = 0; i<crt.right; i += 10) {
- MoveToEx(hMemDC, i, 0, NULL);
- LineTo(hMemDC, i, crt.bottom);
- }
- for (int i = 0; i<crt.bottom; i += 10) {
- MoveToEx(hMemDC, 0, i, NULL);
- LineTo(hMemDC, crt.right, i);
- }
- // 공 그리기
- hPen = CreatePen(PS_INSIDEFRAME, 2, RGB(0, 0, 0));
- OldPen = (HPEN)SelectObject(hMemDC, hPen);
- hBrush = CreateSolidBrush(RGB(255, 120, 0)); // GDI 오브젝트 생성
- OldBrush = (HBRUSH)SelectObject(hMemDC, hBrush); // DC에 오브젝트 선택
- Ellipse(hMemDC, x[0] - R, y[0] - R, x[0] + R, y[0] + R); // 브러시 사용
- DeleteObject(SelectObject(hMemDC, OldPen)); // 이전 브러시로 복구
- DeleteObject(SelectObject(hMemDC, OldBrush)); // 생성한 오브젝트 제거
- SelectObject(hMemDC, OldBit);
- DeleteDC(hMemDC);
- ReleaseDC(hWndMain, hdc);
- InvalidateRect(hWndMain, NULL, FALSE);
- while(1){
- // 공좌표변경
- if (x[0] <= R || x[0] >= crt.right - R)
- {
- xv[0] *= (-BOUNCE);
- }
- if (y[0] <= R || y[0] >= crt.bottom - R)
- {
- yv[0] *= (-BOUNCE);
- }
- Sleep(20);
- y[0] += (int)yv[0];
- x[0] += (int)xv[0];
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement