开发者

C++ win32 project double buffering

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, backBu开发者_高级运维ffer); 
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?


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.


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