Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 790085
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T21:37:43+00:00 2026-05-14T21:37:43+00:00

I have a Delphi 2007 TRichEdit with several lines in it. I want to

  • 0

I have a Delphi 2007 TRichEdit with several lines in it. I want to scroll the richedit vertically such that a specific line number if approximately centered in the visible/display area of the richedit. For example, I want to write the code for CenterLineInRichEdit in this example:

procedure CenterLineInRichEdit(Edit: TRichEdit; LineNum: Integer);
begin
  ...
  Edit.ScrollTo(...);
end;

procedure TForm1.FormCreate(Sender: TObject);
var
  REdit: TRichEdit;
  i: Integer;
begin
  REdit := TRichEdit.Create(Self);
  REdit.Parent := Self;
  Redit.ScrollBars := ssVertical;
  REdit.SetBounds(10, 10, 200, 150);
  for i := 1 to 25 do
    REdit.Lines.Add('This is line number ' + IntToStr(i));
  CenterLineInRichEdit(REdit, 13);
end;

I looked into using the WM_VSCROLL message, and it allows scrolling up/down one line, etc. but not scrolling to center a specific line.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-14T21:37:43+00:00Added an answer on May 14, 2026 at 9:37 pm

    Give this a try;

    procedure VertCenterLine(RichEdit: TRichEdit; LineNum: Integer);
    // I don't know the reason but the RichEdit 2 control in VCL does not
    // respond to the EM_SCROLLCARET in Richedit.h but it does so to the
    // constant in WinUser.h
    const
      EM_SCROLLCARET  = $00B7;
    var
      TextPos: lResult;
      Pos: TSmallPoint;
    begin
      TextPos := SendMessage(RichEdit.Handle, EM_LINEINDEX, LineNum, 0);
    
      if TextPos <> -1 then begin
        // Go to top
        SendMessage(RichEdit.Handle, EM_SETSEL, 0, 0);
        SendMessage(RichEdit.Handle, EM_SCROLLCARET, 0, 0);
    
        // Get the coordinates for the beginning of the line
        Longint(Pos) := SendMessage(RichEdit.Handle, EM_POSFROMCHAR, TextPos, 0);
    
        // Scroll from the top
        SendMessage(RichEdit.Handle, WM_VSCROLL,
            MakeWParam(SB_THUMBPOSITION, Pos.y - RichEdit.ClientHeight div 2), 0);
    
        // Optionally set the caret to the beginning of the line
        SendMessage(RichEdit.Handle, EM_SETSEL, TextPos, TextPos);
      end;
    end;
    

    The below is an alternative in that it centers the first occurance of a string instead of a line number;

    procedure VertCenterText(RichEdit: TRichEdit; Text: string);
    const
      EM_SCROLLCARET  = $00B7;
    var
      FindText: TFindText;
      TextPos: lResult;
      Pos: TSmallPoint;
    begin
      FindText.chrg.cpMin := 0;
      FindText.chrg.cpMax := -1;
      FindText.lpstrText := PChar(Text);
      TextPos := SendMessage(RichEdit.Handle, EM_FINDTEXT,
          FR_DOWN or FR_WHOLEWORD, Longint(@FindText));
    
      if TextPos <> -1 then begin
        SendMessage(RichEdit.Handle, EM_SETSEL, 0, 0);
        SendMessage(RichEdit.Handle, EM_SCROLLCARET, 0, 0);
    
        Longint(Pos) := SendMessage(RichEdit.Handle, EM_POSFROMCHAR, TextPos, 0);
        SendMessage(RichEdit.Handle, WM_VSCROLL,
            MakeWParam(SB_THUMBPOSITION, Pos.y - RichEdit.ClientHeight div 2), 0);
    
        SendMessage(RichEdit.Handle, EM_SETSEL, TextPos, TextPos);
      end;
    end;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a frame in a Delphi 2007 application that I want to embed
I'm using Delphi 2007 Pro. I have a runtime package that includes a number
I have a SOAP 1.1 client that was generated with the Delphi 2007 WSDL
I have Delphi 2007 code that looks like this: procedure WriteString(Stream: TFileStream; var SourceBuffer:
I have a Delphi 2007 application that has a TAnimate control with a FindFile
I have Windows Server 2008 R2 Machine that is running a Delphi 2007 application.
I have a simple Delphi (2007) procedure that given a TDataSet and a (sub)list
I have a Delphi 2007 DBGrid that I'd like to allow the user to
I have a Windows CGI created with Delphi 2007 using CGIExpert that I need
I have just ported several of our home-made Outlook COM-addins from Delphi 2007 to

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.