Question

This code is attempting to capture the image painted on the window in a box of up to 100x100 around the cursor. BitBlt is not returning 0 in either location here, and I'm pretty sure the issue is with the first function call of BitBlt, where I am trying to copy the image from the background of the window into meta, which is an HDC declared globally. In addition to just trying to create the HDC entirely in memory, I tried to create and load a bitmap of white space and capture the new image into the handle associated with it, but all that did was act like an eraser and draw a white box around the cursor as it moved. Relevant code is below, mouseRect and clientRect are global variables relating to the box around the cursor and the client rectangle, respectively. Any help is appreciated, thanks!

    case WM_CREATE:
    hInstance = ((LPCREATESTRUCT) lParam)->hInstance;
    GetClientRect(hWnd, &clientRect);
    hdc = GetDC(hWnd);
    meta = CreateCompatibleDC(hdc);
    return 0;


case WM_MOUSEMOVE:    
    x = LOWORD(lParam);
    y = HIWORD(lParam);
    hdc = GetDC(hWnd);
    BitBlt(hdc, mouseRect.left, mouseRect.top, mouseRect.right-mouseRect.left, mouseRect.bottom-mouseRect.top, meta, 0, 0, SRCCOPY);
    ReleaseDC(hWnd, meta);
    meta = CreateCompatibleDC(hdc);
    if(y<50)
        mouseRect.top = 0;
    else
        mouseRect.top = y-50;
    if(x<50)
        mouseRect.left = 0;
    else
        mouseRect.left = x-50;
    if(clientRect.right-x<50)
        mouseRect.right = clientRect.right;
    else
        mouseRect.right = x+50;
    if(clientRect.bottom-y<50)
        mouseRect.bottom = clientRect.bottom;
    else
        mouseRect.bottom = y+50;
    BitBlt(meta, 0,  0, mouseRect.right-mouseRect.left, mouseRect.bottom-mouseRect.top, hdc, mouseRect.left, mouseRect.top, SRCCOPY);
    ReleaseDC(hWnd, hdc);
    return 0;
Was it helpful?

Solution

Fixed the code, here's the corrected WM_MOUSEMOVE code

case WM_MOUSEMOVE:    
x = LOWORD(lParam);
y = HIWORD(lParam);
hdc = GetDC(hWnd);
BitBlt(hdc, mouseRect.left, mouseRect.top, mouseRect.right-mouseRect.left, mouseRect.bottom-mouseRect.top, hdcMemDC, 0, 0, SRCCOPY);
ReleaseDC(hWnd, hdcMemDC);
if(y<50)
    mouseRect.top = 0;
else
    mouseRect.top = y-50;
if(x<50)
    mouseRect.left = 0;
else
    mouseRect.left = x-50;
if(clientRect.right-x<50)
    mouseRect.right = clientRect.right;
else
    mouseRect.right = x+50;
if(clientRect.bottom-y<50)
    mouseRect.bottom = clientRect.bottom;
else
    mouseRect.bottom = y+50;
hdcMemDC = CreateCompatibleDC(hdc);
hbmScreen = CreateCompatibleBitmap(hdc, mouseRect.right-mouseRect.left, mouseRect.bottom-mouseRect.top);
SelectObject(hdcMemDC,hbmScreen);    
if(!BitBlt(hdcMemDC, 0, 0, mouseRect.right-mouseRect.left, mouseRect.bottom-mouseRect.top, hdc, mouseRect.left, mouseRect.top, SRCCOPY))
{
    MessageBox(hWnd, "BitBlt has failed", "Failed", MB_OK);
}
ReleaseDC(hWnd, hdc);
return 0;
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top