I'm trying to write a simple "MSPaintish" program, but I can't handle this one issue.

case WM_LBUTTONDOWN:{
button = true;

//save the start cords and invoke WM_MOUSEMOVE
x1=LOWORD(lParam); 
y1=HIWORD(lParam);
SendMessage(hwnd, WM_MOUSEMOVE, wParam, lParam);
break;
}
case WM_LBUTTONUP:{
button=false;
break;
}
case WM_MOUSEMOVE:{
if(button){
HDC hdc=GetDC(hwnd);
HDC hdcmem = CreateCompatibleDC(hdc);
HBITMAP bitmap = CreateCompatibleBitmap(hdc,500,600); // (1)
SelectObject(hdcmem, bitmap);

// save the end cords and draw a line
x2=LOWORD(lParam); 
y2=HIWORD(lParam);
DrawMyLine(hdc,x1,y1,x2,y2)

BitBlt(hdc,0,0,500,600,hdcmem,0,0,SRCCOPY);
DeleteDC(hdcmem);
DeleteObject(bitmap);
ReleaseDC(hwnd, hdc);
}
}

What I'm trying to achieve is to draw for example a line using double buffering so I can move the line freely changing it's cords in WM_MOUSEMOVE. The problem is when I try to draw another line obviously the first one that I've draw is gone because I cover the screen once again with the buffer bitmap (1). How can I draw another line that way without erasing what I've done before? It would be great if I could use the existing drawing as a buffer instead of blank bitmap (1), but I'm new to WinAPI and I fail to code it properly. Thanks for any input.

Recommended Answers

All 3 Replies

You will have to create a temporary surface to draw on for that one line, and when you release the mouse, it copies that surface to the main one.

You will have to create a temporary surface to draw on for that one line, and when you release the mouse, it copies that surface to the main one.

Oh I know what I should do I just can't get it to work. What do you mean by the temporary surface - a HBITMAP object ?

Oh I know what I should do I just can't get it to work. What do you mean by the temporary surface - a HBITMAP object ?

Yes, but to draw to it you need its HDC. This kind of drawing with the Windows API is hell, I'd say if it's possible, use a graphics library, if not you might be spending a while trying to get it to work.

I gave up graphics with the Windows API long ago :P

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.