alien_fx_fiend

Infinite Scrolling Canvas Doodle App C++ Win32 *FINAL RELEASE*

Nov 4th, 2024 (edited)
12
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 25.13 KB | Source Code | 0 0
  1. ==++ Here's the full source code (file 1/1) of "main.cpp"::++==
  2. #include <vector>
  3. #include <windows.h>
  4. //#include <iostream>
  5. #include "resource.h"  // Add this with your other includes
  6.  
  7. using namespace std;
  8.  
  9. // Forward Declarations
  10. struct Brushstroke;
  11. struct Eraserstroke;
  12.  
  13. void DrawBrush(HDC hdc, int x, int y);
  14. void Erase(HDC hdc, int x, int y);
  15. void ClearDrawing(HDC hdc, int width, int height);
  16. void DrawSmoothBrush(HDC hdc, int x, int y, bool isScreenDC);
  17. // Add these global variables at the top
  18. int scrollX = 0;
  19. int scrollY = 0;
  20. bool isSpacePressed = false;
  21. POINT lastMousePos = { 0, 0 };
  22. POINT dragStart = { 0, 0 };
  23. int virtualWidth = 10000;
  24. int virtualHeight = 10000;
  25. // Add these global variables if not already present
  26. std::vector<Brushstroke> storedBrushstrokes;
  27. std::vector<Eraserstroke> storedEraserstrokes;
  28. HDC hMemoryDC = NULL;
  29. HBITMAP hMemoryBitmap = NULL;
  30. HBITMAP hOldBitmap = NULL;  // Add this to store the old bitmap
  31. int canvasWidth = 10000;
  32. int canvasHeight = 10000;
  33.  
  34. bool isPaintbrushSelected = true;  // Add a global variable to track the selected tool
  35. // Define a vector to store the doodle points for the Paintbrush tool
  36. //std::vector<POINT> paintbrushDoodlePoints;
  37. //new mod code
  38. POINT previousPoint;  // Add a new global variable to track the previous point
  39. // Declare hdc as a global variable
  40. HDC hdc;
  41. HWND hWnd;  // Declare the window handle variable globally or within the appropriate scope
  42. //bool needsRedraw = true;  // Add this line at the top of your file
  43.            // Declare a global variable to specify the grid spacing
  44. //int gridSpacing = 20;
  45.  
  46. // Define a struct to store brushstroke information
  47. struct Brushstroke {
  48.    int x;
  49.    int y;
  50.    int size;
  51.    COLORREF color;
  52.    // Add any other necessary properties for the brushstroke
  53. };
  54.  
  55. // Define a struct to store eraserstroke information
  56. struct Eraserstroke {
  57.    int x;
  58.    int y;
  59.    int size;
  60.    COLORREF color;
  61.    // Add any other necessary properties for the eraserstroke
  62. };
  63.  
  64. // Declare a vector to store the brushstrokes
  65. //std::vector<Brushstroke> storedBrushstrokes;
  66. //std::vector<Eraserstroke> storedEraserstrokes;
  67. // Global variables to store the minimized brushstrokes
  68. std::vector<Brushstroke> minimizedDrawnBrushstrokes;
  69. std::vector<Eraserstroke> minimizedErasedBrushstrokes;
  70.  
  71. // Function declaration for DrawGrid
  72. //void DrawGrid(HDC hdc, int spacing, COLORREF color); //drawgrid disableddefault
  73.  
  74. // Optimized DrawGrid function to efficiently draw both vertical and horizontal lines
  75.  
  76. // Add this function to initialize the memory bitmap
  77. // Add this function
  78. void InitializeMemoryBitmap(HWND hwnd) {
  79.    HDC hdc = GetDC(hwnd);
  80.    
  81.    // Delete old objects if they exist
  82.    if (hMemoryDC) {
  83.        if (hOldBitmap) {
  84.            SelectObject(hMemoryDC, hOldBitmap);
  85.        }
  86.        DeleteDC(hMemoryDC);
  87.    }
  88.    if (hMemoryBitmap) {
  89.        DeleteObject(hMemoryBitmap);
  90.    }
  91.  
  92.    // Create new memory DC and bitmap
  93.    hMemoryDC = CreateCompatibleDC(hdc);
  94.    hMemoryBitmap = CreateCompatibleBitmap(hdc, virtualWidth, virtualHeight);
  95.    hOldBitmap = (HBITMAP)SelectObject(hMemoryDC, hMemoryBitmap);
  96.  
  97.    // Fill with white background
  98.    HBRUSH whiteBrush = CreateSolidBrush(RGB(255, 255, 255));
  99.    RECT rect = { 0, 0, virtualWidth, virtualHeight };
  100.    FillRect(hMemoryDC, &rect, whiteBrush);
  101.    DeleteObject(whiteBrush);
  102.  
  103.    ReleaseDC(hwnd, hdc);
  104. }
  105.  
  106. LRESULT CALLBACK WindowProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam);
  107.  
  108. HINSTANCE hInst;
  109.  
  110. bool isDrawing = false;
  111. bool isErasing = false;
  112. bool isClearing = false;
  113. //bool isPaintbrushSelected = false;
  114. bool isEraserSelected = false;
  115. int brushSize = 10;
  116. //POINT previousPoint;
  117.  
  118.  
  119. //old pre-gpt
  120. /*void DrawBrush(HDC hdc, int x, int y) {
  121.    HBRUSH hBrush = CreateSolidBrush(RGB(255, 0, 0));  // Set the brush color to fully red
  122.    SelectObject(hdc, GetStockObject(NULL_PEN));  // Set the pen to null to remove the border
  123.    SelectObject(hdc, hBrush);
  124.    Ellipse(hdc, x - brushSize, y - brushSize, x + brushSize, y + brushSize);
  125.    DeleteObject(hBrush);
  126. }*/
  127.  
  128. //new gpt broken?
  129. void DrawBrush(HDC hdc, int x, int y) {
  130.    HRGN hRgn = CreateEllipticRgn(x - brushSize, y - brushSize, x + brushSize, y + brushSize);
  131.    FillRgn(hdc, hRgn, CreateSolidBrush(RGB(255, 0, 0)));
  132.    DeleteObject(hRgn);
  133. }
  134. /*void DrawBrush(HDC hdc, int x, int y) {
  135.    HRGN hRgn = CreateEllipticRgn(x - brushSize, y - brushSize, x + brushSize, y + brushSize);
  136.    FillRgn(hdc, hRgn, CreateSolidBrush(RGB(255, 0, 0)));
  137.    DeleteObject(hRgn);
  138. }*/
  139.  
  140. //broken old
  141. void Erase(HDC hdc, int x, int y) {
  142.    HBRUSH hBrush = CreateSolidBrush(RGB(255, 255, 255));
  143.    SelectObject(hdc, GetStockObject(NULL_PEN));
  144.    SelectObject(hdc, hBrush);
  145.    Ellipse(hdc, x - brushSize, y - brushSize, x + brushSize, y + brushSize);
  146.    DeleteObject(hBrush);
  147. }
  148. /*void Erase(HDC hdc, int x, int y) {
  149.    HBRUSH hBrush = CreateSolidBrush(RGB(255, 255, 255));  // Set the brush color to white
  150.    HGDIOBJ hOldBrush = SelectObject(hdc, hBrush);
  151.    Ellipse(hdc, x - brushSize, y - brushSize, x + brushSize, y + brushSize);
  152.    SelectObject(hdc, hOldBrush);
  153.    DeleteObject(hBrush);
  154. }*/
  155.  
  156. //start test of new eraser
  157. /*void Erase(HDC hdc, int x, int y) {
  158.    HBRUSH hBrush = CreateSolidBrush(RGB(255, 255, 255));
  159.    SelectObject(hdc, GetStockObject(NULL_PEN));
  160.    SelectObject(hdc, hBrush);
  161.    Ellipse(hdc, x - brushSize, y - brushSize, x + brushSize, y + brushSize);
  162.    DeleteObject(hBrush);
  163. }*/
  164. //end test of new eraser
  165.  
  166. //start commented out testing new gpt function eraser
  167. /*void Erase(HDC hdc, int x, int y) {
  168.    // HBRUSH hBrush = CreateSolidBrush(RGB(255, 255, 255));  // Set the brush color to white
  169.    // HBRUSH hBrush = CreateSolidBrush(RGB(0, 0, 0));  // Set the brush color to black
  170.    HBRUSH hBrush = CreateSolidBrush(RGB(255, 255, 255));  // Set the brush color to white
  171.    HPEN hPen = CreatePen(PS_SOLID, 1, RGB(255, 255, 255));  // Set the pen color to white
  172.    HGDIOBJ hOldBrush = SelectObject(hdc, hBrush);
  173.    HGDIOBJ hOldPen = SelectObject(hdc, hPen);
  174.    Ellipse(hdc, x - brushSize, y - brushSize, x + brushSize, y + brushSize);
  175.    SelectObject(hdc, hOldBrush);
  176.    SelectObject(hdc, hOldPen);
  177.    DeleteObject(hBrush);
  178.    DeleteObject(hPen);
  179. }*/
  180. //end commented out testing new gpt function eraser
  181.  
  182. //newgptdelete
  183. void ClearDrawing(HWND hwnd) {
  184.    // Clear the memory DC
  185.    RECT rect = { 0, 0, virtualWidth, virtualHeight };
  186.    HBRUSH whiteBrush = CreateSolidBrush(RGB(255, 255, 255));
  187.    FillRect(hMemoryDC, &rect, whiteBrush);
  188.    DeleteObject(whiteBrush);
  189.  
  190.    // Clear all stored strokes
  191.    storedBrushstrokes.clear();
  192.    storedEraserstrokes.clear();
  193.  
  194.    // Reset scroll position (optional)
  195.    scrollX = 0;
  196.    scrollY = 0;
  197.  
  198.    // Force a redraw
  199.    InvalidateRect(hwnd, NULL, FALSE);
  200. }
  201. /*void ClearDrawing(HDC hdc, int width, int height) {
  202.    RECT rect = { 0, 0, width, height };
  203.    // HBRUSH hBrush = CreateSolidBrush(RGB(255, 255, 255));  // Set the color to white
  204.    // HBRUSH hBrush = CreateSolidBrush(RGB(0, 0, 0));  // Set the color to black
  205.    HBRUSH hBrush = CreateSolidBrush(RGB(255, 255, 255));  // Set the color to white
  206.    FillRect(hdc, &rect, hBrush);
  207.    DeleteObject(hBrush);
  208. }*/
  209.  
  210. //removing drawgrid
  211. /*void DrawGrid(HDC hdc, int spacing, COLORREF color) {
  212.    RECT rect;
  213.    GetClientRect(hWnd, &rect);
  214.  
  215.    HPEN hPen = CreatePen(PS_SOLID, 1, color);
  216.    HPEN hOldPen = (HPEN)SelectObject(hdc, hPen);
  217.  
  218.    // Draw vertical lines
  219.    for (int x = 0; x <= rect.right; x += spacing) {
  220.        MoveToEx(hdc, x, 0, NULL);
  221.        LineTo(hdc, x, rect.bottom);
  222.    }
  223.  
  224.    // Draw horizontal lines
  225.    for (int y = 0; y <= rect.bottom; y += spacing) {
  226.        MoveToEx(hdc, 0, y, NULL);
  227.        LineTo(hdc, rect.right, y);
  228.    }
  229.  
  230.    SelectObject(hdc, hOldPen);
  231.    DeleteObject(hPen);
  232. }*/
  233.  
  234. //gpts old drawgrid
  235. /*void DrawGrid(HDC hdc, int spacing, COLORREF color) {
  236.    RECT rect;
  237.    GetClientRect(hWnd, &rect);
  238.  
  239.    HPEN hPen = CreatePen(PS_SOLID, 1, color);
  240.    HPEN hOldPen = (HPEN)SelectObject(hdc, hPen);
  241.  
  242.    // Draw vertical lines
  243.    for (int x = 0; x < rect.right; x += spacing) {
  244.        MoveToEx(hdc, x, 0, NULL);
  245.        LineTo(hdc, x, rect.bottom);
  246.    }
  247.  
  248.    // Draw horizontal lines
  249.    for (int y = 0; y < rect.bottom; y += spacing) {
  250.        MoveToEx(hdc, 0, y, NULL);
  251.        LineTo(hdc, rect.right, y);
  252.    }
  253.  
  254.    SelectObject(hdc, hOldPen);
  255.    DeleteObject(hPen);
  256. }*/
  257.  
  258. //chatgpt disabled
  259. /*void DrawGrid(HDC hdc, int spacing, COLORREF color) {
  260.    RECT rect;
  261.    GetClientRect(hWnd, &rect);
  262.  
  263.    HPEN hPen = CreatePen(PS_SOLID, 1, color);
  264.    HPEN hOldPen = (HPEN)SelectObject(hdc, hPen);
  265.  
  266.    // Draw vertical lines
  267.    for (int x = 0; x < rect.right; x += spacing) {
  268.        MoveToEx(hdc, x, 0, NULL);
  269.        LineTo(hdc, x, rect.bottom);
  270.    }
  271.  
  272.    // Draw horizontal lines
  273.    for (int y = 0; y < rect.bottom; y += spacing) {
  274.        MoveToEx(hdc, 0, y, NULL);
  275.        LineTo(hdc, rect.right, y);
  276.    }
  277.  
  278.    SelectObject(hdc, hOldPen);
  279.    DeleteObject(hPen);
  280. }*/
  281.  
  282. //temp disabled for relocated code
  283. /*int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE, LPSTR, int nCmdShow) {
  284.    const wchar_t CLASS_NAME[] = L"DoodleAppClass";
  285.  
  286.    WNDCLASS wc = { };
  287.  
  288.    wc.lpfnWndProc = WindowProc;
  289.    wc.hInstance = hInstance;
  290.    wc.lpszClassName = CLASS_NAME;
  291.  
  292.    RegisterClass(&wc);
  293.  
  294.    hInst = hInstance;
  295.  
  296.    HWND hwnd = CreateWindowEx(
  297.        0,
  298.        CLASS_NAME,
  299.        L"Doodle App",
  300.        WS_OVERLAPPEDWINDOW | WS_MAXIMIZE, //& ~WS_MINIMIZEBOX,  Add WS_MAXIMIZE style
  301.        // Disable the Minimize button
  302.  
  303.        CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT,
  304.  
  305.        NULL,
  306.        NULL,
  307.        hInstance,
  308.        NULL
  309.    );
  310.  
  311.    //hdc = GetDC(hWnd); //gptadded
  312.    if (hwnd == NULL) {
  313.        return 0;
  314.    }
  315.  
  316.    ShowWindow(hwnd, SW_SHOWMAXIMIZED);  // Show the window maximized
  317.  
  318.    //old code
  319.    //ShowWindow(hwnd, nCmdShow);
  320.  
  321.    MSG msg = { };
  322.    while (GetMessage(&msg, NULL, 0, 0)) {
  323.        TranslateMessage(&msg);
  324.        DispatchMessage(&msg);
  325.    }
  326.  
  327.    return 0;
  328. }*/
  329.  
  330. //new code
  331. void DrawSmoothBrush(HDC hdc, int x, int y, bool isScreenDC) {
  332.    if (!isDrawing) return;
  333.  
  334.    POINT currentPoint = { x, y };
  335.    int numPoints = 3;
  336.  
  337.    // Convert previous point to current coordinate system if needed
  338.    POINT drawPrevPoint = previousPoint;
  339.    if (isScreenDC) {
  340.        // For screen DC, add scroll offset to stored canvas coordinates
  341.        drawPrevPoint.x = previousPoint.x + scrollX;
  342.        drawPrevPoint.y = previousPoint.y + scrollY;
  343.    }
  344.  
  345.    for (int i = 1; i <= numPoints; i++) {
  346.        float t = (float)i / (float)numPoints;
  347.        int smoothX = (int)(drawPrevPoint.x + t * (currentPoint.x - drawPrevPoint.x));
  348.        int smoothY = (int)(drawPrevPoint.y + t * (currentPoint.y - drawPrevPoint.y));
  349.  
  350.        if (isPaintbrushSelected) {
  351.            DrawBrush(hdc, smoothX, smoothY);
  352.  
  353.            // Only store strokes once, in canvas coordinates
  354.            if (!isScreenDC) {
  355.                Brushstroke newBrushstroke;
  356.                newBrushstroke.x = smoothX;
  357.                newBrushstroke.y = smoothY;
  358.                newBrushstroke.size = brushSize;
  359.                newBrushstroke.color = RGB(255, 0, 0);
  360.                storedBrushstrokes.push_back(newBrushstroke);
  361.            }
  362.        }
  363.        else if (isEraserSelected) {
  364.            Erase(hdc, smoothX, smoothY);
  365.  
  366.            if (!isScreenDC) {
  367.                Eraserstroke newEraserStroke;
  368.                newEraserStroke.x = smoothX;
  369.                newEraserStroke.y = smoothY;
  370.                newEraserStroke.size = brushSize;
  371.                newEraserStroke.color = RGB(255, 255, 255);
  372.                storedEraserstrokes.push_back(newEraserStroke);
  373.            }
  374.        }
  375.    }
  376.  
  377.    // Store the current point in canvas coordinates
  378.    if (!isScreenDC) {
  379.        previousPoint = currentPoint;
  380.    }
  381. }
  382.  
  383. LRESULT CALLBACK WindowProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam) {
  384.    switch (uMsg) {
  385.        {
  386.            InitializeMemoryBitmap(hwnd);
  387.            isPaintbrushSelected = true;
  388.            break;
  389.        }
  390.  
  391.    case WM_CREATE:
  392.    {
  393.        InitializeMemoryBitmap(hwnd);
  394.        isPaintbrushSelected = true;
  395.        break;
  396.    }
  397.  
  398.    case WM_KEYDOWN:
  399.        if (wParam == VK_ADD) {  // Press "+" key to increase brush size
  400.            brushSize += 5;
  401.        }
  402.        else if (wParam == VK_SUBTRACT) {  // Press "-" key to decrease brush size
  403.            if (brushSize > 5) {
  404.                brushSize -= 5;
  405.            }
  406.        }
  407.        else if (wParam == 0x43) {  // Press "C" key to clear drawing
  408.            if (!(GetKeyState(VK_CONTROL) & 0x8000)) {
  409.                if (!(GetKeyState(VK_CONTROL) & 0x8000)) {
  410.                isClearing = true;
  411.                ClearDrawing(hwnd);
  412.            }
  413.        }
  414.            break;
  415.        }
  416.        else if (wParam == 0x50) {  // Press "P" key to select paintbrush
  417.            isPaintbrushSelected = true;
  418.            isEraserSelected = false;
  419.        }
  420.        else if (wParam == 0x45) {  // Press "E" key to select eraser
  421.            isEraserSelected = true;
  422.            isPaintbrushSelected = false;
  423.        }
  424.        else if (wParam == VK_ESCAPE) {
  425.            if (hMemoryDC) {
  426.                if (hOldBitmap) {
  427.                    SelectObject(hMemoryDC, hOldBitmap);
  428.                }
  429.                DeleteDC(hMemoryDC);
  430.            }
  431.            if (hMemoryBitmap) {
  432.                DeleteObject(hMemoryBitmap);
  433.            }
  434.            PostQuitMessage(0);
  435.        }
  436.        else if (wParam == VK_F1) {
  437.            MessageBox(hwnd, TEXT("I made an Infinite Canvas app using GDI and Memory DC, no need for bloated Godot Engine/ Frameworks or M$ Infinite Canvas Control! Eternity of effort paid off! (763 lines of code) by Entisoft Software (c) Evans Thorpemorton"), TEXT("Information"), MB_OK | MB_ICONINFORMATION);
  438.            return 0;
  439.        }
  440.        // Add this to your existing WM_KEYDOWN handler
  441.        if (wParam == VK_SPACE && !isSpacePressed) {
  442.            isSpacePressed = true;
  443.            GetCursorPos(&lastMousePos);
  444.            ScreenToClient(hwnd, &lastMousePos);
  445.            dragStart = lastMousePos;  // Store the starting point of the drag
  446.            SetCursor(LoadCursor(NULL, IDC_SIZEALL));
  447.        }
  448.        break;
  449.  
  450.    case WM_KEYUP:
  451.        if (wParam == VK_SPACE) {
  452.            isSpacePressed = false;
  453.            SetCursor(LoadCursor(NULL, IDC_ARROW));
  454.        }
  455.        break;
  456.  
  457.        // Update WM_LBUTTONDOWN handler:
  458.    case WM_LBUTTONDOWN:
  459.    {
  460.        if (isPaintbrushSelected || isEraserSelected) {
  461.            int x = LOWORD(lParam);
  462.            int y = HIWORD(lParam);
  463.            // Store initial point in canvas coordinates
  464.            previousPoint.x = x - scrollX;
  465.            previousPoint.y = y - scrollY;
  466.            isDrawing = true;
  467.        }
  468.    }
  469.    break;
  470.  
  471.    // Optional: Add this to WM_LBUTTONUP to ensure smooth lines start fresh:
  472.    case WM_LBUTTONUP:
  473.    {
  474.        isDrawing = false;
  475.        break;
  476.    }
  477.  
  478.        //new code
  479.    // Modified WM_MOUSEMOVE handler:
  480.    // Modify the WM_MOUSEMOVE handler:
  481.    case WM_MOUSEMOVE:
  482.    {
  483.        int x = LOWORD(lParam);
  484.        int y = HIWORD(lParam);
  485.  
  486.        if (isSpacePressed) {
  487.            int deltaX = x - lastMousePos.x;
  488.            int deltaY = y - lastMousePos.y;
  489.            scrollX += deltaX;
  490.            scrollY += deltaY;
  491.  
  492.            RECT clientRect;
  493.            GetClientRect(hwnd, &clientRect);
  494.            scrollX = min(0, max(-virtualWidth + clientRect.right, scrollX));
  495.            scrollY = min(0, max(-virtualHeight + clientRect.bottom, scrollY));
  496.  
  497.            lastMousePos.x = x;
  498.            lastMousePos.y = y;
  499.            InvalidateRect(hwnd, NULL, FALSE);
  500.        }
  501.        else if (isDrawing) {
  502.            if (isPaintbrushSelected || isEraserSelected) {
  503.                // Draw to memory DC first (in canvas coordinates)
  504.                DrawSmoothBrush(hMemoryDC, x - scrollX, y - scrollY, false);
  505.  
  506.                // Then draw to screen DC (in screen coordinates)
  507.                HDC hdc = GetDC(hwnd);
  508.                DrawSmoothBrush(hdc, x, y, true);
  509.                ReleaseDC(hwnd, hdc);
  510.            }
  511.        }
  512.  
  513.        if (isSpacePressed) {
  514.            SetCursor(LoadCursor(NULL, IDC_SIZEALL));
  515.        }
  516.        else if (isPaintbrushSelected || isEraserSelected) {
  517.            SetCursor(LoadCursor(NULL, IDC_CROSS));
  518.        }
  519.    }
  520.    break;
  521.  
  522.        /*    case WM_MOUSEMOVE:
  523.                if (isDrawing) {
  524.                    HDC hdc = GetDC(hwnd);
  525.                    DrawBrush(hdc, LOWORD(lParam), HIWORD(lParam));
  526.                    ReleaseDC(hwnd, hdc);
  527.                }
  528.                else if (isErasing) {
  529.                    HDC hdc = GetDC(hwnd);
  530.                    Erase(hdc, LOWORD(lParam), HIWORD(lParam));
  531.                    ReleaseDC(hwnd, hdc);
  532.                }
  533.                else if (isClearing) {
  534.                    HDC hdc = GetDC(hwnd);
  535.                    RECT rect;
  536.                    GetClientRect(hwnd, &rect);
  537.                    ClearDrawing(hdc, rect.right, rect.bottom);
  538.                    ReleaseDC(hwnd, hdc);
  539.                    isClearing = false;
  540.                }
  541.                break;
  542.        */
  543.    case WM_SIZE:
  544.    {
  545.        //old code
  546.        //InvalidateRect(hwnd, NULL, true);
  547.        
  548.        //LASTLAST::
  549.        static bool isMinimized = false;
  550.  
  551.        if (wParam == SIZE_MINIMIZED)
  552.        {
  553.            isMinimized = true;
  554.        }
  555.        else if (wParam == SIZE_MAXIMIZED || wParam == SIZE_RESTORED)
  556.        {
  557.            if (isMinimized)
  558.            {
  559.                minimizedDrawnBrushstrokes = storedBrushstrokes;
  560.                minimizedErasedBrushstrokes = storedEraserstrokes;
  561.                isMinimized = false;
  562.            }
  563.  
  564.            // Redraw the window
  565.            InvalidateRect(hwnd, NULL, TRUE);
  566.        }
  567.  
  568.        //LASTCLASS::
  569.        /*if (wParam == SIZE_MINIMIZED) {
  570.            minimizedDrawnBrushstrokes = storedBrushstrokes;
  571.            minimizedErasedBrushstrokes = storedEraserstrokes;
  572.        }
  573.        else if (wParam == SIZE_MAXIMIZED || wParam == SIZE_RESTORED) {
  574.            storedBrushstrokes = minimizedDrawnBrushstrokes;
  575.            storedEraserstrokes = minimizedErasedBrushstrokes;
  576.  
  577.            // Redraw the window
  578.            InvalidateRect(hwnd, NULL, TRUE);
  579.        }*/
  580.        //gonnareplace winner!
  581.        /*if (wParam == SIZE_MINIMIZED) {
  582.            minimizedDrawnBrushstrokes = storedBrushstrokes;
  583.            minimizedErasedBrushstrokes = storedEraserstrokes;
  584.        }
  585.        else if (wParam == SIZE_RESTORED) {
  586.            storedBrushstrokes = minimizedDrawnBrushstrokes;
  587.            storedEraserstrokes = minimizedErasedBrushstrokes;
  588.            InvalidateRect(hwnd, NULL, TRUE);
  589.        }*/
  590.        //start chatgpt new modified code
  591.        /*if (wParam == SIZE_MINIMIZED) {
  592.            // Save the drawn and erased brushstrokes when minimizing
  593.            minimizedDrawnBrushstrokes = storedBrushstrokes;
  594.            minimizedErasedBrushstrokes = storedEraserstrokes;
  595.        }
  596.        else if (wParam == SIZE_RESTORED) {
  597.            // Restore the minimized brushstrokes when restoring
  598.            storedBrushstrokes = minimizedDrawnBrushstrokes;
  599.            storedEraserstrokes = minimizedErasedBrushstrokes;
  600.            // Trigger a repaint to redraw the brushstrokes
  601.            InvalidateRect(hwnd, NULL, TRUE);
  602.        }*/
  603.        //end chatgpt new modified code
  604.  
  605.        //start working but no erase history code (adding brushstruct for eraser logic) fallbackcode
  606.        /*if (wParam == SIZE_MAXIMIZED || wParam == SIZE_RESTORED) {
  607.            RECT rect;
  608.            GetClientRect(hwnd, &rect);
  609.            //ClearDrawing(hdc, rect.right, rect.bottom);
  610.        }*/
  611.        //end working but no erase history code fallbackcode
  612.        
  613.        //new replaced chatgpt
  614.        /*if (wParam == SIZE_MAXIMIZED) {
  615.            RECT rect;
  616.            GetClientRect(hwnd, &rect);
  617.            ClearDrawing(hdc, rect.right, rect.bottom);
  618.        }*/
  619.        /*else if (wParam == SIZE_RESTORED) {
  620.             RECT rect;
  621.             GetClientRect(hwnd, &rect);
  622.             if (isPaintbrushSelected) {
  623.                 // Redraw the stored doodle contents for the Paintbrush tool
  624.                 for (const auto& point : paintbrushDoodlePoints) {
  625.                     // Use the stored points to redraw the doodle contents for the Paintbrush tool
  626.                     // Example: Draw a small circle at each point
  627.                     Ellipse(hdc, point.x - 2, point.y - 2, point.x + 2, point.y + 2);
  628.                 }
  629.             }
  630.         }*/
  631.         //old code
  632.    //new replaced chatgpt
  633.     /*else if (wParam == SIZE_RESTORED) {
  634.         RECT rect;
  635.         GetClientRect(hwnd, &rect);
  636.             // Handle resizing of contents when the window is restored
  637.             // Add code to restore doodle contents here
  638.         }
  639.         else if (wParam == SIZE_MINIMIZED) {
  640.             // Handle content when the window is minimized
  641.         }*/
  642.    }
  643.    break;
  644.  
  645.    // Modified WM_PAINT handler:
  646.    case WM_PAINT:
  647.    {
  648.        PAINTSTRUCT ps;
  649.        HDC hdc = BeginPaint(hwnd, &ps);
  650.  
  651.        // Create temporary DC for double buffering
  652.        HDC tempDC = CreateCompatibleDC(hdc);
  653.        RECT clientRect;
  654.        GetClientRect(hwnd, &clientRect);
  655.        HBITMAP tempBitmap = CreateCompatibleBitmap(hdc, clientRect.right, clientRect.bottom);
  656.        HBITMAP oldTempBitmap = (HBITMAP)SelectObject(tempDC, tempBitmap);
  657.  
  658.        // Fill with white background
  659.        HBRUSH whiteBrush = CreateSolidBrush(RGB(255, 255, 255));
  660.        FillRect(tempDC, &clientRect, whiteBrush);
  661.        DeleteObject(whiteBrush);
  662.  
  663.        // Copy visible portion from memory DC to temp DC
  664.        BitBlt(tempDC, 0, 0, clientRect.right, clientRect.bottom,
  665.            hMemoryDC, -scrollX, -scrollY, SRCCOPY);
  666.  
  667.        // Copy from temp DC to screen
  668.        BitBlt(hdc, 0, 0, clientRect.right, clientRect.bottom,
  669.            tempDC, 0, 0, SRCCOPY);
  670.  
  671.        // Cleanup
  672.        SelectObject(tempDC, oldTempBitmap);
  673.        DeleteObject(tempBitmap);
  674.        DeleteDC(tempDC);
  675.  
  676.        EndPaint(hwnd, &ps);
  677.    }
  678.    break;
  679.  
  680.    // added newly to set Normal Pointer & not Busy Pointer
  681.    case WM_SETCURSOR:
  682.        if (LOWORD(lParam) == HTCLIENT) {
  683.            SetCursor(LoadCursor(NULL, IDC_ARROW)); // Change to normal pointer
  684.            return TRUE;
  685.        }
  686.        break;
  687.  
  688.    case WM_DESTROY:
  689.    {
  690.        if (hMemoryDC) {
  691.            if (hOldBitmap) {
  692.                SelectObject(hMemoryDC, hOldBitmap);
  693.            }
  694.            DeleteDC(hMemoryDC);
  695.        }
  696.        if (hMemoryBitmap) {
  697.            DeleteObject(hMemoryBitmap);
  698.        }
  699.        PostQuitMessage(0);
  700.        break;
  701.    }
  702.  
  703.    default:
  704.        return DefWindowProc(hwnd, uMsg, wParam, lParam);
  705.    }
  706.    return 0;
  707. }
  708.  
  709. int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE, LPSTR, int nCmdShow) {
  710.    const wchar_t CLASS_NAME[] = L"DoodleAppClass";
  711.  
  712.    WNDCLASS wc = { };
  713.  
  714.    wc.lpfnWndProc = WindowProc;
  715.    wc.hInstance = hInstance;
  716.    wc.lpszClassName = CLASS_NAME;
  717.    wc.hIcon = LoadIcon(hInstance, MAKEINTRESOURCE(IDI_ICON1));     // Add this line
  718.  
  719.    RegisterClass(&wc);
  720.  
  721.    hInst = hInstance;
  722.  
  723.    HWND hwnd = CreateWindowEx(
  724.        0,
  725.        CLASS_NAME,
  726.        L"[Infinite Canvas] Doodle App (P=Brush E=Eraser C=Clear +-=BrushSize Space+Drag=Scroll F1=About)",
  727.        WS_OVERLAPPEDWINDOW | WS_MAXIMIZE,
  728.        CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT,
  729.        NULL,
  730.        NULL,
  731.        hInstance,
  732.        NULL
  733.    );
  734.  
  735.    hdc = GetDC(hwnd);
  736.    if (hwnd == NULL) {
  737.        return 0;
  738.    }
  739.  
  740.    ShowWindow(hwnd, SW_SHOWMAXIMIZED);
  741.  
  742.    //start chatgpt new modified code
  743.    /*MSG msg = {};
  744.    while (GetMessage(&msg, NULL, 0, 0)) {
  745.        TranslateMessage(&msg);
  746.        DispatchMessage(&msg);
  747.        if (msg.message == WM_SIZE) {
  748.            if (msg.wParam == SIZE_MINIMIZED) {
  749.                // Save the drawn and erased brushstrokes when minimizing
  750.                std::vector<Brushstroke> minimizedDrawnBrushstrokes = storedBrushstrokes;
  751.                std::vector<Eraserstroke> minimizedErasedBrushstrokes = storedEraserStrokes;
  752.            }
  753.            else if (msg.wParam == SIZE_RESTORED) {
  754.                // Restore the minimized brushstrokes when restoring
  755.                storedBrushstrokes = minimizedDrawnBrushstrokes;
  756.                storedEraserStrokes = minimizedErasedBrushstrokes;
  757.                // Trigger a repaint to redraw the brushstrokes
  758.                InvalidateRect(hwnd, NULL, TRUE);
  759.            }
  760.        }
  761.    }*/
  762.        //end chatgpt new modified code
  763.  
  764.        //start working but no erase history code (adding brushstruct for eraser logic) fallbackcode
  765.    MSG msg = {};
  766.    while (GetMessage(&msg, NULL, 0, 0)) {
  767.        TranslateMessage(&msg);
  768.        DispatchMessage(&msg);
  769.    }
  770.    //end working but no erase history code fallbackcode
  771.  
  772.    return 0;
  773. }
Add Comment
Please, Sign In to add comment