Recent

Author Topic: scrollbox, scroll with mouse wheel  (Read 14881 times)

Hansvb

  • Hero Member
  • *****
  • Posts: 886
scrollbox, scroll with mouse wheel
« on: July 01, 2016, 09:43:25 pm »
Scrolling with the mouse on a scroll box works only when the mouse hovers over the Scrollbars . If the mouse is over the rest of the scrolbox, scrolling does not work when using the mouse wheel.

How can you scroll a scroll box with the mouse wheel above the scrollbox and not just above the scrollbar off the scrollbox?

molly

  • Hero Member
  • *****
  • Posts: 2330
Re: scrollbox, scroll with mouse wheel
« Reply #1 on: July 01, 2016, 11:27:15 pm »
How to decide which direction that would/should be when both horizontal and vertical scrollbars are present ? That's why it is not so logical to have it implemented by default, even though one could perhaps argue  that it should depend on which scrollbar is/are currently visible and in case both are visible scroll ........ (fill in the dots: randomly, both, horizontal, vertical, none) ?

rvk

  • Hero Member
  • *****
  • Posts: 6944
Re: scrollbox, scroll with mouse wheel
« Reply #2 on: July 01, 2016, 11:35:42 pm »
How to decide which direction that would/should be when both horizontal and vertical scrollbars are present ?
Well, I can rock my mouse-wheel left and right so scrolling up and down should be for vertical scroll  :D

Also... The Explorer.exe (and I assume other programs too) gives preference over scrolling vertical with the mouse-wheel up/down, if there is both horizontal and vertical scrollbars. Rocking the wheel side to side scroll horizontal in that case. Only when there is no vertical scrollbar, up/down scrolling scrolls horizontal.

Not sure how it is on other OSes.
(I also can't check at the moment how it's done in Delphi)
« Last Edit: July 01, 2016, 11:40:09 pm by rvk »

molly

  • Hero Member
  • *****
  • Posts: 2330
Re: scrollbox, scroll with mouse wheel
« Reply #3 on: July 01, 2016, 11:52:47 pm »
Well, I can rock my mouse-wheel left and right so scrolling up and down should be for vertical scroll  :D
Aah, yes indeed. But i am using a trackball for navigation (either that or touchscreen). I tried to 'rock' those, but that resulted in quit some unexpected behaviour :D

Point taken. Although i also have no clue on which events we should be looking at for those (let alone if every OS/widget supports it)

rvk

  • Hero Member
  • *****
  • Posts: 6944
Re: scrollbox, scroll with mouse wheel
« Reply #4 on: July 02, 2016, 12:26:52 pm »
How can you scroll a scroll box with the mouse wheel above the scrollbox and not just above the scrollbar off the scrollbox?
@Hansvb, on what OS (and Laz-version) is this. If I create a TScrollBox with some buttons (outside the visible area) the mouse-wheel scroll works for the entire scrollbox (on Windows).

Could you otherwise create a small sample-project illustrating the problem?

Hansvb

  • Hero Member
  • *****
  • Posts: 886
Re: scrollbox, scroll with mouse wheel
« Reply #5 on: July 02, 2016, 08:58:20 pm »
My mistake. I filled the scollbox complete with buttons. When i make the buttons smaller than i use the mouse scrollwheel when hovering above the scrolbox. When the mouse is above the buttons in the scrollbox nothing happens.

Windows 7 64 bit. Lazarus 1.6 32 bit

Hansvb

  • Hero Member
  • *****
  • Posts: 886
Re: scrollbox, scroll with mouse wheel
« Reply #6 on: July 02, 2016, 09:21:32 pm »
Is it possible to scroll the scrollbox when the mouse is above a button which is on the scrollbox?

rvk

  • Hero Member
  • *****
  • Posts: 6944
Re: scrollbox, scroll with mouse wheel
« Reply #7 on: July 02, 2016, 09:22:22 pm »
My mistake. I filled the scollbox complete with buttons. When i make the buttons smaller than i use the mouse scrollwheel when hovering above the scrolbox. When the mouse is above the buttons in the scrollbox nothing happens.

Windows 7 64 bit. Lazarus 1.6 32 bit
Simplest solution would be to hang this MouseWheel-event to all TButtons:
Code: Pascal  [Select][+][-]
  1. procedure TForm1.ButtonGeneralMouseWheel(Sender: TObject; Shift: TShiftState;
  2.   WheelDelta: Integer; MousePos: TPoint; var Handled: Boolean);
  3. begin
  4.   if WheelDelta > 0 then ScrollBox1.Perform(WM_VSCROLL,0,0);
  5.   if WheelDelta < 0 then ScrollBox1.Perform(WM_VSCROLL,1,0);
  6.   Handled := true;
  7. end;
