4

In Delphi, in many TWinControl descendants, such as in my exact case, the TSynEdit control, how would I read the horizontal and vertical scroll bar position?

I have been searching around in the source code for my particular control, and in the base class documentation for TWinControl, and can't figure it out.

Is there a general VCL specific way to do this, or should I do this via Win32 API calls?

Warren P
  • 65,725
  • 40
  • 181
  • 316
  • 2
    Are you tried using the [GetScrollBarInfo](https://msdn.microsoft.com/en-us/library/windows/desktop/bb787581(v=vs.85).aspx) function? – RRUZ Oct 07 '15 at 18:01
  • Yes, doesn't work for me, with either OBJID_CLIENT or OBJID_HSCROLL, but maybe I'm doing that wrong. `if GetScrollBarInfo(SynEditCtrl.Handle, Integer(OBJID_CLIENT), SB) then ...` is returning false. – Warren P Oct 07 '15 at 18:07
  • I can get pretty close using `SynEditCtrl.LeftChar` which will be 0 if the horizontal scroll position is all the way left. – Warren P Oct 07 '15 at 18:15
  • 1
    This works for me http://pastebin.com/Xwywf04D – RRUZ Oct 07 '15 at 18:20
  • 1
    That must be correct, RRUZ. Something must be weird in my exact case. Maybe you can't call this in the context of WM_PAINT. I think this would be a better question if I change this to ask "for any TWinControl" because I don't see any such question here. Your answer is good. You could post that. I don't see any duplicate of this question on SO. – Warren P Oct 07 '15 at 18:20

1 Answers1

4

The GetScrollBarInfo function is the way to get the scrollbars position of any TWinControl. You must pass the handle of the control, an OBJID_VSCROLL or OBJID_HSCROLL value and a SCROLLBARINFO structure to return the data.

Check this sample

var
 LBarInfo: TScrollBarInfo;
begin
 LBarInfo.cbSize := SizeOf(LBarInfo);
 if GetScrollBarInfo(SynEdit1.Handle, Integer(OBJID_VSCROLL), LBarInfo) then
  ShowMessage(Format('Left %d Top %d Height %d Width %d', [LBarInfo.rcScrollBar.Left, LBarInfo.rcScrollBar.Top, LBarInfo.rcScrollBar.Height, LBarInfo.rcScrollBar.Width]));
end;
RRUZ
  • 134,889
  • 20
  • 356
  • 483
  • 1
    IN my very weird case, I am seeing that I need to do this at some time other than during my particular TWinControl's owner-draw paint events. – Warren P Oct 07 '15 at 19:00