wxwidget - wxEVT_LEAVE_WINDOW事件的问题

时间:2017-11-22 08:15:38

标签: wxwidgets

我希望实现卡式UI(如屏幕截图),卡片使用wxPanel作为容器,静态文本和面板中的静态bmp。当鼠标放在卡上时,卡会放大并改变颜色,当鼠标离开卡时,尺寸和颜色将恢复为正常状态。我在wxPanel的onEnter和onLeave中添加了一些逻辑来处理缩放和颜色变化。在大多数情况下,该卡工作正常,但如果鼠标快速移动,将选择多张卡。从输出日志看,问题似乎发生在OnLeave,!GetClientRect()。包含(event.GetPosition())= false,即使鼠标实际上已经超出了卡的矩形。即使鼠标移动很快,卡怎么能按预期工作? enter image description here

void TypeCard::OnEnter(wxMouseEvent& event) {
  wxRect rect = GetClientRect();
  wxPoint pnt = event.GetPosition();
  TRACELOG_WARNING("@@@@@@ On Enter left=%d top=%d width=%d height=%d", rect.GetLeft(), rect.GetTop(), rect.GetWidth(), rect.GetHeight());
  TRACELOG_WARNING("&&&&&& On Enter x=%d y=%d", pnt.x, pnt.y);
  if (GetClientRect().Contains(event.GetPosition()) && !selected_ ) {
    TRACELOG_WARNING("&&&&&&On Enter = true");
    if (m_bZoomEnabled) {
      ZoomCard(zoom_in, 5, 5);
    }
    selected_ = true;
  } else {
    TRACELOG_WARNING("*****************************************************On Enter = false selected_ = %d", (int)selected_ );
  }

  //  have_focus_ = true;
  Refresh(false);
  Update();
  event.Skip();
}

void TypeCard::OnLeave(wxMouseEvent& event) {
  wxRect rect = GetClientRect();
  wxPoint pnt = event.GetPosition();
  TRACELOG_WARNING("@@@@@@ On OnLeave left=%d top=%d width=%d height=%d", rect.GetLeft(), rect.GetTop(), rect.GetWidth(), rect.GetHeight());
  TRACELOG_WARNING("&&&&&& On OnLeave x=%d y=%d", pnt.x, pnt.y);
  //  if (!GetClientRect().Contains(event.GetPosition())) {
  if (!rect.Contains(pnt)) {
    if (m_bZoomEnabled) {
      ZoomCard(zoom_out, 5, 5);
    }
    selected_ = false;
  } else {
    TRACELOG_WARNING("*****************************************************On OnLeave = false");
  }
  if (!selected_) {
    //      have_focus_ = false;
    Refresh(false);
    Update();
  }

  event.Skip();
}  
void TypeCard::OnPaint(wxPaintEvent& event) {
  wxPaintDC dc(this);
  wxColour colour = selected_ ? CARD_SELECT_BACKGROUND_COLOUR : COMPONENT_UNSELECT_BACKGROUND_COLOUR;
  wxCursor cur = selected_ ? wxCURSOR_HAND : wxCURSOR_ARROW;
  dc.SetPen(wxPen(colour));
  dc.SetBrush(wxBrush(colour));
  this->SetCursor(cur);

  const wxWindowList& list = this->GetChildren();
  for (wxWindowList::compatibility_iterator node = list.GetFirst(); node; node = node->GetNext()) {
    wxWindow* current = node->GetData();
    if (current) {
      current->SetBackgroundColour(colour);
      current->SetCursor(cur);
    }
  }
  dc.DrawRectangle(0, 0, this->GetSize().GetWidth(), GetSize().GetHeight());
}

0 个答案:

没有答案