This is the function I'm using:
BitBlt(memDC, 0, 0, 500, 500, hdc, 0, 0, SRCCOPY)
Thanks in advance,
Timothy.
Usually this is caused by using the memory dc to CreateCompatibleBitmap
the bitmap that will then be selected into it. The bitmap selected into
the memory dc should be based on the source dc.
--
Jeff Partch [VC++ MVP]
memDC and memBM are global.
LRESULT CALLBACK wndprocMain(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM
lParam) {
HDC hdc;
static POINT points[1];
static int index = 0;
switch(uMsg) {
case WM_CREATE:
return 0;
case WM_PAINT:
hdc = GetDC(hwnd);
BitBlt(hdc, 0, 0, 500, 500, memDC, 0, 0, SRCCOPY);
ReleaseDC(hwnd, hdc);
break;
case WM_LBUTTONDOWN: {
if (index == 0) {
// Get first point
points[index].x = LOWORD(lParam);
points[index].y = HIWORD(lParam);
index++;
} else {
// Get second point
points[index].x = LOWORD(lParam);
points[index].y = HIWORD(lParam);
HPEN hpen = CreatePen(PS_SOLID, 4, RGB(255, 0, 0));
hdc = GetDC(hwnd);
SelectObject(hdc, hpen);
// Draw line
MoveToEx(hdc, points[0].x, points[0].y, NULL);
LineTo(hdc, points[1].x, points[1].y);
// Save DC
BitBlt(memDC, 0, 0, 500, 500, hdc, 0, 0, SRCCOPY);
ReleaseDC(hwnd, hdc);
DeleteObject(hpen);
index = 0;
}
break;
}
case WM_MOUSEMOVE: {
if (index == 1) {
HPEN hpen = CreatePen(PS_SOLID, 4, RGB(255, 0, 0));
hdc = GetDC(hwnd);
SelectObject(hdc, hpen);
BitBlt(hdc, 0, 0, 500, 500, memDC, 0, 0, SRCCOPY);
MoveToEx(hdc, points[0].x, points[0].y, NULL);
LineTo(hdc, LOWORD(lParam), HIWORD(lParam));
ReleaseDC(hwnd, hdc);
DeleteObject(hpen);
}
break;
}
case WM_DESTROY:
PostQuitMessage(0);
break;
}
return DefWindowProc(hwnd, uMsg, wParam, lParam);
}
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR
lpCmdLine, int nCmdShow) {
MSG msg;
HWND hwnd;
HDC hdc;
if (!loadResources(hInstance))
return GetLastError();
if (!createWindow(hInstance, nCmdShow, hwnd))
return GetLastError();
hdc = GetDC(hwnd);
memDC = CreateCompatibleDC(hdc);
memBM = CreateCompatibleBitmap(hdc, 500, 500);
SelectObject(memDC, memBM);
ReleaseDC(hwnd, hdc);
while (GetMessage(&msg, NULL, NULL, NULL)) {
TranslateMessage(&msg);
DispatchMessage(&msg);
}
DeleteObject(memBM);
DeleteDC(memDC);
return (int)msg.wParam;
}
In addition to the suggestion to initialize your memDC, a few other
things you should consider are: 1) Use BeginPaint/EndPaint in your
WM_PAINT handler, 2) Create your static POINT point[1] as static POINT
point[2], 3) Save the old HPEN when you SelectObject your red pen, and
SelectObject it back in before you do your DeleteObject. 4) I don't
understand how your createWindow function is supposed to work -- it
looks like it should take an HWND*, but you pass it an HWND instead.