为什么我的控件不捕获 MouseWheel 消息?

Why my Control does not catch MouseWheel messages?

使用 C++Builder 2006。

我正在尝试修改派生的 TCustomControl class,我需要 class 来捕获鼠标滚轮事件(或消息)。 我尝试使用 this Delphi question 的答案,但我没有得到事件。

这是我的相关代码:

TMouseWheelHandler.h

class PACKAGE TMouseWheelHandler : public TCustomControl
{
private:
  typedef TCustomControl inherited;

protected:
  virtual void __fastcall Paint();

  void __fastcall WMSize            (TWMSize &Message);
  void __fastcall CMMouseEnter  (TMessage &Message);
  void __fastcall CMMouseLeave  (TMessage &Message);
  void __fastcall WMMouseMove       (TMessage &Message);
  BEGIN_MESSAGE_MAP
    VCL_MESSAGE_HANDLER(WM_SIZE, TWMSize, WMSize)
    VCL_MESSAGE_HANDLER(CM_MOUSEENTER   , TMessage, CMMouseEnter)
    VCL_MESSAGE_HANDLER(CM_MOUSELEAVE   , TMessage, CMMouseLeave)
    VCL_MESSAGE_HANDLER(WM_MOUSEMOVE    , TMessage, WMMouseMove)
  END_MESSAGE_MAP(TCustomControl)

  virtual bool DoMouseWheel     (TShiftState Shift, int WheelDelta, TPoint & MousePos);
  virtual bool DoMouseWheelDown (TShiftState Shift,                 TPoint & MousePos);
  virtual bool DoMouseWheelUp       (TShiftState Shift,                 TPoint & MousePos);
  void MouseWheelHandler    (const TMessage &Message);

public:
  __fastcall TMouseWheelHandler(TComponent* Owner);

__published:
  __property OnMouseWheelUp;
  __property OnMouseWheelDown;
  __property OnMouseWheel;
  __property OnClick;
};

TMouseWheelHandler.cpp

//===========================================================================
//===========================================================================
//===========================================================================
bool TMouseWheelHandler::DoMouseWheel   (TShiftState Shift, int WheelDelta, TPoint & MousePos)
{
  OutputDebugString(__FUNC__);
  return inherited::DoMouseWheel(Shift,WheelDelta,MousePos);
}
//===========================================================================
bool TMouseWheelHandler::DoMouseWheelDown (TShiftState Shift,         TPoint & MousePos)
{
  OutputDebugString(__FUNC__);
  return inherited::DoMouseWheelDown(Shift, MousePos);
}
//===========================================================================
bool TMouseWheelHandler::DoMouseWheelUp   (TShiftState Shift,         TPoint & MousePos)
{
  OutputDebugString(__FUNC__);
  return inherited::DoMouseWheelUp(Shift, MousePos);
}
//===========================================================================
// MOUSE MANAGEMENT TAKEN FROM Whosebug DELPHI SOLUTION
//===========================================================================
void __fastcall TMouseWheelHandler::CMMouseEnter  (TMessage &Message)
{
  OutputDebugString(__FUNC__);
  SetFocus();
  MouseCapture = true;
  Refresh();
}
//===========================================================================
void __fastcall TMouseWheelHandler::CMMouseLeave  (TMessage &Message)
{
  OutputDebugString(__FUNC__);
  MouseCapture = false;
  Refresh();
}
//===========================================================================
void __fastcall TMouseWheelHandler::WMMouseMove (TMessage &Message)
{
  if (MouseCapture) {
    TPoint pt;
    POINTSTOPOINT(pt , MAKEPOINTS(Message.LParam) );

    if (PtInRect(ClientRect, pt) ) {
      OutputDebugString("MOUSE IN");
    }else{
      OutputDebugString("MOUSE OUT");
      MouseCapture = false;
      Refresh();
    }
  }else{
    OutputDebugString("NO_CAPTURE");
  }
}
//===========================================================================
void TMouseWheelHandler::MouseWheelHandler  (const TMessage &Message)
{
  OutputDebugString(__FUNC__);
  TMessage MyMessage = Message; // In the DELPHI sample Message was'nt a const
  MyMessage.Result = Perform(CM_MOUSEWHEEL, MyMessage.WParam, MyMessage.LParam);
  if (MyMessage.Result == 0) {
    inherited::MouseWheelHandler(MyMessage);
  }
}
//===========================================================================
//===========================================================================
//===========================================================================

这就是我在表单中使用控件的方式:

WH = new TMouseWheelHandler(this);
WH->Parent = Panel1;
WH->Align  = alTop;
WH->Height = Panel1->Height/2;
WH->OnMouseWheel        = GGMouseWheel;
WH->OnMouseWheelDown    = GGMouseWheelDown;
WH->OnMouseWheelUp      = GGMouseWheelUp;

void __fastcall TTestForm::GGMouseWheel(TObject *Sender, TShiftState Shift, int WheelDelta, const TPoint &MousePos, bool &Handled)
{
  OutputDebugString(__FUNC__);
}
//---------------------------------------------------------------------------

void __fastcall TTestForm::GGMouseWheelDown(TObject *Sender, TShiftState Shift, const TPoint &MousePos, bool &Handled)
{
  OutputDebugString(__FUNC__);
}
//---------------------------------------------------------------------------

void __fastcall TTestForm::GGMouseWheelUp(TObject *Sender, TShiftState Shift, const TPoint &MousePos, bool &Handled)
{
  OutputDebugString(__FUNC__);
}
//---------------------------------------------------------------------------

我从调试打印中看到 DoMouse* 函数的 none 以及 MouseWheelHandler 函数是从未调用过,而表单 GGMouseWheel* 函数是。

我只需要管理 DoMouse* 函数中的一些变量。

我做错了什么?

您的 MESSAGE_MAP 应该声明为 public 而不是 protected,因为它覆盖了虚拟 Dispatch() 方法,即 public。就个人而言,我更喜欢覆盖虚拟 WndProc() 方法而不是使用 MESSAGE_MAPs.

也就是说,不需要直接处理鼠标滚轮消息,只需覆盖虚拟 DoMouseWheel...() 方法即可。请注意,您的 DoMouseWheel...()MouseWheelHandler() 方法都声明不正确,因此它们不会覆盖同名的虚拟方法。

试试这些声明(您可以在 Controls.hpp 中查找这些声明,或者阅读文档):

DYNAMIC bool __fastcall DoMouseWheel(TShiftState Shift, int WheelDelta, const TPoint &MousePos);
DYNAMIC bool __fastcall DoMouseWheelDown(TShiftState Shift, const TPoint &MousePos);
DYNAMIC bool __fastcall DoMouseWheelUp(TShiftState Shift, const TPoint &MousePos);
DYNAMIC void __fastcall MouseWheelHandler(TMessage &Message);