Qt:快速屏幕采样导致鼠标光标闪烁

问题描述

您好,我使用此代码快速(约60次/秒)对屏幕进行采样并找到其平均颜色:

samplerWorker::samplerWorker(int newId)
{
    this->id = newId;
    this->screenWidth = GetSystemMetrics(SM_CXSCREEN);
    this->screenHeight = GetSystemMetrics(SM_CYSCREEN);
    this->hDesktopWnd = GetDesktopWindow();
    this->hDesktopDC = GetDC(this->hDesktopWnd);
    this->hCaptureDC = CreateCompatibleDC(this->hDesktopDC);
}

void samplerWorker::sampleAvgColor(int workerId)
{
    if(workerId != this->id)
    {
        return;
    }
    HBITMAP hCaptureBitmap = CreateCompatibleBitmap(this->hDesktopDC,this->screenWidth,this->screenHeight);
    SelectObject(hCaptureDC,hCaptureBitmap);

    BitBlt(this->hCaptureDC,this->screenHeight,this->hDesktopDC,SRCCOPY|CAPTUREBLT);

    BITMAPINFO bmi = {0};
    bmi.bmiHeader.biSize = sizeof(bmi.bmiHeader);
    bmi.bmiHeader.biWidth = this->screenWidth;
    bmi.bmiHeader.biHeight = this->screenHeight;
    bmi.bmiHeader.biPlanes = 1;
    bmi.bmiHeader.biBitCount = 32;
    bmi.bmiHeader.biCompression = BI_RGB;
    int size = this->screenWidth * this->screenHeight * 4;
    unsigned char *pPixels = new unsigned char[size];

    GetDIBits(this->hCaptureDC,hCaptureBitmap,pPixels,&bmi,DIB_RGB_COLORS);

    unsigned int avgR = 0;
    unsigned int avgG = 0;
    unsigned int avgB = 0;
    size = size /4;
    for (int i = 0; i < size; i++) {
        avgB = avgB + pPixels[4*i + 0];
        avgG = avgG + pPixels[4*i + 1];
        avgR = avgR + pPixels[4*i + 2];
    }

    avgR = avgR / (size);
    avgG = avgG / (size);
    avgB = avgB / (size);

    emit returnAvgColor(avgR,avgG,avgB);

    delete[] pPixels;

    DeleteObject(hCaptureBitmap);
}

samplerWorker是一个在几个单独的线程中工作的对象,每个samplerWorker都在一个单独的线程中,它们被一个接一个地调用,因此samplerWorker的第一个屏幕截图是id == 0,然后id == 1,依此类推,直到所有这些都被调用,然后它返回ID == 0的采样器worker。但是由于某些原因,当我以足够快的速度(大于10-15 fps)对屏幕截图时,我的光标开始闪烁。可能导致此问题的原因是什么?

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)