?
Solved

how can i load a bitmap and move it around the dialog box? and muz be flicker free.

Posted on 2003-03-18
19
Medium Priority
?
360 Views
Last Modified: 2013-12-03
HI there....

     i want load a bitmap(IDB_CROSS) on button press and animate around the dialog box? is it possible to posiion my bitmap in terms x and y coordinates? how to 'animate' the icon???

    and muz be flicker free.

desperate
0
Comment
Question by:xiuxiu
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 11
  • 5
  • 2
  • +1
19 Comments
 
LVL 48

Expert Comment

by:AlexFM
ID: 8158602
Add picture control to dialog template, selecting bitmap type. To show bitmap in the control use CStatic::SetBitmap function. To load bitmap from resource use CBitmap::LoadBitmap. Suppose you have CStatic class member m_static_image:

CBitmap bmp;
bmp.LoadBitmap(IDB_CROSS);
m_static_image.SetBitmap((HBITMAP)bmp.m_hObject);

I didn't understand exactly what is "animate around the dialog box". If you want to move an image, move static control on the dialog using MoveWindow.
0
 
LVL 12

Expert Comment

by:Salte
ID: 8159257
AlexFM has the answer.

If you want to draw the bitmap yourself at a specific location you can do so but it is difficult to get it flicker  free. In particular you have to be careful when you move it that you invalidate all the pixels you move from and all the pixels you move to and any pixels in between so that they are redrawn correctly.

The easiest way is to place the picture in a label or static control and place it there and just move the control around as a regular window.

This is the solution that AlexFM already told you about so I think he should get the points for this one.

Alf
0
 

Author Comment

by:xiuxiu
ID: 8164842
sorry but i can't seem to be able to see the bitmap picture. on button push i want to do this:


void CSol2Dlg::Onshow()
{
     m_cross.LoadBitmap(IDB_CROSS2);
     m_static_image.SetBitmap((HBITMAP) m_cross.m_hObject);
}

actually i want the cross to be moving to the specific position i want. Alternatively, i can delete the oldbitmap and set the new position of the bitmap again.

please help...i am willing to give any points u ask for and provided i have it :)

desperate
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 

Author Comment

by:xiuxiu
ID: 8164852
oh yes...i declare the CBitmap m_cross as a class variable. i wonder do i have to add any header??
desperate
0
 
LVL 48

Expert Comment

by:AlexFM
ID: 8164871
1) Please check the type of your static control - did you select Bitmap type?

2) Try to set bitmap directly in dialog template (just to check) - do you see it?

3) Check return value of m_cross.LoadBitmap(IDB_CROSS2);
0
 

Author Comment

by:xiuxiu
ID: 8164888
sorry there.....

   if i dun use bitmap...and i wanna remove a drawing a have drawn...how do i do it????

   if i draw
   
    pDC->MoveTo(50,100);
    pDC->LineTo(150,750);
    pDC->SelectObject(pOldPen);
    ReleaseDC(pDC);

how can i use invalidateRect to remove ONLY the particular part so as to prevent flickering
0
 

Author Comment

by:xiuxiu
ID: 8164900
sorry there.....

   if i dun use bitmap...and i wanna remove a drawing a have drawn...how do i do it????

   if i draw
   
    pDC->MoveTo(50,100);
    pDC->LineTo(150,750);
    pDC->SelectObject(pOldPen);
    ReleaseDC(pDC);

how can i use invalidateRect to remove ONLY the particular part so as to prevent flickering
0
 
LVL 48

Expert Comment

by:AlexFM
ID: 8164931
Drawing code should be placed on OnPaint function. It should look like this:

if ( m_bDrawFlag )
{
   pDC->MoveTo(50,100);
   pDC->LineTo(150,750);
   pDC->SelectObject(pOldPen);
}

To draw line write:

m_bDrawFlag = TRUE;
Invalidate();
UpdateWindow();

To remove line write:

m_bDrawFlag = FALSE;
Invalidate();
UpdateWindow();
0
 

Author Comment

by:xiuxiu
ID: 8164939
sorry there.....

   if i dun use bitmap...and i wanna remove a drawing a have drawn...how do i do it????

   if i draw
   
    pDC->MoveTo(50,100);
    pDC->LineTo(150,750);
    pDC->SelectObject(pOldPen);
    ReleaseDC(pDC);

how can i use invalidateRect to remove ONLY the particular part so as to prevent flickering
0
 

Author Comment

by:xiuxiu
ID: 8165038
Thanks AlexFM,
    U r a king soul. u have been a great help in helping me load the bitmap into my dialog box.
   
    actually the main aim of the program is to move the IDC_CROSS like a device following a signal. Can m_static_image.MoveWindow does that????

 MoveWindow( int x, int y, int nWidth, int nHeight, BOOL bRepaint = TRUE );

   but my dialog window will not move, so what do i have to put in nWidth and int nHeight??

side point:
  i ever tried to use invalidate() to refresh my dialog box before. the prob is it gives me a lot of flickering
0
 

Author Comment

