C++实现鼠标选中框功能代码

bool m_IsMouseScelect;
CSprite m_RectImage;
RECT m_MouseRect;
void init()
{
	m_IsMouseScelect = false;

	m_RectImage.LoadImage(".\\cards\\temp\\RGB.bmp");
	m_RectImage.SetAlpha(176);
	m_RectImage.SetRGB(176,176,176);
}
void render()
{
	if (m_IsMouseScelect && 
		m_MouseRect.bottom>0 && m_MouseRect.left>0 && m_MouseRect.right>0 && m_MouseRect.top>0)
	{
		m_RectImage.SetViewPort(m_MouseRect);
		m_RectImage.SetPosition(m_MouseRect.left,m_MouseRect.top);
		m_RectImage.Render();
	}
}
void HandleMessage(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
	switch(msg)
	{
	case WM_LBUTTONDOWN:
		{
			if (!m_IsMouseScelect)
			{
				m_IsMouseScelect=true;
				m_MouseRect.left=LOWORD(lParam);
				m_MouseRect.top=HIWORD(lParam);
			}		
		}
		break;
	case WM_LBUTTONUP:
		{
			int swap=0;
			if (m_MouseRect.left>m_MouseRect.right)
			{
				swap=m_MouseRect.left;
				m_MouseRect.left=m_MouseRect.right;
				m_MouseRect.right=swap;


			}
			if (m_MouseRect.top>m_MouseRect.bottom)
			{
				swap=m_MouseRect.top;
				m_MouseRect.top=m_MouseRect.bottom;
				m_MouseRect.bottom=swap;
			}

			m_IsMouseScelect=false;
			m_MouseRect.bottom=0;
			m_MouseRect.left=0;
			m_MouseRect.right=0;
			m_MouseRect.top=0;
		}
		break;
	case WM_MOUSEMOVE:
		{
			if (m_IsMouseScelect)
			{
				m_MouseRect.right=LOWORD(lParam);
				m_MouseRect.bottom=HIWORD(lParam);
			}
		}
		break;
}

你可能感兴趣的:(C++)