954,498 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

GDI

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!

sbcro
Newbie Poster
2 posts since Mar 2011
Reputation Points: 10
Solved Threads: 0
 

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.

orwell84
Junior Poster
102 posts since Nov 2008
Reputation Points: 17
Solved Threads: 5
 

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]

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;
}
sbcro
Newbie Poster
2 posts since Mar 2011
Reputation Points: 10
Solved Threads: 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.

orwell84
Junior Poster
102 posts since Nov 2008
Reputation Points: 17
Solved Threads: 5
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You
View similar articles that have also been tagged: