Date: 02-16-2022
Return to Index
created by gbSnippets
'Primary Code:
'DDT and API methods can be used to get the selected text and the selection
'boundaries. However, the DDT approach must be supplemented with one API call.
'DDT method for textbox control. There is no DDT statement which will identify the
'range of characters selected in an edit control, so the %EM_GetSel message is required.
Control Get Text hDlg, %IDC_TextBox To temp$
Control Send hDlg, %IDC_TextBox, %EM_GetSel, VarPTR(iStartPos), VarPTR(iEndPos)) 'zero-based start/stop positions
temp$ = Mid$(temp$,iStartPos+1, iEndPos-iStartPos)
'API method for any edit control
SendMessage(hEdit, %EM_GetSel, VarPTR(iStartPos), VarPTR(iEndPos)) 'zero-based start/stop positions
TextLength = SendMessage(hEdit, %WM_GetTextLength, 0, 0)+1 '+1 to cover terminating Chr$(0)
CharsReceived = SendMessage(hEdit, %WM_GetText, TextLength, StrPtr(temp$))
temp$ = Mid$(temp$,iStartPos, iEndPos-iStartPos)
'When using API with Edit Controls, remember that line numbers and char positions
'are zero-based. Also, character positions include $crlf at end of previous lines.
'Compilable Example: (Jose Includes)
#Compiler PBWin 9, PBWin 10
#Compile Exe
#Dim All
%Unicode=1
#Include "Win32API.inc"
Global hDlg, hEdit As Dword
%IDC_TextBox = 500
%IDC_ButtonDDT = 501
%IDC_ButtonAPI = 502
Function PBMain() As Long
Local style&
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, "Get Selected Text",300,300,200,150, %WS_OverlappedWindow To hDlg
Control Add Button, hDlg, %IDC_ButtonDDT, "Get Selected Text (DDT)",20,10,160,20
Control Add Button, hDlg, %IDC_ButtonAPI, "Get Selected Text (API)",20,40,160,20
Control Add TextBox, hDlg, %IDC_TextBox, "Hello World"+$crlf+"Waiting ..."+$CrLf+"Goodbye World",20,70,160,80, style&, %WS_Ex_ClientEdge
Control Handle hDlg, %IDC_TextBox To hEdit
Dialog Show Modal hDlg Call DlgProc
End Function
CallBack Function DlgProc() As Long
Local iStartPos, iEndPos As Dword, temp$, CharsReceived, TextLength As Long
Select Case Cb.Msg
Case %WM_Command
Select Case Cb.Ctl
Case %IDC_ButtonDDT
'DDT method
Control Send hDlg, %IDC_TextBox, %EM_GetSel, VarPTR(iStartPos), VarPTR(iEndPos) 'zero-based start/stop positions
If iStartPos <> iEndPos Then
Control Get Text hDlg, %IDC_TextBox To temp$
temp$ = Mid$(temp$,iStartPos+1, iEndPos-iStartPos)
? temp$
Else
? "No text selected."
End If
Case %IDC_ButtonAPI
'API method
SendMessage(hEdit, %EM_GetSel, VarPTR(iStartPos), VarPTR(iEndPos)) 'zero-based start/stop positions
If iStartPos <> iEndPos Then
TextLength = SendMessage(hEdit, %WM_GetTextLength, 0, 0)+1 '+1 to cover terminating Chr$(0)
CharsReceived = SendMessage(hEdit, %WM_GetText, TextLength, StrPtr(temp$))
temp$ = Mid$(temp$,iStartPos, iEndPos-iStartPos)
? temp$
Else
? "No text selected."
End If
End Select
End Select
End Function
'gbs_00564
'Date: 03-10-2012
http://www.garybeene.com/sw/gbsnippets.htm