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.
00prf00 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.