Jump to content

Sky Slate Blueberry Blackcurrant Watermelon Strawberry Orange Banana Apple Emerald Chocolate

Scroll to the middle of a HiEdit or RichText

  • Please log in to reply
6 replies to this topic
  • Members
  • 1029 posts
  • Last active:
  • Joined: 01 Oct 2005
I'm trying to figure out how to scroll a particular line to the center of the HiEdit control. I'm unable to get it to work consistently without knowing how many lines are currently visible on the control.

Does anyone know of a message or consistent methodology of identifying how many lines that are currently visible on the HiEdit control? This same message/methodology should work on Rich Text control.


Edit: Solved. See this post for the solution.

  • Moderators
  • 3622 posts
  • Last active: Dec 24 2015 02:21 AM
  • Joined: 07 Oct 2006
You can take a look at this to see a method of getting various info about a scrollbar and its position. Perhaps you can tweak it to your needs.

  • Moderators
  • 4323 posts
  • Last active: Jan 28 2016 01:38 AM
  • Joined: 08 Apr 2009
[VxE], I thought about this script of yours, but if there are multiple lines and the scrollbar is disabled, he won't be able to perform the desired action.


Antonio Fran├ža -- git.io -- github.com -- ahk4.net -- sites.google.com -- ahkscript.org

Member of the AHK community since 08/Apr/2009. Moderator since mid-2012.

  • Moderators
  • 3622 posts
  • Last active: Dec 24 2015 02:21 AM
  • Joined: 07 Oct 2006
True, if the scrollbar is disabled, it probably means that all of the lines are visible. In which case, he won't be able to scroll anything in that control at all. The reason I posted that script and linked to it was to hopefully give a nudge in the right direction, or at least a hint towards a possible solution.

  • Members
  • 1029 posts
  • Last active:
  • Joined: 01 Oct 2005
Thank you for your feedback and your interest.

Although I had fun playing with your example script, commands based on the scrollbar are not really in scope for my problem. The reason: The scrollbar controls scrolling the entire contents of the control. I'm only interested (for this one problem) in scrolling a particular line within the visible list of lines. For example, if 20 lines (out of 5,000) were displayed and my selected line were at the top of the 20 visible lines, I want to scroll up ~10 lines so that the selected line is in the middle of the visible list of lines. There is a message to identify the 1st visible line, EM_GETFIRSTVISIBLELINE, and I can identify where my guy is but like I said, without a message to tell me how many total visible lines are displayed, there is not an easy way to accurately scroll to the middle assuming a dynamic control size, a user-determined font, and user-determined font size.

I've written a work-around that scrolls the control until the selected line is no longer visible and I use those calculations to determine how many lines to scroll back so that the selected line is in the middle. It's accurate except when the visible lines are at the very top of the control. I was just hoping that there was a message that told me how many lines were visible.

Once again, thank you for your feedback and your interest. I appreciate it.

  • Guests
  • Last active:
  • Joined: --



  • Members
  • 1029 posts
  • Last active:
  • Joined: 01 Oct 2005
✓  Best Answer
Based on the EM_POSFROMCHAR message clue from n-l-i-d, I figured out how to determine the total number of visible lines in an Edit, RichText, or HiEdit control. Here's a code example just in case anyone else needs to figure out how to do this.

;-- Note: hEdit is the handle to an Edit, RichEdit, or HiEdit control.
;   Disclaimer: I've only tried this on a HiEdit control.

SendMessage EM_GETFIRSTVISIBLELINE,0,0,,ahk_id %hEdit%
;;;;;MsgBox FirstVisibleLine=%FirstVisibleLine%

SendMessage EM_GETRECT,0,&RECT_Structure,,ahk_id %hEdit%
Left  :=NumGet(RECT_Structure,0,"Int")
Top   :=NumGet(RECT_Structure,4,"Int")
Right :=NumGet(RECT_Structure,8,"Int")
;;;;;MsgBox Left=%Left%, Top=%Top%, Right=%Right%, Bottom=%Bottom%

;;;;;NumPut(0,POINTL_Structure,0,"Int")          ;-- x
;;;;;NumPut(Bottom,POINTL_Structure,4,"Int")     ;-- y
SendMessage EM_CHARFROMPOS,0,(Bottom<<16)|0,,ahk_id %hEdit%
    ;-- lParam=DWORD=(y<<16)|x where x=LOWORD and y=HIWORD
    ;   Note: This method works for standard Edit controls and for the HiEdit
    ;   control.  For a RichEdit control, you might need to send a pointer to a
    ;   POINTL structure instead of sending the x/y DWORD.  See the 
    ;   documentation for EM_CHARFROMPOS for more information.

;;;;;MsgBox CharPos=%CharPos%

SendMessage EM_LINEFROMCHAR,CharPos,0,,ahk_id %hEdit%
;;;;;MsgBox LastVisibleLine=%LastVisibleLine%

MsgBox Number of visible lines=%NumberOfVisibleLines%

Thanks for your help.