Click here to Skip to main content
15,886,095 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I'm starting to use mfc in visual studio 2022, and I want to move more than 2 picture controls, one moves automatically and the other moves using the keyboard, but while the one that automatically moves its working, I cant move the other one and I want to know how to move the two of them at the same time.

What I have tried:

C++
BOOL CMyDlg::PreTranslateMessage(MSG* pMsg)
{

	int m = 10; 
	int X = (int)pMsg->wParam;

	CRect rect;
	GetWindowRect(&rect);

	if (pMsg->message == WM_KEYDOWN)
	{

		switch (X) 
		{
		case VK_LEFT:

			if (m_player.GetPosX() >= 10)
			{

				m_player.SetmovIzq(m);
				m_ImageControl.MoveWindow(m_player.GetPosX(), m_player.GetPosY(), m_player.GetAncho(), m_player.GetAltura());

			}
			break;
		case VK_RIGHT:

			if (m_player.GetPosX() <= 522)
			{

				m_player.SetmovDer(m);
				m_ImageControl.MoveWindow(m_player.GetPosX(), m_player.GetPosY(), m_player.GetAncho(), m_player.GetAltura());

			}
			break;

		default:
			break;

		}
	}

	return CDialogEx::PreTranslateMessage(pMsg);
}


void CMyDlgDlg::OnTimer(UINT_PTR nIDEvent)
{

	if (nIDEvent == 1)
	{
		int m = 7;
        
        while (m_enemy.GetPosX() <= 522)
	{
		m_enemy.SetmovR(m);
		m_ImgEnemyCtrl.MoveWindow(m_enemy.GetPosX(), m_enemy.GetPosY(), m_enemy.GetAncho(), m_enemy.GetAltura());

    Sleep(1000);
	}
    
    }
	CDialogEx::OnTimer(nIDEvent);
}
Posted
Updated 22-May-23 20:30pm
v3
Comments
CPallini 23-May-23 2:06am    
How could we help you without having a look at 'the mess'?
Oswaldo Francisco Quiñonez Jiménez 23-May-23 2:22am    
Yeah, I realize that shortly after, i already upload it

1 solution

I guess the OnTimer handler is the problem.
There you should update the enemy position just once and immediately exit, instead of performing a loop with an embedded delay.
 
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