Date: 02-16-2022
Return to Index
created by gbSnippets
'Sometimes you'll want to put content into 2 side-by-side controls, and
'have them scroll synchronously so you can compare the contents of each.
'Compiler Comments:
'This code is written to compile with PBWin10. To compile with PBWin9,
'add this line:
#Include "CommCtrl.inc"
'Primary Code:
'The EventMask must be set to include ENM_Scroll
SendMessage hRichEdit1, %EM_SETEVENTMASK, 0, %ENM_SELCHANGE Or %ENM_CHANGE Or %ENM_LINK Or %ENM_Scroll
'Then, in the dialog callback function, this code ensures that the top
'position of each richedit control are synchronized
CallBack Function DlgProc() As Long
Select Case CB.Msg
Case CB.Msg = %WM_Command
Select Case CB.Ctl
Case %IDC_RichEdit1
If CB.Ctlmsg = %EN_VScroll Then
Control Send hDlg, %IDC_RichEdit1, %EM_EXGETSEL, 0, VarPTR(P) 'save original position
Control Send hDlg, %IDC_RichEdit1, %EM_GetScrollPos, 0, VarPTR(P)
Control Send hDlg, %IDC_RichEdit2, %EM_SetScrollPos, 0, VarPTR(P)
End If
Case %IDC_RichEdit2
If CB.Ctlmsg = %EN_VScroll Then
Control Send hDlg, %IDC_RichEdit2, %EM_EXGETSEL, 0, VarPTR(P) 'save original position
Control Send hDlg, %IDC_RichEdit2, %EM_GetScrollPos, 0, VarPTR(P)
Control Send hDlg, %IDC_RichEdit1, %EM_SetScrollPos, 0, VarPTR(P)
End If
End Select
End Select
End Function
'Compilable Example: (Jose Includes)
'This demonstrates two side-by-side RichEdit controls with enough text in them
'to force display of the scrollbars. The two will scroll together when clicking
'either arrow button, clicking between the arrow button and the thumb or
'keyboard events (HOME, End, Page UP, Page DOWN, UP ARROW, Or DOWN ARROW),
'Clicking the scroll bar mouse does not scroll synchronously. Using the thumb
'as an async scroll provides an easy way to switch between sync/async scrolling.
#Compiler PBWin 10
#Compile EXE
#Dim All
%Unicode=1
#Include "win32api.inc"
#Include "RichEdit.inc"
Global hDlg as DWord, hRichEdit1 as DWord, hRichEdit2 as DWord
%IDC_RichEdit1 = 500 : %IDC_RichEdit2 = 501
Function PBMain() As Long
Dialog New Pixels, 0, "Test Code",300,300,300,175, %WS_OverlappedWindow To hDlg
CreateRichEditComparisonControls
Dialog Show Modal hDlg Call DlgProc
End Function
CallBack Function DlgProc() As Long
Local w As Long, h As Long, P As Point
Select Case CB.Msg
Case %WM_Size
Dialog Get Client hDlg To w, h
Control Set Size hDlg, %IDC_RichEdit1, w/2-10, h-15
Control Set Loc hDlg, %IDC_RichEdit2, w/2+5, 10
Control Set Size hDlg, %IDC_RichEdit2, w/2-10, h-15
Case %WM_Command
Select Case CB.Ctl
Case %IDC_RichEdit1
If CB.Ctlmsg = %EN_VScroll Then
Control Send hDlg, %IDC_RichEdit1, %EM_EXGETSEL, 0, VarPTR(P) 'save original position
Control Send hDlg, %IDC_RichEdit1, %EM_GetScrollPos, 0, VarPTR(P)
Control Send hDlg, %IDC_RichEdit2, %EM_SetScrollPos, 0, VarPTR(P)
End If
Case %IDC_RichEdit2
If CB.Ctlmsg = %EN_VScroll Then
Control Send hDlg, %IDC_RichEdit2, %EM_EXGETSEL, 0, VarPTR(P) 'save original position
Control Send hDlg, %IDC_RichEdit2, %EM_GetScrollPos, 0, VarPTR(P)
Control Send hDlg, %IDC_RichEdit1, %EM_SetScrollPos, 0, VarPTR(P)
End If
End Select
End Select
End Function
Sub CreateRichEditComparisonControls
Local temp$, i as Long
temp$ = "this is sample text" + $crlf + "and so is this."
For i = 1 to 3 : temp$ = temp$ + $crlf + temp$ : Next i
LoadLibrary("riched32.dll")
Call InitCommonControls
Control Add "RichEdit", hDlg, %IDC_RichEdit1, "", 5, 10, 160, 175, _
%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, _
%WS_Ex_ClientEdge
Control Handle hDlg, %IDC_RichEdit1 To hRichEdit1
SendMessage hRichEdit1, %EM_SETEVENTMASK, 0, %ENM_SELCHANGE Or %ENM_CHANGE Or %ENM_LINK Or %ENM_Scroll
SendMessage hRichEdit1, %EM_SETLIMITTEXT, &H100000&, 0
Control Add "RichEdit", hDlg, %IDC_RichEdit2, "", 105, 10, 160, 175, _
%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, _
%WS_Ex_ClientEdge
Control Handle hDlg, %IDC_RichEdit2 To hRichEdit2
SendMessage hRichEdit2, %EM_SETEVENTMASK, 0, %ENM_SELCHANGE Or %ENM_CHANGE Or %ENM_LINK Or %ENM_Scroll
SendMessage hRichEdit2, %EM_SETLIMITTEXT, &H100000&, 0
Control Set Text hDlg, %IDC_RichEdit1, temp$
Control Set Text hDlg, %IDC_RichEdit2, temp$
End Sub
'gbs_00286
'Date: 03-10-2012
http://www.garybeene.com/sw/gbsnippets.htm