Urgent:Using a BMP picture to show the Joystick movement!

Hi! When I change the X axis and Y axis position of Joystick, I would like to use a BMP picture from Resoruce editor (LoadBitmap(IDB_XXX) ) to show the Joystick movement. Right now I do understand how to calculate the relative position on the Client area of Windows , so you do not teach me how to do it. My big problem is that I do not know how to erase the BMP picture at the old position and generate this BMP picture at the new position  after Joystick moved. If you could write a piece of code for doing this, and explain it a little, I would be very grateful.
Thank a lot!.
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.

In general this problem is not trivial. Ideally if you bitmap picture is ralatevly small, as small as cursur icon the best way is to use cursor. If it is larger what you need to do is save screen area where you want to display your bitmap into some temporary bitmap using screen DC. Then draw your bitmap on this place. when you need to move your bitmap you first should draw the bitmap you saved back on screen and then repeat the same steps. The problem is if you bitmap is rather large you will get terrible flickering. In this case DirectDraw is the only one way.

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
In your view class do this (pseudo code)

Get the old bitmap rect
Set the new bitmap rect
InvalidateRect for the old bitmap rectangle
InvalidateRect for the new bitmap rectangle

This should do it.

PS: Please reject the current proposed answer so I can propose this as an answer if you find it more helpful

If user moves losticj rather fast bitmaps will not be updated since InvalidateRect doesn't update rectangle immediately. This will reasult in tail of bitmaps. moreover he needs to draw bitmap anywhere and InvalidateRect will also result in marking invalidte rectangle for many windows so it will further slow down drawing.
you must directly update bitmaps by obtaining device context of the display. Using GDI BitBlt function is good solution if the bitmap is not large otherwise I told only DD can help
Upgrade your Question Security!

Your question, your audience. Choose who sees your identity—and your question—with question security.

Where does he say he needs to "draw bitmap anywhere" ??

I assume he is just drawing it in his view (he talks about client coordinates).  So there will not be 'many windows' to invalidate at all.

It is true that InvalidateRect does not update the view immediatle - it just marks it as dirty.  But the UpdateWindow call will do the repainting of the old and new rectangles.  I don't see how you would end up with this trail of bitmaps you mention.

If you want to avoid flicker and get a smoother display, then an offscreen bitmap for rendering would be the way to go.

Another technique is to render the bitmap with an XOR, so then you just paint the bitmap in the old position then in the new position, and the bitmap will move.  Of course, the colours may look a bit screwy as they are xor'd with the background, but that may be suitable for his application.

Yes, i agree it is not clear that he wants to move bitmap only inside his window client area. But my cooments are still valid. Using your method will cause terrible flickering or rectangle will not be redrwn at all if user moves jostick rather fast.
What I suggest is double buffering and this is the same method used by Windows when you move one window over another. It saves invalide rectangle or region and then blits it back.
The 'save-under' double buffering you recommend is not really good.  It will resotre the OLD backgroudn (just before the bitmap was drawn at the rect) .. if the background has since changed (eg. you are moving the joystick slowly) then you'll end up with 'rubbish' being repainted .. not the current image.

If you are drawing in your client area only (and not already using directx) then use the invalidate/updatewindow method, and use an offscreen bitmap for rendering (the famouse CMemDC class should help with that .. check it out at http://www.codeguru.com

BTW: I think that your 'double-buffering' (ie save-under) method is NOT used (usually) when moving one window over another in Windows .. instead Windows invalidates and send paint messages.

I guess while user is moving joystick any windows is not updated. This is at least truth between two speps of moving joystick. I know from my experience that InvalidateRect works extremely slow since it doesn't update rectangle immediately but sets WM_PAINT message in message queue. Taking into account that WM_PAINT message is not processed if there are ohter messages in message queue ans several WM_PAINT messages are combined into single one fast moving of joystick will result in not updating rectangle.
Concerning your comment about invalidating rectangle when moving one window over another you are right. Windows doesn't update invalide rectangle with InvalidateRect when a menu is tracked. It uses the mechanism I mentioned.
Yes, InvalidateRect marks a rect of the window as requireing update.  These would normally be accumulated as you state.

However, UpdateWindow causes an IMMEDITATE repaint.  So there is not delay.

According to the docs it...

Updates the client area by sending a WM_PAINT message if the update region is not empty. The UpdateWindow member function sends a WM_PAINT message directly, bypassing the application queue. If the update region is empty, WM_PAINT is not sent.

Note that it BYPASSES the message queue .. so the usually delayed WM_PAINT handling (when there are no other message etc) is bypassed as well.

I think we now need to here from jennyH so we know what she(?) is wanting to do so we can get a solution for her particular requirements.
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
System Programming

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.