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?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

nils pipenbrinckCommented:
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


Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
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..

It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft Development

From novice to tech pro — start learning today.