使用GetWindowDC(NULL)时为什么BitBlt会变慢?

时间:2016-04-06 19:29:26

标签: c++ winapi gdi+

目的 此代码是我正在处理的屏幕捕获/应用程序捕获应用程序的一部分。

问题 在这个例子中,我试图捕获最大化并适合整个屏幕的Firefox。我省略了BitBlt之后的代码,因为它不相关。我的问题是,为什么当我使用GetWindowDC(NULL)获取整个屏幕的设备上下文时,Bitblt的使用时间要长10倍?与使用GetWindowDC(FireFoxHWND)时相反?

代码

#include <windows.h>
#include <iostream>

double PCFreq = 0.0;
__int64 CounterStart = 0;

void StartCounter()
{
    LARGE_INTEGER li;
    if (!QueryPerformanceFrequency(&li))
    {
        std::cout << "QueryPerformanceFrequency failed!\n";
    }

    PCFreq = double(li.QuadPart) / 1000.0;

    QueryPerformanceCounter(&li);
    CounterStart = li.QuadPart;
}
double GetCounter()
{
    LARGE_INTEGER li;
    QueryPerformanceCounter(&li);
    return double(li.QuadPart - CounterStart) / PCFreq;
}

void BitBltFunc(HWND hwnd)
{
    HDC device_context = GetWindowDC(hwnd);
    if (!device_context)
    {
        std::cout << "Error creating device context" << std::endl;
    }

    HDC hdc_capture = CreateCompatibleDC(device_context);

    int width = GetDeviceCaps(device_context, HORZRES);
    int height = GetDeviceCaps(device_context, VERTRES);
    HBITMAP hbm_screen = CreateCompatibleBitmap(device_context, width, height);

    if (!hbm_screen)
    {
        std::cout << "Couldn't create compatible bitmap" << std::endl;
    }
    else
    {
        HBITMAP bmp_old = (HBITMAP)SelectObject(hdc_capture, hbm_screen);
        if (!BitBlt(hdc_capture, 0, 0, width, height, device_context, 0, 0, SRCCOPY))
        {
            std::cout << "Bitblt failed" << std::endl;
        }

        SelectObject(hdc_capture, bmp_old);
        DeleteObject(hbm_screen);
    }
    DeleteObject(hdc_capture);
    ReleaseDC(hwnd, device_context);
    std::cout << "Finished BitBlt" << std::endl;
}

int main()
{
    HWND mozilla_window = FindWindow(TEXT("MozillaWindowClass"), NULL);
    if (!mozilla_window)
    {
        std::cout << "Cannot find mozilla window" << std::endl;
    }
    for (int i = 0; i < 30; i++)
    {
        StartCounter();
        BitBltFunc(mozilla_window);
        std::cout << GetCounter() << "\n";
    }
    for (int j = 0; j < 30; j++)
    {
        StartCounter();
        BitBltFunc(NULL);
        std::cout << GetCounter() << std::endl;
    }

    return 0;
}

这两个像素不是相同数量的像素吗?

0 个答案:

没有答案