Search Up and Down

Category: Controls - RichEdit

Date: 02-16-2022

Return to Index


 
'Compilable Example:  (Jose Includes)
#Compile Exe
#Dim All
 
%Unicode = 1
 
#Include "Win32Api.inc"
 
Global hDlg As Dword
 
%RichEdit01          = 101
%ButtonSearchPrev    = 301
%ButtonSearchNext    = 302
%FR_UP               =   0 'For clarity only
 
Function PBMain()
 Local hlib        As Dword
 Dialog New %HWND_Desktop, "Richtext EM_FINDTEXTEX", , , 300, 130, _
 %WS_Caption Or %WS_MinimizeBox Or %WS_SysMenu, %WS_Ex_Left To hDlg
 hLib = LoadLibrary("MsFtEdit.dll")
 Control Add "RichEdit50W", hDlg, %RichEdit01, _
 Repeat$(3 ,"Search for text, " & $CrLf & "previous or next to the caret." & $CrLf & $CrLf) & _
 "If search control is empty, text selection will be used as input.", _
 5, 5, 290, 90, %WS_Child Or %WS_Visible Or %WS_TabStop Or %ES_SAVESEL Or %WS_VScroll Or %WS_HScroll Or _
 %ES_Left Or %ES_MultiLine Or %ES_AutoVScroll Or %ES_AutoHScroll Or %ES_WantReturn Or %ES_NoHideSel, _
 %WS_Ex_ClientEdge Or %WS_Ex_Left Or %WS_Ex_LtrReading Or %WS_Ex_RightScrollbar
 Control Add Button, hDlg, %ButtonSearchPrev, "<", 50, 99, 25, ', %BS_VCENTER
 Control Add Button, hDlg, %ButtonSearchNext, ">", 214, 99, 25, ', %BS_VCENTER
 Dialog Show Modal hDlg, Call MainProc
End Function
 
CallBack Function MainProc()
 Local  sTextToSearchFor As String
 Static hRichEdit        As Dword
 Select Case As Long CbMsg
   Case %WM_InitDialog
     hRichEdit = GetDlgItem(CbHndl, %RichEdit01)
   Case %WM_Command
     Select Case As Long CbCtl
       Case %ButtonSearchNext, %ButtonSearchPrev
           sTextToSearchFor = "previous"
           SearchText(hRichEdit, sTextToSearchFor, IIf(CbCtl = %ButtonSearchNext, %FR_DOWN, %FR_UP))
           Control Set Focus hDlg, %RichEdit01
     End Select
 End Select
End Function
 
Function SearchText(hRichEdit As Dword, ByVal sTextToSearchFor As WString, SearchDirection As Long) As Long
 Local  FindTextText         As FINDTEXTEX
 Local  NextMatch, SelStart, SelEnd As Long
 
 SendMessage(hRichEdit, %EM_GETSEL, VarPtr(SelStart), VarPtr(SelEnd))
 FindTextText.lpStrText = StrPtr(sTextToSearchFor)
 
 If SearchDirection = %FR_Down Then
   FindTextText.chrg.cpMin = SelEnd + 1 'Search from current position
   FindTextText.chrg.cpMax = -1         '- till the end
 Else '%FR_UP
   FindTextText.chrg.cpMin = SelStart   'Search from current position
   FindTextText.chrg.cpMax = 0          '- up to the start
 End If
 
 NextMatch = SendMessage(hRichEdit, %EM_FINDTEXTEX, SearchDirection, VarPtr(FindTextText)) 'Return next match or -1 for no more
 
 If NextMatch = -1 Then WinBeep(250,300) :Exit Function
 SendMessage(hRichEdit, %EM_SETSEL, FindTextText.chrgText.cpMin, FindTextText.chrgText.cpMax)
 
End Function
 
 
 


created by gbSnippets
http://www.garybeene.com/sw/gbsnippets.htm