How to get the scrollbar positon in a RichEdit control (Views: 728)
Problem/Question/Abstract: Is it possible to read the actual position of a scrollbar from a richedit-component? Answer: Solve 1: YES, you can get the position of the scroll bar, by several ways. Here is some code that gets the scroll bar position for a RichEdit. I also subclass the RichRdit to get the WM_VSCROLL message and move the Claret to the top line plus one character private { Private declarations } PRichEdWndProc, POldWndProc: Pointer; procedure RichEdWndProc(var Msg: TMessage); procedure TForm1.FormCreate(Sender: TObject); begin {subclass the richedit to get the windows messages} PRichEdWndProc := MakeObjectInstance(RichEdWndProc); POldWndProc := Pointer(SetWindowLong(RichEdit1.Handle, GWL_WNDPROC, Integer(PRichEdWndProc))); end; procedure TForm1.FormClose(Sender: TObject; var Action: TCloseAction); begin {un-sublass it so you app can close} if Assigned(PRichEdWndProc) then begin SetWindowLong(RichEdit1.Handle, GWL_WNDPROC, Integer(POldWndProc)); FreeObjectInstance(PRichEdWndProc); end; end; procedure TForm1.RichEdWndProc(var Msg: TMessage); begin Msg.Result := CallWindowProc(POldWndProc, RichEdit1.Handle, Msg.Msg, Msg.WParam, Msg.LParam); if (Msg.Msg = WM_VSCROLL) and (LOWORD(Msg.wParam) = SB_THUMBTRACK) then begin {the SB_THUMBTRACK message is only sent when the user moves the scroll bar position} Label1.Caption := 'thumb Pos is ' + IntToStr(HIWORD(Msg.Wparam)); RichEdit1.SelStart := RichEdit1.Perform(EM_LINEINDEX, RichEdit1.Perform(EM_GETFIRSTVISIBLELINE, 0, 0), 0) + 1; end; end; procedure TForm1.but_REditInfoClick(Sender: TObject); var ScrolPos, VisLineOne: Integer; ScrollInfo1: TScrollInfo; begin {use some windows messages to get scroll position and other info} VisLineOne := RichEdit1.Perform(EM_GETFIRSTVISIBLELINE, 0, 0); ScrollInfo1.cbSize := SizeOf(TScrollInfo); ScrollInfo1.fMask := SIF_RANGE; GetScrollInfo(RichEdit1.Handle, SB_VERT, ScrollInfo1); ScrolPos := GetScrollPos(RichEdit1.Handle, SB_VERT); ShowMessage('ScrolPos is ' + IntToStr(ScrolPos) + ' First Vis Line is ' + IntToStr(VisLineOne) + ' Scroll max is ' + IntToStr(ScrollInfo1.nMax)); end; Solve 2: The most simple way function GetHorzScrollBarPosition: Integer; begin // constants: SB_HORZ = 0, SB_VERT = 1, // SB_BOTH = 3, SB_THUMBPOSITION = 4 Result := GetScrollPos(RichEdit1.Handle, SB_HORZ); // Result is the number of pixels the RichEdit is scrolled end; |