How to Detect that the Mouse is unmoved and button still pressed?

1.6k Views Asked by At

In Delphi, I've added a Scrollbar component (oriented vertical) to the right side of my form.

I've added a Scrollbar OnChange event so I can change the view of the form and the position of the scrollbar thumb when the user clicks on the UpArrow or DownArrow button with his mouse, and this works fine.

But the OnChange event only seems to get triggered when the mouse button is initially pressed on the arrow.

I notice all scrollbar controls repeat the command and continue scrolling while the mouse remains pressed on the arrow, and I'd like to implement this behavior.

So how can I easily detect if the user has not moved the mouse and continues to press the mouse button while the mouse remains over the arrow?


Conclusion. Somehow something in the scrollbar in my project got corrupted. After I deleted the ScrollBar, and added it again, the problem vanished.

This is one of those tricky ones that took me a lot of time to solve. Thanks for your help. I'm closing this question.

3

There are 3 best solutions below

2
On BEST ANSWER

Use the OnScroll event.

The following code adds 'xxx' to a memo as long as the mouse is held down on the scrollbar arrow button. Tested with Delphi 6.

procedure TForm1.ScrollBar1Scroll(Sender: TObject; ScrollCode: TScrollCode;
  var ScrollPos: Integer);
begin
    Memo1.Lines.Add( 'xxx' );
end;
0
On

The usual way to handle auto-repeating is to enable a TTimer and check in the OnTimer() event handler whether the action needs to be performed again, and to deactivate the timer if not. If you need sample code, I seem to remember that the SynEdit control used a similar technique for autoscrolling in drag and drop operations.

1
On

If a component does not encapsulate the behaviour you are looking for and you can't easily simulate the behaviour with the methods available you should really subclass the closest component that does most of what you need and add the behaviours that are missing.

I know that some extra work is involved but it really is the better way to go. Now with Delphi, I seem to recall that subclassed components needed a bit of extra work as well to be able to be used from the IDE for form design, maybe this has changed since version 7.