有没有一种方法可以在随机位置上反转颜色?

时间:2019-11-27 15:00:07

标签: c++ winapi

我想在屏幕上拍摄颜色并将其反转,然后使用BitBlt函数将其打印回屏幕,因为我知道该功能可以拍摄照片,复制并粘贴 它回到屏幕上。

基本上我想要的是使用DSTINVERT反转颜色,它在屏幕上获取颜色,将它们反转并打印回屏幕。

所以我用了这个,我想在屏幕上做一个随机的位置,我用GetSystemMetrics函数获取屏幕像素并在屏幕上做一个随机打印。 但这对我不起作用,什么也不打印,什么也不做,如果我输入我的随机分辨率(1920x1080),它可以工作并随机打印,但是我希望程序在所有屏幕上也能正常工作。

不胜感激。

#include <iostream>
#include <Windows.h>
#include <time.h>

using namespace std;

int main()
{
    srand(time(0));
    HDC desktop = GetDC(GetDesktopWindow());

    int width = GetSystemMetrics(SM_CXSCREEN);
    int height = GetSystemMetrics(SM_CYSCREEN);

    for (int i = 0; i < 1000; i++) {

        // Position on the screen
        int x = rand() % width;
        int y = rand() % height;

        Sleep(3000);
        BitBlt(desktop, width, height, 250, 250, desktop, width, height, DSTINVERT);
    }
    return 0;
}

如果有人解释这两个参数(7、8),我也很喜欢,因为我对这样做的了解不多,所以我做了同样的随机性。

1 个答案:

答案 0 :(得分:1)

对不起!我的错。

我已经使用宽度和高度作为参数。 我需要使用x和y。

我的程序现在运行良好。

但是我仍然想请您解释一下参数(8、7)。谢谢!

#include <iostream>
#include <Windows.h>
#include <time.h>

using namespace std;

int main()
{
    srand(time(0));
    HDC desktop = GetDC(GetDesktopWindow());

    int width = GetSystemMetrics(SM_CXSCREEN);
    int height = GetSystemMetrics(SM_CYSCREEN);

    for (int i = 0; i < 1000; i++) {

        // Size 
        int size1 = rand() % 500;
        int size2 = rand() % 500;

        // Position on the screen
        int x = rand() % width;
        int y = rand() % height;

        Sleep(3000);
        BitBlt(desktop, x, y, size1, size2, desktop, x, y, DSTINVERT);
    }
    return 0;
}