Date: 02-16-2022
Return to Index
created by gbSnippets
'DDT method for textbox control
Control Send hDlg, %IDC_TextBox, %EM_GetSel, VarPTR(StartPos), VarPTR(EndPos) 'start/stop of selection, caret position if no selection
Control Get Text hDlg, %IDC_TextBox TO temp$
Control Set Text hDlg, %IDC_Textbox, Left$(temp$,StartPos-1) + ReplaceText$ + Mid$(temp$,EndPos+1)
'API method for any edit control
SendMessage(hEdit, %EM_GetSel, VarPTR(StartPos), VarPTR(EndPos)) 'start/stop of selection, caret position if no selection
If iStartPos& <> iStopPos& Then
SendMessage(hEdit, %EM_ReplaceSel, %True, StrPTR(ReplaceText$)) 'only replace if selection exists.
End If
'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_TabStop Or %WS_Border Or %ES_NoHideSel
Dialog New Pixels, 0, "Replace Selection",300,300,200,150, %WS_OverlappedWindow To hDlg
Control Add Button, hDlg, %IDC_ButtonDDT, "Replace Selection (DDT)",20,10,160,20
Control Add Button, hDlg, %IDC_ButtonAPI, "Replace Selection (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 ReplacementText$, temp$, StartPos, EndPos As DWord
Select Case Cb.Msg
Case %WM_Command
Select Case Cb.Ctl
Case %IDC_ButtonDDT
ReplacementText$ = "xxx"
'DDT method
Control Send hDlg, %IDC_TextBox, %EM_GetSel, VarPTR(StartPos), VarPTR(EndPos) 'start/stop of selection, caret position if no selection
Control Get Text hDlg, %IDC_TextBox To temp$
Control Set Text hDlg, %IDC_Textbox, Left$(temp$,StartPos) + ReplacementText$ + Mid$(temp$,EndPos+2)
Case %IDC_ButtonAPI
ReplacementText$ = "xxx"
'API method
SendMessage(hEdit, %EM_GetSel, VarPTR(StartPos), VarPTR(EndPos)) 'start/stop of selection, caret position if no selection
SendMessage(hEdit, %EM_ReplaceSel, %True, StrPTR(ReplacementText$)) 'only replace if selection exists.
End Select
End Select
End Function
'gbs_01082
'Date: 03-10-2012
http://www.garybeene.com/sw/gbsnippets.htm