1

I have a win32 application and i want to draw a line by dragging the mouse. I use double buffering as well but the problem is it draws multiple lines in the path of the mouse. Here is my drawing code:

hdc = BeginPaint(hWnd, &ps);
hdcBack = CreateCompatibleDC(hdc); 
GetClientRect(hWnd, &windowRect); 
backBuffer = CreateCompatibleBitmap(hdc, windowRect.right, windowRect.bottom);
SelectObject(hdcBack, backBuffer); 
FloodFill(hdcBack, 0, 0, RGB(255, 255, 255)); 
BitBlt(hdcBack,0,0,windowRect.right,windowRect.bottom,hdc,0,0,SRCCOPY);
color = RGB(rand() % 255, rand() % 255, rand() % 255); 
hBrush = CreateSolidBrush(color); 
SelectObject (hdcBack, hBrush); 

MoveToEx(hdcBack,x1,y1,NULL); //x1,y1,x2,y2 are the initial click point and the current position of the mouse when keeping the left button down and dragging
LineTo(hdcBack,x2,y2);
BitBlt(hdc, 0, 0, windowRect.right, windowRect.bottom, hdcBack, 0, 0, SRCCOPY);

DeleteObject(hBrush); 
DeleteDC(hdcBack);
DeleteObject(backBuffer);
EndPaint(hWnd, &ps);

I tried also not copying the background into the buffer before drawing the line and it draws the line correctly but when i draw a new line the previously drawn line disappears. So how can i draw multiple lines with double buffering and keeping the previous drawn lines?

Nulano
  • 1,148
  • 13
  • 27
Paul
  • 13
  • 3

2 Answers2

1

The problem's inherent in the design, if I understand the requirement correctly.

The first BitBlt() copies the previous contents on to the buffer, then you draw the line, and then you apply the changes to the screen, which gives the exact results you described. This way you're only adding graphics rather than replacing, and you see several lines instead of one.

If you want to display a line that's being dragged by the mouse, you need to first fill the back-buffer with whatever background you had in mind (call it the constant-data), and paint on it the relevant graphics (call it changing-data) every mouse move. Anyway, I believe that commenting that first BitBlt() should do the trick.

Also, you're selecting a brush before calling the line functions, which use a pen. Shouldn't that call come before calling FloodFill()?

EDIT:

Use a third buffer to hold the most recent data as suggested in my comment to yours. In your "mouse-up" handler, finally draw the new line on that buffer.
So you read from that buffer in your mouse-move handler and on-paint handler, and write to it in the mouse-up handler, when the user is finally sure how he wants his line drawn.

Assaf Levy
  • 1,312
  • 1
  • 12
  • 20
  • I'm sorry for being a little unclear in my question. If I comment the first BitBlt it works on drawing a single line. But I want to draw multiple lines. But when I start to draw the next line, the previous one disappears. I want it to remain. Somehow i want to save the background but only when a line has been completely drawn. And the brush was for another purpose. I should have removed it but it doesn't affect my code. – Paul Mar 15 '11 at 08:55
  • In that case, you'll need another buffer, that will exist beyond the mouse-move handler's scope and contain the most recent _applied_ changes, and copy from it the data instead of `hdc`, before drawing the dragged line. I hope this addresses the issue. – Assaf Levy Mar 15 '11 at 09:55
0

One way to design this without using a backbuffer is to keep a dynamic list of lines that you draw each time.

Your message proc could look something like this:

  • On WM_LBUTTONDOWN, add a new line to the list with the current position as a start.
  • On WM_MOUSEMOVE (with left button down), change the end position of the latest line in the list to the current mouse pos.

On each redraw you simply iterate through the list and draw each line.

Rune Aamodt
  • 2,551
  • 2
  • 23
  • 27
  • Yes, this is correct but I cannot use such a list. The requirement is to somehow use a third buffer and save the background there but only at certain times, and I'm trying to figure out when. – Paul Mar 15 '11 at 08:58
  • OK, then you could just use the same principle and BitBlt the front buffer to the back buffer each time the mouse is released. Then when you draw, first BitBlt the backbuffer to the front buffer, then draw the current line on the front. – Rune Aamodt Mar 15 '11 at 11:31