button problem

Started by
0 comments, last by jpetrie 15 years, 9 months ago

#define KEY_DOWN(vk_code) ((GetAsyncKeyState(vk_code) & 0x8000) ? 1 : 0)
#define KEY_UP(vk_code) ((GetAsyncKeyState(vk_code) & 0x8000) ? 0 : 1)

void cIntroState::HandleEvents()
{
	RECT clientRect;
	GetWindowRect(m_GameEngine->GethWnd(), &clientRect);

	GetCursorPos(&mousePos);
	POINT relMousePos;
	relMousePos.x = mousePos.x - clientRect.left;
	relMousePos.y = mousePos.y - clientRect.top;

	static bool StartButtonDown = false;
	if(KEY_DOWN(VK_LBUTTON))
	{
		if(relMousePos.x< 180 && relMousePos.x > 140 && relMousePos.y < 250 && relMousePos.y > 225 && StartButtonDown == false)
		{
				StartButtonDown = true;
		}
	}
	if(KEY_UP(VK_LBUTTON))
	{
		if(relMousePos.x< 180 && relMousePos.x > 140 && relMousePos.y < 250 && relMousePos.y > 225 && StartButtonDown)
		{
			cPlayState *playState = new cPlayState;
			m_GameEngine->getStateManagerPtr()->PushState(playState);
		}
		StartButtonDown = false;
	}
}

Hi everyone the above is my code for my button. There is a problem i run into that is when i click the mouse outside the button area, then hover over the button and release it, the button works. How can i code my button such that this wont happen?
Advertisement
Normally one responds to these sorts of things via Windows messages, which basically takes care of the issue for you -- assuming you are using actual Win32 button controls and the like.

Otherwise, you will basically need to track which control the mouse is currently over (by checking when the mouse moves) and if the button is down and mouse hasn't been captured and the mouse leaves the control, cancel the is-down state.

If you're using Win32 buttons and Windows messages, this is much easier.

This topic is closed to new replies.

Advertisement