I very recently got into C++, and am basically learning through watching videos and reverse-engineering other people's code. Needless to say, I'm not very good at this stuff yet.
Anyway, I'm working on a program that detects if a specific color (RGB) is at a specific coordinate in another window, and if it is, the code executes a click. This is what I put together
#include <windows.h>
#include <cstdlib>
#include <iostream>
#include <cstring>
using namespace std;
void BigClick()
{
INPUT Input = { 0 };
Input.type = INPUT_MOUSE;
Input.mi.dwFlags = MOUSEEVENTF_LEFTDOWN; //push
::SendInput(1, &Input, sizeof(INPUT));
::ZeroMemory(&Input, sizeof(INPUT)); //or NULL?
Input.type = INPUT_MOUSE;
Input.mi.dwFlags = MOUSEEVENTF_LEFTUP; //release
::SendInput(1, &Input, sizeof(INPUT));
}
int main()
{
COLORREF colortosearch = RGB(0, 0, 255); // color to search
HDC hdcScreen = GetDC(NULL);
int x = 954;
int y = 540;
while (true)
{
if (::GetPixel(hdcScreen, x, y) == colortosearch)
{
// start code to click
cout << "one click completed";
BigClick();
}
}
::ReleaseDC(NULL, hdcScreen);
return 0;
}
The code compiles but it does not click even when the entire screen is blue or RGB(0,0,255)
. I know BigClick()
clicks, since I tested it by itself to make sure. What am I missing here? I'm thinking I'm not giving GetPixel
the coordinates to check in the right way, but since I'm so new it could be anything as far as I know.
I changed the fixed coordinates to follow the cursor position and your program seems to work fine. Clicks are also executed!
int main()
{
COLORREF colortosearch = RGB(0, 0, 255); // color to search
HDC hdcScreen = GetDC(NULL);
while (true)
{
POINT cursor;
GetCursorPos(&cursor);
COLORREF color = ::GetPixel(hdcScreen, cursor.x, cursor.y);
if (color == colortosearch) {
// start code to click
cout << "one click completed";
BigClick();
}
else {
int red = GetRValue(color);
int green = GetGValue(color);
int blue = GetBValue(color);
cout << "x: " << cursor.x << ", y:" << cursor.y << " --> ";
cout << "(" << red << ", " << green << ", " << blue << ")\r\n";
}
}
::ReleaseDC(NULL, hdcScreen);
return 0;
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.