我有一个 win32 应用程序,我想通过拖动鼠标来画一条线。我也使用双缓冲,但问题是它在鼠标路径中绘制了多条线。这是我的绘图代码:
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);
我也尝试在画线之前不将背景复制到缓冲区中,它会正确地画线,但是当我画一条新线时,之前画的线会消失。那么如何使用双缓冲绘制多条线并保留先前绘制的线?