Problem with changing the position of cross mark on BMP image

Hi,

I am displaying BMP image with cross mark by using DirectDraw. I used the following functions.
-------------------------------------------------------------------------------------------------------------------------------
HBITMAP hbitmap = (HBITMAP)LoadImage(hinstance,bmp_name,IMAGE_BITMAP,0,0,LR_LOADFROMFILE);
HBITMAP old_bitmap = (HBITMAP)SelectObject(hdc,hbitmap);
HDC surfaceDC = NULL;
HRESULT result = backSurface->GetDC(&surfaceDC);
StretchBlt(surfaceDC,0,0,reSize.cx,reSize.cy,hdc,0,0,imageSize.cx,imageSize.cy,SRCCOPY);
HPEN pen = CreatePen(PS_DOT, 1, RGB(0,0,0));
HPEN oldPen = (HPEN)SelectObject(surfaceDC, pen);
MoveToEx(surfaceDC, 0, pt.y, NULL);
LineTo(surfaceDC, reSize.cx, pt.y);
MoveToEx(surfaceDC, pt.x, 0, NULL);
LineTo(surfaceDC, pt.x, reSize.cy);
SelectObject(surfaceDC, oldPen);
backSurface->ReleaseDC(surfaceDC);
SelectObject(hdc,old_bitmap);
DeleteObject(hbitmap);
----------------------------------------------------------------------------------------------------------

I am moving the cross on image by using key events, means for each event i am redrawing image and new position of cross. Its working well.
The problem is, when i am showing multiple images in loop with 40ms sleep time, i am unable to move the cross until it completes the loop. No event is working when loop is running. How can i move cross, ie independent from loop and image?

Thanking you
Vihar
vihar123Asked:
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.

AlexFMCommented:
There are two ways to solve this problem:

1) Instead of loop with Sleep refresh the screen by timer. This allows to handle keyboard messages between timer ticks and change pt value.

2) Using VB-like DoEvents finction:

void DoEvents()
{
    MSG msg;
   
    while ( ::PeekMessage(&msg, NULL, 0, 0, PM_NOREMOVE ) )
    {  
        if ( ::GetMessage(&msg, NULL, 0, 0))
        {
            ::TranslateMessage(&msg);
            ::DispatchMessage(&msg);
        }
        else
        {
            break;
        }
    }
}

void DoSomething()
{
     while ( ... )   // some loop
    {
         // Draw, Sleep etc. - any code

         DoEvents();    // handle events. Here pt value may be changed
    }
}

By the way, this is also the way to stop long process if user presses Stop or Cancel button. Notice that DoEvents handles any messages, for example, user may close window etc. - your program should be ready for this. When I use this way, I usually disable all UI elements except those which I expect to handle.
0

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
AlexFMCommented:
Another function you can use in this situation is this:

void CGeneralFunctions::DoEventsEx(UINT wMsgFilterMin, UINT wMsgFilterMax)
{
    MSG msg;
   
    while ( ::PeekMessage(&msg, NULL, wMsgFilterMin, wMsgFilterMax, PM_NOREMOVE ) )
    {  
        if ( ::GetMessage(&msg, NULL, wMsgFilterMin, wMsgFilterMax))
        {
            ::TranslateMessage(&msg);
            ::DispatchMessage(&msg);
        }
        else
        {
            break;
        }
    }
}

Passing some message interval to this function, you handle only these messages, other messages remain in the message queue. You can use this function passing interval of keyboard messages. In this case current window will not be unexpectedly closed inside of loop.
0
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.