Writing to the screen from the screen using BitBlt

2019-09-09 11:51发布

I'm trying to copy parts of the screen, modify them, and then copy those parts back to the screen. This is in windows, using C++.

The general structure of my code looks like this:

HDC hdcDesktop = GetDC(NULL);
HDC hdcTemp = CreateCompatibleDC(hdcDesktop);

BitBlt(hdcTemp, 0, 0, 100, 100, hdcDesktop, 100, 100, SRCCOPY);
BitBlt(hdcDesktop, rand() % 1920, rand() % 1080, 100, 100, hdcTemp, 0, 0, SRCCOPY);

This should copy a 100x100 portion of the screen starting at (100, 100) to some random part of the screen. This doesn't work, however. What am I doing wrong?

1条回答
爱情/是我丢掉的垃圾
2楼-- · 2019-09-09 12:37

There are a few issues with this code:

  1. As indicated by the docs, CreateCompatibleDC creates a new in-memory image that is 1x1 pixels. This is obviously not big enough for your 100x100 chunk of image. You should probably use CreateCompatibleBitmap.

  2. The coordinates passed to BitBlt are:

    • top-left cornder of destination (nXDest, nYDest)
    • width/height of copy (nWidth,nHeight)
    • top-left corner of soruce (nXSrc,nYSrc)

    in that order. You seem to be confusing nXSrc/nYSrc with nWidth/nHeight. Check your numbers.

  3. Wanton abuse of the desktop surface like this may actually (1) be disallowed and (2) produce unexpected results. Be careful what you are attempting to achieve.

查看更多
登录 后发表回答