How do I prevent drawn pixels from disappearing?

I’ve created a program in C++ that allows drawing on the screen. In this program, you can change the color using the keys g, r, and b, and adjust the thickness of the drawing tool between certain values using the t and x keys. The program also saves the coordinates of the pixels drawn to a text file, although this part is unrelated to the issue.

However, the pixels drawn on the screen can disappear, which can hinder drawing on the screen. What I mean is, for example, if you draw something in the bottom right corner of the screen and then drag a Google window over it and then pull it back, the drawing disappears. This is just one example.

#include <Windows.h>
#include <iostream>
#include <fstream>
#include <set>
#include <string>

COLORREF defaultColor = RGB(255, 255, 255); // White color

bool penState = false;
bool penStateX = false;

std::set<std::pair<int, int>> coordinatesSet; // A set to keep track of coordinates (to prevent duplicate coordinates)

// Function to write coordinates to a file
void WriteCoordinateToFile(int x, int y) {
    if (x >= 0 && x <= 1920 && y >= 0 && x <= 1080) {
        std::ofstream outputFile("coordinates.txt", std::ios::app); // Open the file for appending

        // Check if the file is opened
        if (outputFile.is_open()) {
            // Add to the coordinates set
            if (coordinatesSet.find(std::make_pair(x, y)) == coordinatesSet.end()) {
                // If not found in the coordinates set, write to file and add to set
                outputFile << x << " " << y << std::endl;
                coordinatesSet.insert(std::make_pair(x, y));
            }
            outputFile.close(); // close the file
        }
        else {
            std::cerr << "File cannot be opened!" << std::endl;
        }
    }
}

// Callback function for mouse movement
LRESULT CALLBACK MouseProc(int nCode, WPARAM wParam, LPARAM lParam) {
    if (nCode >= 0 && wParam == WM_MOUSEMOVE) {
        // If left click is not pressed, do not proceed
        if (!(GetAsyncKeyState(VK_LBUTTON) & 0x8000))
            return CallNextHookEx(NULL, nCode, wParam, lParam);

        MSLLHOOKSTRUCT* pMouseStruct = (MSLLHOOKSTRUCT*)lParam;
        // Get the coordinates of the movement
        int x = pMouseStruct->pt.x;
        int y = pMouseStruct->pt.y;

        // Print the coordinates
        // std::cout << "X: " << x << ", Y: " << y << std::endl;

        HDC hdcScreen = GetDC(NULL);
        if (penStateX) {
            for (int i = -10; i <= 10; ++i) {
                for (int j = -10; j <= 10; ++j) {
                    SetPixel(hdcScreen, x + i, y + j, defaultColor);
                }
            }
        }
        else {
            if (penState) {
                for (int i = -2; i <= 2; ++i) {
                    for (int j = -2; j <= 2; ++j) {
                        SetPixel(hdcScreen, x + i, y + j, defaultColor);
                    }
                }
            }
            else {
                SetPixel(hdcScreen, x, y, defaultColor);
            }
        }

        // Write the coordinates to file
        WriteCoordinateToFile(x, y);
    }
    // Necessary to handle other mouse events
    return CallNextHookEx(NULL, nCode, wParam, lParam);
}

// Function to change color on pressing a specific key
void ChangeColorOnKeyPress(WPARAM key) {
    if (key == 't' || key == 'T' || key == 'x' || key == 'X'){
        if (key == 't' || key == 'T') {
            if (penState) {
                penState = false;
                std::cout << "Thick pen closed." << std::endl;
            }
            else {
                penState = true;
                std::cout << "Thick pen opened." << std::endl;
            }
        }
        if (key == 'x' || key == 'X') {
            if (penStateX) {
                penStateX = false;
                std::cout << "Thick pen closed." << std::endl;
            }
            else {
                penStateX = true;
                std::cout << "Thick pen opened." << std::endl;
            }
        }
    }
    else {
        switch (key) {
        case 'G':
            defaultColor = RGB(0, 76, 0); // Dark green color
            std::cout << "Dark green color selected." << std::endl;
            break;
        case 'g':
            defaultColor = RGB(0, 255, 0); // Green color
            std::cout << "Green color selected." << std::endl;
            break;
        case 'R':
            defaultColor = RGB(76, 0, 0); // Dark red color
            std::cout << "Dark red color selected." << std::endl;
            break;
        case 'r':
            defaultColor = RGB(255, 0, 0); // Red color
            std::cout << "Red color selected." << std::endl;
            break;
        case 'B':
            defaultColor = RGB(0, 0, 76); // Dark blue color
            std::cout << "Dark blue color selected." << std::endl;
            break;
        case 'b':
            defaultColor = RGB(0, 0, 255); // Blue color
            std::cout << "Blue color selected." << std::endl;
            break;
            // Color changes for other keys can be added here.
        default:
            std::cout << "Unknown key, default color set to white." << std::endl;
            defaultColor = RGB(255, 255, 255);
            break;
        }
    }
}

// Callback function for keyboard events
LRESULT CALLBACK KeyboardProc(int nCode, WPARAM wParam, LPARAM lParam) {
    if (nCode == HC_ACTION) {
        // When a key is pressed
        if (wParam == WM_KEYDOWN) {
            KBDLLHOOKSTRUCT* pKeyStruct = (KBDLLHOOKSTRUCT*)lParam;
            ChangeColorOnKeyPress(pKeyStruct->vkCode);
        }
    }
    // Necessary to handle other keyboard events
    return CallNextHookEx(NULL, nCode, wParam, lParam);
}

int main() {
    // Install the mouse hook
    HHOOK hMouseHook = SetWindowsHookEx(WH_MOUSE_LL, MouseProc, NULL, 0);

    // Install the keyboard hook
    HHOOK hKeyboardHook = SetWindowsHookEx(WH_KEYBOARD_LL, KeyboardProc, NULL, 0);

    // Event loop
    MSG message;
    while (GetMessage(&message, NULL, 0, 0)) {
        TranslateMessage(&message);
        DispatchMessage(&message);
    }

    // Remove the hooks
    UnhookWindowsHookEx(hMouseHook);
    UnhookWindowsHookEx(hKeyboardHook);

    return 0;
}

I just switched to C++ and I haven’t figured out why this is happening yet.

I don’t want the drawn pixels to disappear as long as the program remains open.

New contributor

00prf00 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật