views:

129

answers:

2

I've used some programs with scroll bars that update the linked content while you're still dragging the "thumb", and others that don't until you release the mouse. This implies that there are different types of Windows messages involved here. But all I can find from TScrollBar is an OnScroll event which fires continually while you're dragging. It also doesn't have a OnMouseDown or OnMouseUp event. Is there any way to set up an "OnEndDragging" notification for a TScrollBar?

+4  A: 

Try this code (tested with Delphi 2009), it will fill the form client area with a random colour while you track the thumb, and fill it in yellow when the thumb is released:

procedure TForm1.ScrollBar1Scroll(Sender: TObject; ScrollCode: TScrollCode;
  var ScrollPos: Integer);
begin
  Randomize;
  if ScrollCode = scTrack then
    Color := RGB(Random(256), Random(256), Random(256));
  if ScrollCode = scEndScroll then
    Color := clYellow;
end;

The TScrollCode values map to the WPARAM values that you will find documented for WM_HSCROLL and WM_VSCROLL.

mghie
So it only needs one event because it builds this information into the ScrollCode parameter? That's good to know. Thanks!
Mason Wheeler
Exactly. This matches the way Windows sends a single scroll message for each scroll bar too, with the additional information encoded in the `wParam` and `lParam` message data.
mghie
+2  A: 

Programs that update their scrolling region "live" as the user drags the thumb are handling the sb_ThumbTrack code for the wm_HScroll and wm_VScroll messages. Those that only update when the user releases the thumb are handling the sb_ThumbPosition code.

There's a compromise on those two options, which is to update after the user hasn't moved the thumb for a little bit, even if the user hasn't actually released it yet. Handle sb_ThumbTrack, and then set a timer. If the timer fires, update the display. If another sb_ThumbTrack arrives, reset the timer.

Rob Kennedy