0

使用 C++Builder 2006。

我正在尝试修改 TCustomControl 派生类,并且我需要该类来捕获鼠标滚轮事件(或消息)。我试图使用这个 Delphi 问题的答案,但我没有得到这个事件。

这是我的相关代码:

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 STACKOVERFLOW 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 * 函数和MouseWheelHandler函数,而 Form GGMouseWheel * 函数是。

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

我究竟做错了什么?

4

1 回答 1

3

MESSAGE_MAP应该声明publicnot protected,因为它覆盖了虚拟Dispatch()方法 is 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);
于 2021-09-22T19:53:52.720 回答