简体   繁体   English

TScrollbox在C ++ Builder中使用鼠标滚轮滚动

[英]TScrollbox scrolling using mouse wheel in c++ builder

I have added a scroll box inside a tabsheet in my form. 我在表单的选项卡中添加了一个滚动框。 I am able to scroll the contents when i clicked scroll up and down button in scroll box. 单击滚动框中的向上和向下按钮时,可以滚动内容。 But i want to scroll the contents using mouse wheel up and down. 但是我想使用鼠标滚轮上下滚动内容。 I have tried the below code. 我尝试了下面的代码。

void __fastcall TForm1::ScrollBox1MouseWheelUp(TObject *Sender, TShiftState Shift,
      TPoint &MousePos, bool &Handled)
{
    Form1->ScrollBox1->VertScrollBar->Position -= 3;
}

void __fastcall TForm1::ScrollBox1MouseWheelDown(TObject *Sender, TShiftState Shift,
      TPoint &MousePos, bool &Handled)
{
    Form1->ScrollBox1->VertScrollBar->Position += 3;
}

But the scrolling is not happening and control does not come over here when i tried to debug it. 但是,当我尝试对其进行调试时,滚动不会发生,并且控制也不会出现在此处。 How to do scrolling using mouse wheel in scroll box? 如何使用鼠标滚轮在滚动框中进行滚动?

You could implement the MouseWheel event on the owner form, and then test for Control under mouse is a TScrollBox: 您可以在所有者窗体上实现MouseWheel事件,然后在mouse下测试控件是否为TScrollBox:

procedure TForm1.FormMouseWheel(Sender: TObject; Shift: TShiftState; WheelDelta: Integer; MousePos: TPoint; var Handled: Boolean);
var
  i: Integer;
  TheScrollBox: TScrollBox;
  Control: TWinControl;
begin
  Control := FindVCLWindow(Mouse.CursorPos);
  Handled := Control is TScrollBox;

  if not Handled then
    exit;

  TheScrollBox := Control as TScrollBox;

  for i := 1 to Mouse.WheelScrollLines do
    try
      if WheelDelta > 0 then
        TheScrollBox.Perform(WM_VSCROLL, SB_LINEUP, 0)
      else
        TheScrollBox.Perform(WM_VSCROLL, SB_LINEDOWN, 0);
    finally
      TheScrollBox.Perform(WM_VSCROLL, SB_ENDSCROLL, 0);
    end;
end;

Another and more generic approach would be to implement Application.OnMessage : 另一种更通用的方法是实现Application.OnMessage:

Add a TApplicationEvents component to you mainform and the implement a OnMessageEvent : 在您的主窗体中添加一个TApplicationEvents组件,并实现一个OnMessageEvent:

procedure TForm1.ApplicationEvents1Message(var Msg: tagMSG; var Handled: Boolean);
var
  i, Count: Integer;
  Control: TWinControl;
begin
  if Msg.message <> WM_MOUSEWHEEL then
    exit;

  Control := FindVCLWindow(Mouse.CursorPos);
  Handled := Control <> nil;

  if not Handled then
    exit;

  Count := 1;
  if Smallint(loWord(Msg.wParam)) = MK_CONTROL then
    Count := 5;

  try
    for i := 1 to Count do
      if Smallint(HiWord(Msg.wParam)) > 0 then
        Control.Perform(WM_VSCROLL, SB_LINEUP, 0)
      else
        Control.Perform(WM_VSCROLL, SB_LINEDOWN, 0);
  finally
    Control.Perform(WM_VSCROLL, SB_ENDSCROLL, 0);
  end;
end;

PS: WPARAM and LPARAM is documented in the MSDN PS: MSDN中记录了WPARAM和LPARAM

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM