#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; LONG WINAPI WndProc(HWND, UINT, WPARAM,LPARAM); 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"Lab4 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); //DWORD thr; //hThr = CreateThread(NULL, 0, LapsePaintPolygon, 0, 0, &thr); ShowWindow(hWnd, SW_SHOWMAXIMIZED); UpdateWindow(hWnd); ShowWindow(hWndEx, nCmdShow); UpdateWindow(hWndEx); while(GetMessage(&msg, NULL, 0, 0)) { TranslateMessage(&msg); DispatchMessage(&msg); } return msg.wParam; } void PaintPolygon(HDC hdc) { //HPEN oldPen = (HPEN) SelectObject(hdc, pen); //HBRUSH oldBrush = (HBRUSH) 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); //DeleteObject(SelectObject(hdc, oldPen)); //DeleteObject(SelectObject(hdc, oldBrush)); } 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(); } } LONG WINAPI WndProc(HWND hwnd, UINT Message, WPARAM wparam, LPARAM lparam) { switch (Message){ case WM_PAINT: if(hwnd == hWnd) { w = GetSystemMetrics(SM_CXSCREEN); h = GetSystemMetrics(SM_CYSCREEN); HDC hdc; PAINTSTRUCT ps; hdc = BeginPaint(hwnd, &ps); PaintPolygon(hdc); EndPaint(hwnd, &ps); } else return DefWindowProc(hwnd, Message, wparam, lparam); break; case WM_DESTROY: //CloseHandle(hThr); PostQuitMessage(0); break; case WM_KEYDOWN: if (wparam == VK_LEFT) { if (points[3].x - 10 >= 0) { offset -= 10; RepaintWnd(); } else MessageBeep(0); } if (wparam == VK_RIGHT) { if (points[1].x + 10 <= w) { offset += 10; RepaintWnd(); } 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; default: return DefWindowProc(hwnd, Message, wparam, lparam); } return 0; }