Click here to Skip to main content
15,890,527 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have uses SetTimer() function for viewing the movement of buttons but the problem is that it does not show the movement here is the code,what is the problem from this code it moves the button but does not show it's movement.

void CMoveDlg::OnButton1() 
{
	// TODO: Add your control notification handler code here
	
		UINT m_tim=SetTimer(1,10000,NULL);
		Move(m_tim);
}
	

void CMoveDlg::Move(UINT m_tim)
{

//ON PLACE OF BUTTON 3	
m_butn1.MoveWindow(30,10,130,60,true);
Sleep(800);
m_butn1.MoveWindow(60,10,130,60,true);
Sleep(800);
m_butn1.MoveWindow(80,10,130,60,true);
Sleep(800);
m_butn1.MoveWindow(100,10,130,60,true);
Sleep(800);
m_butn1.MoveWindow(120,10,130,60,true);
Sleep(800);
m_butn1.MoveWindow(140,10,130,60,true);
Sleep(800);
m_butn1.MoveWindow(220,260,130,60,true);
Sleep(800);
m_butn1.MoveWindow(300,150,130,60,true);
Sleep(800);
m_butn1.MoveWindow(320,130,130,60,true);
Sleep(800);
m_butn1.MoveWindow(320,125,130,60,true);
Sleep(800);
m_butn1.MoveWindow(320,120,130,60,true);
stop(m_tim);
	
}
void CMoveDlg::OnTimer(UINT nIDEvent) 
{
	// TODO: Add your message handler code here and/or call default
	m_butn1.MoveWindow(30,10,130,60,true);
Sleep(800);
m_butn1.MoveWindow(60,10,130,60,true);
Sleep(800);
m_butn1.MoveWindow(80,10,130,60,true);
Sleep(800);
m_butn1.MoveWindow(100,10,130,60,true);
Sleep(800);
m_butn1.MoveWindow(120,10,130,60,true);
Sleep(800);
m_butn1.MoveWindow(140,10,130,60,true);
Sleep(800);
m_butn1.MoveWindow(220,260,130,60,true);
Sleep(800);
m_butn1.MoveWindow(300,150,130,60,true);
Sleep(800);
m_butn1.MoveWindow(320,130,130,60,true);
Sleep(800);
m_butn1.MoveWindow(320,125,130,60,true);
Sleep(800);
m_butn1.MoveWindow(320,120,130,60,true);
stop(nIDEvent);
	CDialog::OnTimer(nIDEvent);
}

C#
void CMoveDlg::stop(UINT m_tim)
{
KillTimer(m_tim);
}
Posted
Updated 20-Jan-10 0:04am
v3

Immediately after you start your timer you call the Move() function which moves the button around, kills the timer, and returns. Chances are that the actual timer never gets to fire, since it is stopped before the first timer event gets sent.
 
Share this answer
 
Instead of putting all the movement code into one block with sleeps in between, the OnTimer function should only move your button for one single step. It means that in the OnTimer function, you will have only one call to MoveWindow (and no sleep at all). You will need to check the current button position and update it. Once you want to stop moving the button, you have to kill the timer.
 
Share this answer
 
void CMoveDlg::NewMoveParameters()
{
  m_dx = (rand() % 10) - 4;
  m_dy = (rand() % 10) - 4;
  m_step = 0;
  m_stepMax = rand() % 40 + 40;
}
void CMoveDlg::OnButton1() 
{
  // UINT m_tim, INT m_x, m_y, m_dx, m_dy, m_step, m_stepMax are CMoveDig member vars
  m_x = m_y = 150; // button start position
  NewMoveParameters();	
  m_tim = SetTimer(1, 100, NULL);
}
void CMoveDlg::OnTimer(UINT nIDEvent) 
{
  if ( nIDEvent == m_tim)
  {
    if ( m_step == m_stepMax )
      NewMoveParameters();
    m_x += m_dx;
    m_y += m_dy;
    m_butn1.MoveWindow(m_x, m_y,130,60, true);
    m_step++;
  }
  CDialog::OnTimer(nIDEvent);
}
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900