You could also create a override-class for TButton above your form-declaration which does this automatically so you don't have to assign it to each button yourself.

rvk

  • Hero Member
  • *****
  • Posts: 6944
Re: scrollbox, scroll with mouse wheel
« Reply #8 on: July 02, 2016, 09:28:34 pm »
You could also create a override-class for TButton above your form-declaration which does this automatically so you don't have to assign it to each button yourself.
This is the method of overriding the TButton OnWheel event and sending the message to its parent. It will automatically do this for all your buttons on your form. Note the declaration/override above your form-declaration. It's an easy way to override components without having to modify and install them (although it might be considered by some as an hack :))

Code: Pascal  [Select][+][-]
  1. unit Unit1;
  2.  
  3. {$mode objfpc}{$H+}
  4.  
  5. interface
  6.  
  7. uses
  8.   Classes, SysUtils, FileUtil, Forms, Controls, Graphics, Dialogs,
  9.   StdCtrls, Types;
  10.  
  11. type
  12.   TButton = class(StdCtrls.TButton)
  13.   protected
  14.     function DoMouseWheel(Shift: TShiftState; WheelDelta: Integer; MousePos: TPoint): Boolean; override;
  15.   end;
  16.  
  17. type
  18.  
  19.   { TForm1 }
  20.  
  21.   TForm1 = class(TForm)
  22.     Button1: TButton;
  23.     Button2: TButton;
  24.     Button3: TButton;
  25.     Button4: TButton;
  26.     Button5: TButton;
  27.     Button6: TButton;
  28.     Button7: TButton;
  29.     ScrollBox1: TScrollBox;
  30.   private
  31.     { private declarations }
  32.   public
  33.     { public declarations }
  34.   end;
  35.  
  36. var
  37.   Form1: TForm1;
  38.  
  39. implementation
  40. uses Windows;
  41.  
  42. {$R *.lfm}
  43.  
  44. function TButton.DoMouseWheel(Shift: TShiftState; WheelDelta: Integer; MousePos: TPoint): Boolean;
  45. begin
  46.   if WheelDelta > 0 then Postmessage(Parent.Handle, WM_VSCROLL,0,0);
  47.   if WheelDelta < 0 then Postmessage(Parent.Handle, WM_VSCROLL,1,0);
  48. end;
  49.  
  50. { TForm1 }
  51.  
  52. end.

Hansvb

  • Hero Member
  • *****
  • Posts: 886
Re: scrollbox, scroll with mouse wheel
« Reply #9 on: July 03, 2016, 06:42:27 pm »
I do not understand how to  get one of both options working.

rvk

  • Hero Member
  • *****
  • Posts: 6944
Re: scrollbox, scroll with mouse wheel
« Reply #10 on: July 03, 2016, 06:46:05 pm »
I do not understand how to  get one of both options working.
Which one?

In the first one you create one event (just declare the procedure manually in the form) and hang this event in all TButtons on your TScrollbox to the MouseWheel-event (either manually or through code).

In the second you declare a TButton override above the Form1 declaration which only has one procedure overridden (the DoMouseWheel).

Both methods work by sending the scrollcommand to the parent (which should be TScrollBox).

Hansvb

  • Hero Member
  • *****
  • Posts: 886
Re: scrollbox, scroll with mouse wheel
« Reply #11 on: July 04, 2016, 07:21:19 pm »
Option one works. Thanks. I was to quick yesterday i think

micheus

  • Jr. Member
  • **
  • Posts: 62
Re: scrollbox, scroll with mouse wheel
« Reply #12 on: March 11, 2017, 03:02:34 pm »
Simplest solution would be to hang this MouseWheel-event to all TButtons:
Code: Pascal  [Select][+][-]
  1. procedure TForm1.ButtonGeneralMouseWheel(Sender: TObject; Shift: TShiftState;
  2.   WheelDelta: Integer; MousePos: TPoint; var Handled: Boolean);
  3. begin
  4.   if WheelDelta > 0 then ScrollBox1.Perform(WM_VSCROLL,0,0);
  5.   if WheelDelta < 0 then ScrollBox1.Perform(WM_VSCROLL,1,0);
  6.   Handled := true;
  7. end;
I also thought it should work automatically, anyway your code help me. Thanks.

We can use a simplified version without the use of the if statement - I do prefer that always as possible:
Quote
begin
  sbxEntidade.Perform(WM_VSCROLL, ord(WheelDelta < 0), 0);
  Handled := true;
end;
« Last Edit: April 13, 2017, 11:45:39 pm by micheus »

 

TinyPortal © 2005-2018