Date: 02-16-2022
Return to Index
created by gbSnippets
'Often a programmer needs to modify the content of a RichEdit control and then
'restore the control to it's previous position (same top line of text). The EM_GetScrollPos
'and EM_SetScrollPos message will do the trick nicely.
'Compiler Comments:
'This code is written to compile with PBWin10. To compile with PBWin9,
'add this line:
#Include "CommCtrl.inc"
'Primary Code:
'This code is supplied as a message that will save/restore the position
Sub RichEditPosition(Task$)
Static P As point, OldPD As CharRange
Select Case Task$
Case "save"
Control Send hDlg, %IDC_RichEdit, %EM_EXGETSEL, 0, VarPTR(OldPD) 'save original position
Control Send hDlg, %IDC_RichEdit, %EM_GetScrollPos, 0, VarPTR(P)
Case "restore"
OldPD.cpmax = OldPD.cpmin
Control Send hDlg, %IDC_RichEdit, %EM_EXSETSEL, 0, VarPTR(OldPD) 'get original position
Control Send hDlg, %IDC_RichEdit, %EM_SetScrollPos, 0, VarPTR(P)
End Select
End Sub
'Compilable Example: (Jose Includes)
'In this example, press the Save button, scroll around, then press the Restore
'button to return to the starting point.
#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$
buf$ = "1This is sample" + $CrLf + "2text for the" + $CrLf + "3edit control."
buf$ = buf$ + $CrLf + "4This is sample" + $CrLf + "5text for the" + $CrLf + "6edit control."
buf$ = buf$ + $CrLf + "7This is sample" + $CrLf + "8text for the" + $CrLf + "9edit control."
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,200, %WS_OverlappedWindow To hDlg
Control Add Button, hDlg, 100,"Save", 30,10,140,20
Control Add Button, hDlg, 150,"Restore", 30,40,140,20
LoadLibrary("riched32.dll") : InitCommonControls
Control Add "RichEdit", hDlg, %ID_RichEdit, buf$,20,70,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 RichEditPosition("save")
If CB.Msg = %WM_Command AND CB.Ctl = 150 AND CB.Ctlmsg = %BN_Clicked Then RichEditPosition("restore")
End Function
Sub RichEditPosition(Task$)
Static P As point, OldPD As CharRange
Select Case Task$
Case "save"
Control Send hDlg, %ID_RichEdit, %EM_EXGETSEL, 0, VarPTR(OldPD) 'save original position
Control Send hDlg, %ID_RichEdit, %EM_GetScrollPos, 0, VarPTR(P)
Case "restore"
OldPD.cpmax = OldPD.cpmin
Control Send hDlg, %ID_RichEdit, %EM_EXSETSEL, 0, VarPTR(OldPD) 'get original position
Control Send hDlg, %ID_RichEdit, %EM_SetScrollPos, 0, VarPTR(P)
End Select
End Sub
'gbs_00230
'Date: 03-10-2012
http://www.garybeene.com/sw/gbsnippets.htm