Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //BY Alan VS2015E
- // W VS 10 zastapic wypelnianie struktur z S = {...}; na s.a = ...; s.b = .... dla kazdego pola
- #include <Windows.h>
- #include <time.h>
- #include <malloc.h>
- #include <stdio.h>
- #include <stdlib.h>
- #define BTN_COL_NO 16 // Ilosc kolorow
- int WND_WID = 900; // Szerokosc okna
- int WND_HEI = 600; // Wysokosc okna
- bool Print = false;
- int NoOfShapes;
- enum MouseButton { LEFT, RIGHT };
- enum figure { SQUARE, ELIPSE, TRIANGLE }; // Rodzaje figur
- struct color { int Red, Green, Blue; }; // Struktura kolorow
- struct Button { // Struktura przycisku
- figure Figure; // Ksztalt przycisku
- int BorderWidth, PosX, PosY, Width, Height; // Szerokosc krawedzi ,pozycja x i y ,szeroksc i wysokosc
- color BorderColor, FillColor; // Kolory krawedzi i wypelnienie
- };
- struct cursor { // Struktura kursora
- POINT Start, End;
- int Size = 2; // Rozmiar 'pedzla'
- bool LeftPressed = false, RightPressed = false;
- bool Fill = false; // Czy rysowac , czy wypelnic kolorem
- figure Figure = SQUARE; // Ksztalt pedzla
- color BorderColor = { 0,0,0 }, FillColor = { 255, 255, 255 }; // Kolor ramki i wypelnienia
- };
- Button Pallet; // Paleta/przybornik tlo
- Button ColorButtons[BTN_COL_NO]; // Przyciski kolorow
- cursor Cursor; // Kursor
- Button *Shape;
- void AddButton(HWND &hwnd, Button &btn) { // Dodawanie/rysowanie przycisku
- HDC hdc = GetDC(hwnd);
- HBRUSH Fillament = CreateSolidBrush(RGB(btn.FillColor.Red, btn.FillColor.Green, btn.FillColor.Blue));// Wypelnienie
- HPEN Border = CreatePen(PS_SOLID, btn.BorderWidth, RGB(btn.BorderColor.Red, btn.BorderColor.Green, btn.BorderColor.Blue));// Ramka
- HBRUSH OldFlilament = (HBRUSH)SelectObject(hdc, Fillament); // Pedzel
- HPEN OldBorder = (HPEN)SelectObject(hdc, Border); // Piuro
- if (btn.Figure == SQUARE) // Kwadratowy przycisk
- Rectangle(hdc, btn.PosX, btn.PosY, btn.PosX + btn.Width, btn.PosY + btn.Height);
- else if (btn.Figure == ELIPSE) // Eliptyczny przycisk
- Ellipse(hdc, btn.PosX, btn.PosY, btn.PosX + btn.Width, btn.PosY + btn.Height);
- else if (btn.Figure == TRIANGLE) { // Trojkat (wlasny) (wielkoat)
- POINT Points[3];
- Points[0] = { (btn.PosX + (btn.Width / 2)) ,btn.PosY };
- Points[1] = { btn.PosX + btn.Width ,btn.PosY + btn.Height };
- Points[2] = { btn.PosX , btn.PosY + btn.Height };
- Polygon(hdc, Points, 3);
- }
- SelectObject(hdc, OldFlilament);
- SelectObject(hdc, OldBorder);
- DeleteObject(Border);
- DeleteObject(Fillament);
- ReleaseDC(hwnd, hdc);
- }
- void SetRandData(HWND &hwnd) {
- for (int i = 0; i < NoOfShapes; i++) {
- Shape[i].Figure = (figure)(rand() % 3);
- Shape[i].BorderWidth = (rand() % 2);
- Shape[i].PosX = (rand() % (WND_WID - 330)) + 100; Shape[i].PosY = (rand() % (WND_HEI - 200)) + 100;
- Shape[i].Width = (rand() % 100); Shape[i].Height = (rand() % 100);
- Shape[i].BorderColor.Red = 0;
- Shape[i].BorderColor.Green = 0;
- Shape[i].BorderColor.Blue = 0;
- Shape[i].FillColor.Red = 255;
- Shape[i].FillColor.Green = 255;
- Shape[i].FillColor.Blue = 255;
- }
- }
- void AddElements(HWND &hwnd) { // Tworzenie przyciskow (ksztalt ,kolor)
- Pallet = { SQUARE,2, WND_WID - 146, 1, 130, WND_HEI - 40,{ 0,0,0 },{ 200,200,200 } };
- //Set(Pallet, WND_WID - 146, 1, 130, WND_HEI - 40, 0, 0, 0, 200, 200, 200);
- int i = 0; // TYPE Bsize X Y W H Ramka Wypelnienie /// Kolory
- ColorButtons[i++] = { SQUARE, 1, Pallet.PosX + 10, 10, 50, 50,{ 0,0,0 },{ 0,0,0 } };
- ColorButtons[i++] = { SQUARE, 1, Pallet.PosX + 70, 10, 50, 50,{ 0,0,0 },{ 255,0,0 } };
- ColorButtons[i++] = { SQUARE, 1, Pallet.PosX + 10, 70, 50, 50,{ 0,0,0 },{ 0,255,0 } };
- ColorButtons[i++] = { SQUARE, 1, Pallet.PosX + 70, 70, 50, 50,{ 0,0,0 },{ 0,0,255 } };
- ColorButtons[i++] = { SQUARE, 1, Pallet.PosX + 10, 130, 50, 50,{ 0,0,0 },{ 100,100,100 } };
- ColorButtons[i++] = { SQUARE, 1, Pallet.PosX + 70, 130, 50, 50,{ 0,0,0 },{ 255,100,100 } };
- ColorButtons[i++] = { SQUARE, 1, Pallet.PosX + 10, 190, 50, 50,{ 0,0,0 },{ 100,255,100 } };
- ColorButtons[i++] = { SQUARE, 1, Pallet.PosX + 70, 190, 50, 50,{ 0,0,0 },{ 100,100,255 } };
- ColorButtons[i++] = { SQUARE, 1, Pallet.PosX + 10, 250, 50, 50,{ 0,0,0 },{ 200,200,200 } };
- ColorButtons[i++] = { SQUARE, 1, Pallet.PosX + 70, 250, 50, 50,{ 0,0,0 },{ 200,255,255 } };
- ColorButtons[i++] = { SQUARE, 1, Pallet.PosX + 10, 310, 50, 50,{ 0,0,0 },{ 255,200,255 } };
- ColorButtons[i++] = { SQUARE, 1, Pallet.PosX + 70, 310, 50, 50,{ 0,0,0 },{ 255,255,200 } };
- ColorButtons[i++] = { SQUARE, 1, Pallet.PosX + 10, 370, 50, 50,{ 0,0,0 },{ 100,200,255 } };
- ColorButtons[i++] = { SQUARE, 1, Pallet.PosX + 70, 370, 50, 50,{ 0,0,0 },{ 200,255,100 } };
- ColorButtons[i++] = { SQUARE, 1, Pallet.PosX + 10, 430, 50, 50,{ 0,0,0 },{ 255,100,200 } };
- ColorButtons[i++] = { SQUARE, 1, Pallet.PosX + 70, 430, 50, 50,{ 0,0,0 },{ 255,255,255 } };
- AddButton(hwnd, Pallet); // Rysowanie palety
- for (int i = 0; i < BTN_COL_NO; i++) // Rysowanie przyciskow kolorow
- AddButton(hwnd, ColorButtons[i]);
- for (int i = 0; i < NoOfShapes; i++) // Rysowanie przyciskow kolorow
- AddButton(hwnd, Shape[i]);
- }
- void ButtonsClicks(HWND &hwnd, int MouseX, int MouseY, MouseButton MB) { // Wykrywanie klikniec na przyciskach ...
- for (int i = 0; i < BTN_COL_NO; i++) { // ... kolorow
- if (MouseX >= ColorButtons[i].PosX &&
- MouseX <= ColorButtons[i].PosX + ColorButtons[i].Width &&
- MouseY >= ColorButtons[i].PosY &&
- MouseY <= ColorButtons[i].PosY + ColorButtons[i].Height) {// Jesli kliknieto na [i]'ty prycisk
- if (MB == RIGHT)
- Cursor.BorderColor = ColorButtons[i].FillColor; // Zmiana kolorow
- else if (MB == LEFT)
- Cursor.FillColor = ColorButtons[i].FillColor;
- return;
- }
- }
- }
- LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam) {
- int MouseX = LOWORD(lParam); // Pobranie
- int MouseY = HIWORD(lParam); // X i Y myszy
- switch (msg) {
- case WM_RBUTTONDOWN: ButtonsClicks(hwnd, MouseX, MouseY, RIGHT); break; // Prawe klikniecie tylko na przyborniku -> sprawdzenie kolizji
- case WM_MOVE: AddElements(hwnd); break; // Przesowanie okna -> przerysowanie elementow
- case WM_SIZE: { // Zmiana rozmiaru okna -> pobranie nowego rozmiaru
- RECT rect;
- if (GetWindowRect(hwnd, &rect)) {
- WND_WID = rect.right - rect.left;
- WND_HEI = rect.bottom - rect.top;
- }
- AddElements(hwnd); // Przerysowanie elementow
- }break;
- case WM_LBUTTONDOWN: { // Lewy klikniet (w dol)
- if (MouseX > Pallet.PosX) { // Jesli nad przybornikiem
- ButtonsClicks(hwnd, MouseX, MouseY, LEFT);// Sprawdzenie kolizji
- }
- else {
- for (int i = 0; i < NoOfShapes; i++) {
- if (MouseX >= Shape[i].PosX &&
- MouseX <= Shape[i].PosX + Shape[i].Width &&
- MouseY >= Shape[i].PosY &&
- MouseY <= Shape[i].PosY + Shape[i].Height) {// Jesli kliknieto na [i]'ty prycisk
- for (int j = 0; j < NoOfShapes; j++) {
- if (Shape[i].Figure == Shape[j].Figure) {
- Shape[j].FillColor = Cursor.FillColor;
- Shape[j].BorderColor = Cursor.BorderColor;
- }
- }
- }
- }
- }
- AddElements(hwnd);
- } break;
- case WM_MOUSEMOVE: {
- }break;
- case WM_CLOSE: { // Standard
- DestroyWindow(hwnd);
- }break;
- case WM_DESTROY: {// Standard
- PostQuitMessage(0);
- }break;
- default: {// Standard
- return DefWindowProc(hwnd, msg, wParam, lParam);
- }break;
- }
- return 0;
- }
- int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR ilCmdLine, int nCmdShow) {
- srand(time(NULL));
- WNDCLASSEX window;
- MSG msg;
- TCHAR Class_Name[] = TEXT("OKNO_TEST"), Title[] = TEXT("Tytul");
- window.cbClsExtra = NULL;
- window.cbSize = sizeof(WNDCLASSEX);
- window.cbWndExtra = NULL;
- window.hbrBackground = (HBRUSH)(COLOR_WINDOW + 1);
- window.hCursor = LoadCursor(NULL, IDC_CROSS);//IDC_ARROW
- window.hIcon = LoadIcon(NULL, IDI_APPLICATION);
- window.hIconSm = LoadIcon(NULL, IDI_APPLICATION);
- window.hInstance = hInstance;
- window.lpfnWndProc = WndProc;
- window.lpszClassName = Class_Name;
- window.lpszMenuName = 0;
- window.style = CS_HREDRAW | CS_VREDRAW;
- if (!RegisterClassEx(&window)) {
- MessageBox(NULL, TEXT("Błąd rejestracji okna"), TEXT("Błąd"), MB_ICONEXCLAMATION);
- return 0;
- }
- HWND hwnd = CreateWindowEx(WS_EX_WINDOWEDGE, Class_Name, Title, WS_OVERLAPPED | WS_CAPTION | WS_SYSMENU | WS_THICKFRAME | WS_MINIMIZEBOX | WS_MAXIMIZEBOX | WS_VISIBLE, CW_USEDEFAULT, CW_USEDEFAULT, WND_WID, WND_HEI, NULL, NULL, hInstance, NULL);
- if (hwnd == NULL) {
- MessageBox(NULL, TEXT("Błąd tworzenia okna"), TEXT("Błąd"), MB_ICONEXCLAMATION);
- UnregisterClass(Class_Name, hInstance);
- return 0;
- }
- NoOfShapes = (rand() % 10 + 10);
- Shape = (Button *)malloc(sizeof(Button)*NoOfShapes);
- SetRandData(hwnd);
- ShowWindow(hwnd, nCmdShow);
- UpdateWindow(hwnd);
- AddElements(hwnd); // To dodac do szablonu MAINA
- while (GetMessage(&msg, NULL, 0, 0)) {
- TranslateMessage(&msg);
- DispatchMessage(&msg);
- }
- if (!UnregisterClass(Class_Name, hInstance)) {
- MessageBox(NULL, TEXT("Błąd niszczenia okna"), TEXT("Błąd"), MB_ICONEXCLAMATION);
- return 0;
- }
- return (int)msg.wParam;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement