#include #define ID_EXIT 367 #define ID_COLOR 601 HWND hWnd, hWndEx; HINSTANCE hInst; HANDLE hThr; CHOOSECOLOR nColor; WINDOWINFO hInfo; COLORREF curColor = RGB(204, 0, 0); HPEN pen = CreatePen(PS_SOLID, 1, curColor), oldPen; HBRUSH brush = CreateSolidBrush(curColor), oldBrush; POINT points[4]; int offset = 0, w, h, interval = 500; bool isLeft = false; void RepaintWnd(); LONG WINAPI WndProc(HWND, UINT, WPARAM,LPARAM); DWORD WINAPI LapsePaintPolygon(void* Param) { while(true) { if (isLeft) { if (points[3].x - 10 >= 0) offset -= 10; else { MessageBeep(0); isLeft = false; } } else { if (points[1].x + 10 <= w) offset += 10; else { MessageBeep(0); isLeft = true; } } RepaintWnd(); Sleep(interval); } } int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow) { MSG msg; WNDCLASS w; memset(&w, 0, sizeof(WNDCLASS)); w.style = WS_TILED | CS_HREDRAW | CS_VREDRAW; w.lpfnWndProc = WndProc; w.hInstance = hInst = hInstance; w.hbrBackground = (HBRUSH)GetStockObject(LTGRAY_BRUSH); w.lpszClassName = L"Lab4"; w.hIcon = LoadIcon(NULL, IDI_EXCLAMATION); w.hCursor = LoadCursor(NULL, IDC_ARROW); RegisterClass(&w); hWnd = CreateWindow(L"Lab4", L"Main Window", WS_POPUP, 0, 0, GetSystemMetrics(SM_CXSCREEN), GetSystemMetrics(SM_CYSCREEN), NULL, NULL, hInstance, NULL); hWndEx = CreateWindowEx(WS_EX_TOPMOST, L"Lab4", L"Lab5 Nepryakhin 3121", WS_DLGFRAME, 200, 100, 200, 70, NULL, NULL, hInstance, NULL); HWND hColor = CreateWindowEx(WS_EX_CLIENTEDGE | WS_EX_TOPMOST, L"Button", L"Choose Color", WS_CHILD | WS_VISIBLE | BS_PUSHBUTTON, 5, 5, 120, 30, hWndEx, (HMENU)ID_COLOR, hInstance, NULL); HWND hExit = CreateWindowEx(WS_EX_CLIENTEDGE | WS_EX_TOPMOST, L"Button", L"Exit", WS_CHILD | WS_VISIBLE | BS_PUSHBUTTON, 130, 5, 60, 30, hWndEx, (HMENU)ID_EXIT, hInstance, NULL); ShowWindow(hWnd, SW_SHOWMAXIMIZED); UpdateWindow(hWnd); ShowWindow(hWndEx, nCmdShow); UpdateWindow(hWndEx); DWORD thr; hThr = CreateThread(NULL, 0, LapsePaintPolygon, 0, 0, &thr); while(GetMessage(&msg, NULL, 0, 0)) { TranslateMessage(&msg); DispatchMessage(&msg); } return msg.wParam; } void RepaintWnd() { InvalidateRect (hWnd, NULL, TRUE); UpdateWindow (hWnd); } void CreateChooseColorDlg() { nColor.lStructSize = sizeof(CHOOSECOLOR); nColor.hwndOwner = hWndEx; COLORREF cc[16] = { 0 }; nColor.lpCustColors = cc; if (ChooseColor(&nColor) == TRUE) { curColor = nColor.rgbResult; pen = CreatePen(PS_SOLID, 1, curColor); brush = CreateSolidBrush(curColor); RepaintWnd(); } } void PaintPolygon(HDC hdc) { /*HDC hdcMem; HBITMAP hMem; HANDLE hOld; hdcMem = CreateCompatibleDC(hdc); hMem = CreateCompatibleBitmap(hdc, w, h); hOld = SelectObject(hdcMem, hMem);*/ SelectObject(hdc, pen); SelectObject(hdc, brush); points[0].x = w/2+15+offset; points[1].x = w/2+35+offset; points[2].x = w/2-15+offset; points[3].x = w/2-35+offset; points[0].y = h/2+10; points[1].y = h/2-10; points[2].y = h/2-10; points[3].y = h/2+10; Polygon(hdc, points, 4); /*BitBlt(hdc, 0, 0, w, h, hdcMem, 0, 0, SRCCOPY); SelectObject(hdcMem, hOld); DeleteObject(hMem); DeleteDC(hdcMem);*/ //DeleteObject(SelectObject(hdc, oldPen)); //DeleteObject(SelectObject(hdc, oldBrush)); } LONG WINAPI WndProc(HWND hwnd, UINT Message, WPARAM wparam, LPARAM lparam) { switch (Message){ /*case WM_ERASEBKGND: return 1;*/ case WM_PAINT: if(hwnd == hWnd) { HDC hdc; PAINTSTRUCT ps; hdc = BeginPaint(hwnd, &ps); w = GetSystemMetrics(SM_CXSCREEN); h = GetSystemMetrics(SM_CYSCREEN); PaintPolygon(hdc); EndPaint(hwnd, &ps); } else return DefWindowProc(hwnd, Message, wparam, lparam); break; case WM_KEYDOWN: if (wparam == VK_LEFT) isLeft = true; if (wparam == VK_RIGHT) isLeft = false; if (wparam == VK_UP) if (interval - 10 > 10) interval -= 10; else MessageBeep(0); if (wparam == VK_DOWN) if (interval + 10 < 1000) interval += 10; else MessageBeep(0); break; case WM_COMMAND: if (HIWORD(wparam) == BN_CLICKED && LOWORD(wparam) == ID_COLOR) CreateChooseColorDlg(); if (HIWORD(wparam) == BN_CLICKED && LOWORD(wparam) == ID_EXIT) exit(0); break; case WM_CREATE: if(hwnd != hWnd) break; w = GetSystemMetrics(SM_CXSCREEN); h = GetSystemMetrics(SM_CYSCREEN); points[0].x = w/2+15+offset; points[1].x = w/2+35+offset; points[2].x = w/2-15+offset; points[3].x = w/2-35+offset; points[0].y = h/2+10; points[1].y = h/2-10; points[2].y = h/2-10; points[3].y = h/2+10; break; case WM_DESTROY: CloseHandle(hThr); PostQuitMessage(0); break; default: return DefWindowProc(hwnd, Message, wparam, lparam); } return 0; }