Hi,

I am trying to draw some rectangles by pressing the mouse button and dragging. Each time a new rectangle is being drawn, the previous one disappears. The question is how can I avoid this? I am programming in C.

Thanks!

Recommended Answers

All 3 Replies

I don't know the answer to your question, but I know what would really help someone who does: some code.
Off the top of my head, though, I would guess that some sort of draw() function is being implemented each time the mouse is released. It's likely that this just outputs a bitmap. You'd probably have to save the previous rectangle somehow and then include that in the data the program is drawing. The draw() function could also turn on a pixel if it's on in one data structure OR the other.
Then again, without code, it's pretty hard to tell.

Hi,

Thanks for your help orwell! You're right: I output a bitmap at each repaint. How could I save the content of the drawing and then paint over it? Below is how the WM_PAINT message is handled.

[code=c]
static HDC hdcBack;
static RECT windowRect;
static HBITMAP backBuffer;
...

case WM_PAINT:
  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));
  hBrush = CreateSolidBrush(color);
  SelectObject (hdcBack, hBrush); 
  Rectangle (hdcBack, x1, y1, x2, y2);
  BitBlt(hdc, 0, 0, windowRect.right, windowRect.bottom, hdcBack, 0, 0, SRCCOPY); 
  DeleteObject(hBrush); 
  DeleteDC(hdcBack); 
  DeleteObject(backBuffer);
  EndPaint(hWnd, &ps);
  break;
case WM_DESTROY:
  PostQuitMessage(0);
  break;
case WM_LBUTTONDOWN: 
  x1 = LOWORD(lParam); 
  y1 = HIWORD(lParam);  
  break;
case WM_MOUSEMOVE:
  if(wParam & MK_LBUTTON) { 
    x2 = LOWORD(lParam); 
    y2 = HIWORD(lParam); 
    InvalidateRect(hWnd, NULL, FALSE); 
  }
default:
  return DefWindowProc(hWnd, message, wParam, lParam);
}
return 0;
}

You'd have to combine the pixel values. I suppose you could do a logical OR between the two bitmaps. Basically, if a pixel is on in either of the bitmaps (one for the existing drawing, one for the new rectangle), the pixel will be on (black) for the resulting bitmap.
In terms of procedure, you could just loop through each (2-dimensional?) array and do an OR on each pair of values.

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.