Date: 02-16-2022
Return to Index
created by gbSnippets
'It's sometimes useful to have a RichEdit highlight the text when the control
'receives focus. The default action of a textbox is to highlight all text if it
'gets focus after TABing, but not if a control gains focus by being clicked on
'by the mouse.
'Note: In the code that follows, EM_EXSETSEL is used over EM_SETSEL.
' EM_SetSel supports both edit and richedit controls, but has a 64K
' text limit. For larger text content in richedit controls, use EM_EXSETSEL
'Compiler Comments:
'This code is written to compile with PBWin10. To compile with PBWin9,
'add this line:
#Include "CommCtrl.inc"
'Primary Code:
'Both methods (Send or Post) will work.
Local P as CharRange
P.cpmin = 0 : P.cpmax = -1 'highlight all
Control Send hDlg, %ID_RichEdit, %EM_EXSETSEL, 0, VarPTR(P)
Control Post CB.Hndl, CB.Ctl, %EM_EXSETSEL, 0, VarPTR(P)
'Compilable Example: (Jose Includes)
'Click on either of the RichEdit controls to select all of the text. Because
'the style& used for each include ES_NoHideSel, the EM_SetSel is used
'to cancel the selection in the RichEdit control that is not selected.
#Compiler PBWin 10
#Compile EXE
#Dim All
%Unicode=1
#Include "win32api.inc"
#Include "RichEdit.inc"
Global hDlg as Dword, hEdit1 as Dword, hEdit2 as Dword
Function PBMain() As Long
Dialog New Pixels, 0, "Test Code",300,300,200,200, %WS_OverlappedWindow To hDlg
LoadLibrary("riched32.dll") : InitCommonControls
Control Add "RichEdit", hDlg, 500, "Select me!",20,40,160,50, 1345393092 'style& is the integer value of common styles
Control Add "RichEdit", hDlg, 600, "No, select me!",20,110,160,50, 1345393092 'style& is the integer value of common styles
Control Handle hDlg, 500 TO hEdit1
Control Handle hDlg, 600 TO hEdit2
Dialog Show Modal hDlg Call DlgProc
End Function
CallBack Function DlgProc() As Long
Local P as CharRange
Select Case CB.Msg
Case %WM_Command
Select Case CB.Ctlmsg
Case %EN_SetFocus
Select Case CB.Ctl
Case 500
P.cpmin = 0 : P.cpmax = -1 'highlight all
SendMessage hEdit1, %EM_EXSETSEL, 0, VarPTR(P)
'Control Post CB.Hndl, 500, %EM_EXSETSEL, 0, VarPTR(P)
P.cpmin = 1 : P.cpmax = 1 'highlight none
SendMessage hEdit2, %EM_EXSETSEL, 0, VarPTR(P)
'Control Post CB.Hndl, 600, %EM_EXSETSEL, 0, VarPTR(P)
Case 600
P.cpmin = 0 : P.cpmax = -1 'highlight all
SendMessage hEdit2, %EM_EXSETSEL, 0, VarPTR(P)
'Control Post CB.Hndl, 600, %EM_EXSETSEL, 0, VarPTR(P)
P.cpmin = 1 : P.cpmax = 1 'highlight none
SendMessage hEdit1, %EM_EXSETSEL, 0, VarPTR(P)
'Control Post CB.Hndl, 500, %EM_EXSETSEL, 0, VarPTR(P)
End Select
End Select
End Select
End Function
'gbs_00220
'Date: 03-10-2012
http://www.garybeene.com/sw/gbsnippets.htm