by:xiuxiu
ID: 8165073
Thanks AlexFM,
    U r a king soul. u have been a great help in helping me load the bitmap into my dialog box.
   
    actually the main aim of the program is to move the IDC_CROSS like a device following a signal. Can m_static_image.MoveWindow does that????

 MoveWindow( int x, int y, int nWidth, int nHeight, BOOL bRepaint = TRUE );

   but my dialog window will not move, so what do i have to put in nWidth and int nHeight??

side point:
  i ever tried to use invalidate() to refresh my dialog box before. the prob is it gives me a lot of flickering
0
 

Author Comment

by:xiuxiu
ID: 8165078
Thanks AlexFM,
    U r a king soul. u have been a great help in helping me load the bitmap into my dialog box.
   
    actually the main aim of the program is to move the IDC_CROSS like a device following a signal. Can m_static_image.MoveWindow does that????

 MoveWindow( int x, int y, int nWidth, int nHeight, BOOL bRepaint = TRUE );

   but my dialog window will not move, so what do i have to put in nWidth and int nHeight??

side point:
  i ever tried to use invalidate() to refresh my dialog box before. the prob is it gives me a lot of flickering
0
 
LVL 48

Accepted Solution

by:
AlexFM earned 400 total points
ID: 8165094
This code moves window to one pixel in right-down direction (just a sample):

CRect rect;
m_static_image.GetWindowRect(&rect);
ScreenToClient(&rect);
rect.left += 1;
rect.top += 1;
rect.right += 1;
rect.bottom += 1;
m_static_image.MoveWindow(&rect);

Call such code when necessary (for example, by timer or as reaction on any other message).
0
 

Author Comment

by:xiuxiu
ID: 8165121
Thanks AlexFM,
    U r a king soul. u have been a great help in helping me load the bitmap into my dialog box.
   
    actually the main aim of the program is to move the IDC_CROSS like a device following a signal. Can m_static_image.MoveWindow does that????

 MoveWindow( int x, int y, int nWidth, int nHeight, BOOL bRepaint = TRUE );

   but my dialog window will not move, so what do i have to put in nWidth and int nHeight??

side point:
  i ever tried to use invalidate() to refresh my dialog box before. the prob is it gives me a lot of flickering
0
 
LVL 12

Expert Comment

by:Salte
ID: 8165494
You should be careful when calling invalidate. You probably don't want to invalidate your whole client area, only invalidate the part that actually changes. This means invalidate the area the picture moves from and invalidate the area the picture moves to. Invalidate will cause WM_PAINT message to be sent to windows and MFC will then call OnPaint.

You should absolutely place the bitmap in a control and then move that control around. It means that windows will handle the drawing, invalidating etc all by itself.

If you set the bitmap in a control you don't need any OnDraw function since windows will do the drawing. Just place the bitmap in the control and then move the control around.

However, this means that you place the bitmap at position 0,0 in the control and it stays at that position and you size the control to be so that the client area of the control (for a static control that area is the whole control, it doesn't have any other areas) becomes the size of the image. You then place the control at the position where you want to place the image.

Alf
0
 

Author Comment

by:xiuxiu
ID: 8165913
To AlexFM and all other experts:
    my bitmap is moving well. but when it moves on a white background, the previous position of my IDC_CROSS leaves behind mark(a grey rect), which is not what i wanted. how can i solve this???

    is there a way to make my bitmap transparent???
0
 
LVL 48

Expert Comment

by:AlexFM
ID: 8165956
How do you move it? What is your window - dialog, view?
0
 

Author Comment

by:xiuxiu
ID: 8170859
my window is dialog.

i want to move the IDC_CROSS. here is my code to move the IDC_CROSS in the dialog box

DrawBackgroundWhite();

CRect rect;    
m_cross.LoadBitmap(IDB_CROSS);
m_static_image.SetBitmap((HBITMAP)m_cross.m_hObject);

m_static_image.MoveWindow(200,300,10,10,FALSE);
ScreenToClient(&rect);
UpdateWindow();
for(int k=0;k<2000;k++)TRACE("wait");

m_static_image.GetWindowRect(&rect);
ScreenToClient(&rect);
rect.left -= 10;
rect.top -= 10;
rect.right -= 10;
rect.bottom -= 10;
m_static_image.MoveWindow(&rect);
UpdateWindow();

the above code is working fine as my IDB_CROSS is moving. BUT when it moves from A to B... IDB_CROSS left a patch of grey area on the white backgroud...i dun wish this to happen...what should i do? how to set the IDB_CROSS as transparent???
0
 
LVL 9

Expert Comment

by:tinchos
ID: 9510517
No comment has been added lately, so it's time to clean up this TA.
I will leave a recommendation in the Cleanup topic area that this question is:

Answered by: AlexFM

Please leave any comments here within the next seven days.

PLEASE DO NOT ACCEPT THIS COMMENT AS AN ANSWER!

Tinchos
EE Cleanup Volunteer
0

Featured Post

New benefit for Premium Members - Upgrade now!

Ready to get started with anonymous questions today? It's easy! Learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

IntroductionThis article is the second in a three part article series on the Visual Studio 2008 Debugger.  It provides tips in setting and using breakpoints. If not familiar with this debugger, you can find a basic introduction in the EE article loc…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

770 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question