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?