0

嗨,所以我正在尝试制作一个小的 UFO 位图(已经处理好的绘图/绘画)可以在屏幕上拖动。我似乎无法更新 UFO 位置,然后从 MouseButtonDown() 函数(鼠标事件处理程序的简化代码)重复重绘。关于相应地检测拖动和重绘的任何建议?相关功能的代码如下:

void MouseButtonDown(int x, int y, BOOL bLeft)
{
  if (bLeft)
  {
    while(_bMouseMoving == true && _bMouseDragRelease == false)  {  
        _iSaucerX = x - (_pSaucer->GetWidth() / 2);
        _iSaucerY = y - (_pSaucer->GetHeight() / 2);
        InvalidateRect(_pGame->GetWindow(), NULL, FALSE);
    }

        // Set the saucer position to the mouse position
        _iSaucerX = x - (_pSaucer->GetWidth() / 2);
        _iSaucerY = y - (_pSaucer->GetHeight() / 2);

  }
  else
  {
    // Stop the saucer
    _iSpeedX = 0;
    _iSpeedY = 0;
  }
}

void MouseButtonUp(int x, int y, BOOL bLeft)
{
    _bMouseDragRelease = true;
}

void MouseMove(int x, int y)
{
    _bMouseMoving = true;  
}
4

1 回答 1

0

为了澄清 chris 所说的,您只会收到 WM_xBUTTONDOWN 消息一次,并且您需要使用它来切换拖动状态,当您收到 WM_MOUSEMOVE 消息时可以查询该状态。

当您在拖动状态下收到鼠标移动消息时,您需要使 ufo 所在的矩形周围以及它所在位置周围的矩形无效。

使矩形无效会导致 WM_PAINT 消息,您可以在其中重绘 ufo 后面的任何内容,并将 ufo 放在新位置。

或者您可以在拖动时作弊并使 UFO 成为光标 :)

于 2013-05-08T01:12:09.343 回答