Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <windows.h>
- #include <vector>
- LPCWSTR ClassName = L"WndClass";
- MSG msg;
- std::vector<HWND> handles;
- std::vector<COLORREF> colours;
- PAINTSTRUCT ps;
- HDC hdc;
- RECT wRect;
- HBRUSH currentBrush;
- HPEN currentPen;
- COLORREF menuColor = RGB(135, 135, 135);
- COLORREF colBrush;
- COLORREF colPen;
- RECT menu;
- int x, y, x2, y2;
- enum {rectangle, ellipse, triangle} object;
- LRESULT CALLBACK WndProc( HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam );
- void IconRect(HDC hdc, int left, int top, int right, int bottom);
- void IconEllipse(HDC hdc, int left, int top, int right, int bottom);
- void IconTriangle(HDC hdc, int left, int top, int right, int bottom);
- int WINAPI WinMain( HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow )
- {
- WNDCLASSEX wc;
- wc.cbSize = sizeof( WNDCLASSEX );
- wc.style = 0;
- wc.lpfnWndProc = WndProc;
- wc.cbClsExtra = 0;
- wc.cbWndExtra = 0;
- wc.hInstance = hInstance;
- wc.hIcon = LoadIcon( 0, IDI_APPLICATION );
- wc.hCursor = LoadCursor( 0, IDC_ARROW );
- wc.hbrBackground =( HBRUSH )( COLOR_WINDOW + 1 );
- wc.lpszMenuName = 0;
- wc.lpszClassName = ClassName;
- wc.hIconSm = LoadIcon( 0, IDI_APPLICATION );
- colours.push_back(RGB(255,255,255));
- colours.push_back(RGB(0,0,0));
- colours.push_back(RGB(127,0,0));
- colours.push_back(RGB(0,127,0));
- colours.push_back(RGB(0,0,127));
- colours.push_back(RGB(127,127,0));
- colours.push_back(RGB(127,0,127));
- colours.push_back(RGB(0,127,127));
- if( !RegisterClassEx( & wc ) )
- {
- MessageBox( NULL, L"Wysoka Komisja odmawia rejestracji tego okna!", L"Niestety...", MB_ICONEXCLAMATION | MB_OK );
- return 1;
- }
- handles.push_back(CreateWindowEx( 0, ClassName, L"MainWnd", WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, CW_USEDEFAULT, 800, 600, 0, 0, hInstance, 0 ));
- for(int i = 0; i < handles.size(); ++i)
- {
- if( handles[i] == 0 )
- {
- MessageBox( NULL, L"Okno odmówiło przyjścia na świat!", L"Ale kicha...", MB_ICONEXCLAMATION );
- return 1;
- }
- }
- for(int i = 0; i < handles.size(); ++i)
- {
- ShowWindow( handles[i], nCmdShow );
- UpdateWindow( handles[i] );
- }
- while( GetMessage( & msg, 0, 0, 0 ) )
- {
- TranslateMessage( & msg );
- DispatchMessage( & msg );
- }
- UnregisterClassW(ClassName, hInstance);
- return msg.wParam;
- }
- LRESULT CALLBACK WndProc( HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam )
- {
- switch( uMsg )
- {
- case WM_CLOSE:
- {
- DestroyWindow( hwnd );
- }
- break;
- case WM_DESTROY:
- if(handles.size() <= 1)
- {
- colours.erase(colours.begin(), colours.end());
- PostQuitMessage( 0 );
- }
- break;
- case WM_PAINT:
- {
- hdc = BeginPaint(hwnd, &ps);
- GetClientRect(hwnd, &wRect);
- menu = wRect;
- menu.left = wRect.right - 220;
- HBRUSH brush = CreateSolidBrush(menuColor);
- SelectObject(hdc, brush);
- Rectangle(hdc, menu.left, menu.top, menu.right, menu.bottom);
- DeleteObject(brush);
- for(int i = 0; i < colours.size(); ++i)
- {
- HBRUSH brush = CreateSolidBrush(colours[i]);
- HBRUSH def = (HBRUSH) SelectObject(hdc, brush);
- if(i % 2)
- {
- Rectangle(hdc, wRect.right - 190, wRect.top - 20 + i*40, wRect.right - 140, wRect.top + (30 + i*40));
- }
- else
- {
- Rectangle(hdc, wRect.right - 90, wRect.top + (20 + i*40), wRect.right - 40, wRect.top + (70 + i*40));
- }
- SelectObject(hdc, def);
- DeleteObject(brush);
- }
- IconRect(hdc, wRect.right - 190, wRect.top + 400, wRect.right - 140, wRect.top + 450);
- IconEllipse(hdc, wRect.right - 90, wRect.top + 400, wRect.right - 40, wRect.top + 450);
- IconTriangle(hdc, wRect.right - 190, wRect.top + 470, wRect.right - 140, wRect.top + 520);
- switch(object)
- {
- case rectangle:
- {
- HPEN defPen = (HPEN) SelectObject(hdc, currentPen);
- Rectangle(hdc, x, y, x2, y2);
- HBRUSH defBrush = (HBRUSH) SelectObject(hdc, currentBrush);
- Rectangle(hdc, x, y, x2, y2);
- }
- break;
- case ellipse:
- {
- HPEN defPen = (HPEN) SelectObject(hdc, currentPen);
- Ellipse(hdc, x, y, x2, y2);
- HBRUSH defBrush = (HBRUSH) SelectObject(hdc, currentBrush);
- Ellipse(hdc, x, y, x2, y2);
- }
- break;
- case triangle:
- {
- POINT pts[3];
- pts[0].x = x + (x2-x)/2;
- pts[0].y = y;
- pts[1].x = x2;
- pts[1].y = y2;
- pts[2].x = x;
- pts[2].y = y2;
- HPEN defPen = (HPEN) SelectObject(hdc, currentPen);
- Polygon(hdc, pts , 3);
- HBRUSH defBrush = (HBRUSH) SelectObject(hdc, currentBrush);
- Polygon(hdc, pts , 3);
- }
- break;
- }
- EndPaint(hwnd, &ps);
- }
- break;
- case WM_RBUTTONDOWN:
- {
- x = LOWORD(lParam);
- y = HIWORD(lParam);
- hdc = GetDC(hwnd);
- if(GetPixel(hdc, x, y) != menuColor && x > menu.left)
- {
- currentPen = CreatePen(BS_SOLID, 3, GetPixel(hdc, x, y));
- }
- ReleaseDC(hwnd, hdc);
- }
- break;
- case WM_LBUTTONDOWN:
- {
- x = LOWORD(lParam);
- y = HIWORD(lParam);
- hdc = GetDC(hwnd);
- if(GetPixel(hdc, x, y) != menuColor && x > menu.left)
- {
- currentBrush = CreateSolidBrush(GetPixel(hdc, x, y));
- }
- ReleaseDC(hwnd, hdc);
- if(x > wRect.right - 190 && y > wRect.top + 400 && x < wRect.right - 140 && y < wRect.top + 450)
- {
- object = rectangle;
- }
- else if(x > wRect.right - 90 && y > wRect.top + 400 && x < wRect.right - 40 && y < wRect.top + 450)
- {
- object = ellipse;
- }
- else if(x > wRect.right - 190 && y > wRect.top + 470 && x < wRect.right - 140 && y < wRect.top + 520)
- {
- object = triangle;
- }
- }
- break;
- case WM_LBUTTONUP:
- {
- x2 = LOWORD(lParam);
- y2 = HIWORD(lParam);
- if(x2 < menu.left && x < menu.left)
- {
- InvalidateRect(hwnd, 0, false);
- }
- }
- break;
- case WM_SIZE:
- {
- InvalidateRect(hwnd, &menu, true);
- UpdateWindow(hwnd);
- }
- break;
- default:
- return DefWindowProc( hwnd, uMsg, wParam, lParam );
- }
- return 0;
- }
- void IconRect(HDC hdc, int left, int top, int right, int bottom)
- {
- HBRUSH brush = CreateSolidBrush(RGB(255, 255, 255));
- HBRUSH brush2 = CreateSolidBrush(RGB(0, 0, 0));
- HBRUSH def = (HBRUSH) SelectObject(hdc, brush);
- Rectangle(hdc, left, top, right, bottom);
- SelectObject(hdc, brush2);
- Rectangle(hdc, left + (right-left)*0.15, top + (bottom - top)*0.15, right - (right-left)*0.15, bottom - (bottom - top)*0.15);
- SelectObject(hdc, def);
- DeleteObject(brush);
- DeleteObject(brush2);
- }
- void IconEllipse(HDC hdc, int left, int top, int right, int bottom)
- {
- HBRUSH brush = CreateSolidBrush(RGB(255, 255, 255));
- HBRUSH brush2 = CreateSolidBrush(RGB(0, 0, 0));
- HBRUSH def = (HBRUSH) SelectObject(hdc, brush);
- Rectangle(hdc, left, top, right, bottom);
- SelectObject(hdc, brush2);
- Ellipse(hdc, left + (right-left)*0.15, top + (bottom - top)*0.15, right - (right-left)*0.15, bottom - (bottom - top)*0.15);
- SelectObject(hdc, def);
- DeleteObject(brush);
- DeleteObject(brush2);
- }
- void IconTriangle(HDC hdc, int left, int top, int right, int bottom)
- {
- POINT pts[3];
- pts[0].x = left + (right-left)/2;
- pts[0].y = top + (bottom - top)*0.15;
- pts[1].x = left + (right-left)*22/25;
- pts[1].y = top + (bottom - top)*0.85;
- pts[2].x = left + (right-left)*3/25;
- pts[2].y = top + (bottom - top)*0.85;
- HBRUSH brush = CreateSolidBrush(RGB(255, 255, 255));
- HBRUSH brush2 = CreateSolidBrush(RGB(0, 0, 0));
- HBRUSH def = (HBRUSH) SelectObject(hdc, brush);
- Rectangle(hdc, left, top, right, bottom);
- SelectObject(hdc, brush2);
- Polygon(hdc, pts , 3);
- SelectObject(hdc, def);
- DeleteObject(brush);
- DeleteObject(brush2);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement