We help IT Professionals succeed at work.
Get Started

Animated objects in dialog application do not stay on a background

khlauster
khlauster asked
on
662 Views
Last Modified: 2013-11-20
Hi, experts!

I am trying to build an animated object in my dialog application:

1. User clicks on button1 and object1(bitmap1) moves then draws into background a few times  at some point in dialog
2. User clicks on button2 and the object moves then draws into background  a few times at some other point in dialog.
In the code snippet bellow the drawings do not remain on dialog’s background.
All the object drawings must remain on dialog’s background and do not get erased after dialog is dragged off screen or other drawings took a place in the same application.

Working code sample is greatly appreciated!

Thank you for your help in advance

BOOL CFlickerFreeDlg::OnInitDialog()
{
	CDialog::OnInitDialog();

	BITMAP bm;
	CClientDC dc(this);

	m_picture.topleft.x = 0;m_picture.topleft.y = 0;
	m_offs.cx = 0;m_offs.cy = 0;

	m_CanvasDC->CreateCompatibleDC(&dc);
	m_PicDC->CreateCompatibleDC(&dc);
	m_FinalCanvasDC->CreateCompatibleDC(&dc);

	m_BkgBitmap->LoadBitmap(IDB_BG);
	m_BkgBitmap->GetBitmap(&bm);
	m_hBkg = bm.bmHeight;
	m_wBkg = bm.bmWidth;
	m_Bitmap->LoadBitmap(IDB_PIC);
	m_Bitmap->GetBitmap(&bm);
	m_picture.height = bm.bmHeight;
	m_picture.width = bm.bmWidth;

	return TRUE;  
}


void CFlickerFreeDlg::OnPaint()
{

	CPaintDC dc(this);
	OnDraw(&dc);
}

void CFlickerFreeDlg::OnDraw(CDC* pDC)
{
	
		CBitmap* pOldBmp1; 
		CBitmap* pOldBmp2; 

		CRect cr;

		//selects canvas 
		pOldBmp1 = m_CanvasDC->SelectObject(m_CanvasBitmap);

		//selects background, blits into canvas
		pOldBmp2 = m_PicDC->SelectObject(m_BkgBitmap);
		m_CanvasDC->BitBlt(0, 0, m_wBkg, m_hBkg, m_PicDC, 0, 0, SRCCOPY);

		//selects small pic, blits into canvas
		m_PicDC->SelectObject(m_Bitmap);
		m_CanvasDC->BitBlt(m_picture.topleft.x, 
			m_picture.topleft.y, 
			m_picture.width, 
			m_picture.height, 
			m_PicDC, 
			0, 0, SRCCOPY);

		//blits canvas on screen.The "dirty rectangle" part only.
		pDC->GetClipBox(cr);
		pDC->BitBlt(cr.left, cr.top, cr.right, cr.bottom, m_CanvasDC, cr.left, cr.top, SRCCOPY);

		//deselects all the bitmaps 
		m_CanvasDC->SelectObject(pOldBmp1);
		m_PicDC->SelectObject(pOldBmp2);


}

void CFlickerFreeDlg::OnSize(UINT nType, int cx, int cy) 
{
	ASSERT(!(m_CanvasBitmap == NULL));
	delete m_CanvasBitmap;
	m_CanvasBitmap = new CBitmap;
	CClientDC dc(this);
	m_CanvasBitmap->CreateCompatibleBitmap(&dc, cx, cy);

	CDialog::OnSize(nType, cx, cy);
}


BOOL CFlickerFreeDlg::OnEraseBkgnd(CDC* pDC) 
{
	return false;
}


CRect CFlickerFreeDlg::CalcDirtyRect(int pic_w, int pic_h, CPoint p1, CPoint p2)
{
	CSize PicSize(pic_w,pic_h);
	CRect r1(p1,PicSize);
	CRect r2(p2,PicSize);

	return r1 | r2;
}

void CFlickerFreeDlg::Animate(int count) 
{

	for (int i = 0; i < 40; i ++)
	{
	bAnimate = TRUE;

	CRect DirtyRc;
		DirtyRc.left = 0 + i*5;
		DirtyRc.right = DirtyRc.left + m_picture.width;
		DirtyRc.top = 0 + i*5;
		DirtyRc.bottom = DirtyRc.top + m_picture.height;

	m_picture.topleft.x = i*5;
	m_picture.topleft.y = i*5;

	if(i > (40 - count))
	{
	InvalidateRect(DirtyRc,1);
	}else if( i <= (40 - count) )
		{
		InvalidateRect(NULL,1);
		UpdateWindow();
		}

	bAnimate = FALSE;

	UpdateWindow();

	Sleep(100);
		
	}


}

void CFlickerFreeDlg::OnButton1() 
{
	Animate(9);
}


void CFlickerFreeDlg::OnButton2() 
{
	Animate(19);
}

Open in new window

Comment
Watch Question
Commented:
This problem has been solved!
Unlock 1 Answer and 20 Comments.
See Answer
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE