Date: 02-16-2022
Return to Index
created by gbSnippets
'Often, a programmer needs to ensure that a particular line is displayed at the
'top of a RichEdit control. A combination of messages will do the trick.
'Compiler Comments:
'This code is written to compile with PBWin10. To compile with PBWin9,
'add this line:
#Include "CommCtrl.inc"
'Primary Code:
'The code uses GetFirstVisible to get the current topmost visible line and then
'LineScroll to move to the desired line. LineScroll can scroll up or down.
'Because the topmost Line can be partially obscured, it's necessary to use
'the code twice - once to ensure that a full line is visibile and a second time
'to move un-ambiguously to the desired line.
Local iTopLine&, IDesiredLine&
iDesiredLine& = 12
'first time aligns a line at the top of the control, but it may not be the desired line
iTopLine& = SendMessage (hRichEdit, %EM_GetFirstVisibleLine, 0,0)
SendMessage hRichEdit, %EM_LineScroll, 0, iDesiredLine& - iTopLine&
'the second time ensures the proper result
iTopLine& = SendMessage (hRichEdit, %EM_GetFirstVisibleLine, 0,0)
SendMessage hRichEdit, %EM_LineScroll, 0, iDesiredLine& - iTopLine&
'Compilable Example: (Jose Includes)
#Compiler PBWin 10
#Compile EXE
#Dim All
%Unicode=1
#Include "win32api.inc"
#Include "RichEdit.inc"
Global hDlg as DWord, hRichEdit as DWord
%ID_RichEdit = 500
Function PBMain() As Long
Local style&, buf$, i as Long
For i = 0 to 50 : buf$ = buf$ + Format$(i, " 000 ") + "Line" + $crlf : Next i : buf$ = Trim$(buf$, $crlf)
style& = %WS_Child Or %WS_Visible Or %ES_MultiLine Or %WS_VScroll Or %ES_AutoHScroll _
Or %WS_HScroll Or %ES_AutoVScroll Or %ES_WantReturn Or %ES_NoHideSel Or %WS_TabStop
Dialog New Pixels, 0, "Test Code",300,300,200,150, %WS_OverlappedWindow To hDlg
Control Add Button, hDlg, 100,"Line 12 to top", 30,10,140,20
LoadLibrary("riched32.dll") : InitCommonControls
Control Add "RichEdit", hDlg, %ID_RichEdit, buf$,20,40,160,100, style&, %WS_EX_ClientEdge
Control Handle hDlg, %ID_RichEdit To hRichEdit
Dialog Show Modal hDlg Call DlgProc
End Function
CallBack Function DlgProc() As Long
If CB.Msg = %WM_Command AND CB.Ctl = 100 AND CB.Ctlmsg = %BN_Clicked Then
Local iTopLine&, IDesiredLine&
iDesiredLine& = 12
'first time aligns a line at the top of the control
iTopLine& = SendMessage (hRichEdit, %EM_GetFirstVisibleLine, 0,0)
SendMessage hRichEdit, %EM_LineScroll, 0, iDesiredLine& - iTopLine&
'the second time ensures the proper result
iTopLine& = SendMessage (hRichEdit, %EM_GetFirstVisibleLine, 0,0)
SendMessage hRichEdit, %EM_LineScroll, 0, iDesiredLine& - iTopLine&
End If
End Function
'gbs_00409
'Date: 03-10-2012
http://www.garybeene.com/sw/gbsnippets.htm