Does anybody know how MS-Paint works ???

Hi all,

While drawing (positioning the second control point of) a line (or circle or rectangle) with any line width and color in MS-Paint you can see that the whole line is drawn in one color independent of the pixels beyond the line (so no 'xor-drawing'). Even you can see no flickering.

I want to know if anybody knows how this is implemented (samples appreciated).


LVL 31
Who is Participating?

[Webinar] Streamline your web hosting managementRegister Today

nils pipenbrinckConnect With a Mentor Commented:
That's quite easy to do.. I don't know exactly how mspaint does it, but I usually do it this way:

In the wm_paint handler you first draw the bitmap, and then you draw the line/circle/whatever onto the screen.

While you are dragging around the end-point of the line you invalidate the boundary rectangle of the old line and the boundary rectangle of the new line (since the mouse moves they differ by a pixel or two).

You _only_ update your memory bitmap when the user releases the mouse-button.

To avoid flicker you have to make sure, that the window doesn't repaint it's client rectangle, and that you only invalidate the line-boundary rectangles if the endpoint has been moved.. if you don't move the mouse there is no need to repaint.

works like a charm here,

  nils pipenbrinck

ZoppoAuthor Commented:
Hi nils,

thanks for reply ...

Do you really think it's such simple?

If this works as you told, there would be following steps proceeded each time the mouse moves:

1. BitBlt from the original bitmap to a memory device context
2. Draw the line(circle) to memory device context
3. BitBlt from the memory device context to screen

for full screen size windows on slow computers this is a lot of work. Can you imagine how slow this will work on an old 80386 with a slow graphics card?

Further I do not know any other graphics programm which does drawing like this.

I'm not sure ... I'll let this question open for a while to see if other experts have comments on this.

Zopo, that is the way it is done (and I think you explained it better).  
To speed things up, you only do the BitBlt()ing for the clipping box (rectangle), rather than the whole bitmap.  It will go faster that way.  And yes it can get slow, or at least it did on old computers, especially when the object being drawn got large.  (Drawing a large circle on a 286 was noticably slow, you could move the mouse and wait a second before the circle "popped into place".)

but on a any half-decently modern computer it will go very fast.  I use this technique and don't see any speed problems with it, even with large bitblt's.
ZoppoAuthor Commented:
ok, I believe it :)

thank you also, nietod ...

nils pipenbrinckCommented:
yep.. after re-reading my message I must agree, that it my explanation is a little bit messy..

All Courses

From novice to tech pro — start learning today.