Date: 02-16-2022
Return to Index
created by gbSnippets
'Compilable Example: (Jose Includes)
'====================================================================
' Declares
'====================================================================
#Compiler PBWin 9, PBWin 10
#Compile EXE
'------------------------------------------------
%USEMACROS = 1
#Include "win32api.inc"
#Include "RICHEDIT.INC"
'------------------------------------------------
%ID_RICHEDIT = %WM_USER + 150
%IDM_UNDO = %WM_USER + 220
%IDM_CUT = %WM_USER + 222
%IDM_COPY = %WM_USER + 223
%IDM_PASTE = %WM_USER + 224
%IDM_DELETE = %WM_USER + 225
%IDM_SELALL = %WM_USER + 226
'====================================================================
' Program entrance
'====================================================================
Function PBMain () As Long
Local hDlg, hLib AS DWord
hLib = LoadLibrary("RICHED32.DLL") ' must load RichEdit dll
IF hLib = 0 THEN EXIT Function
'------------------------------------------------------------------
DIALOG NEW 0, "RichEdit example",,, 300, 242, _
%WS_BORDER OR %WS_SYSMENU, 0 TO hDlg
CONTROL ADD BUTTON, hDlg, %IDCANCEL, "&Quit", 242, 5, 50, 14
'------------------------------------------------------------------
CONTROL ADD "RichEdit", hDlg, %ID_RICHEDIT, "Right-click for popup menu", _
4, 24, 289, 200, %WS_CHILD OR %WS_VISIBLE OR %WS_TABSTOP OR _
%WS_VSCROLL OR %ES_AUTOVSCROLL OR %ES_DISABLENOSCROLL OR _
%ES_MULTILINE OR %ES_NOHIDESEL OR %ES_SAVESEL, %WS_EX_CLIENTEDGE
' following gives small selection area on left side
CONTROL SEND hDlg, %ID_RICHEDIT, %EM_SETOPTIONS, %ECOOP_OR, %ECO_SELECTIONBAR
'------------------------------------------------------------------
DIALOG SHOW MODAL hDlg CALL DlgProc
IF hLib THEN FreeLibrary hLib
End Function
'====================================================================
' Main Dialog procedure
'====================================================================
CallBack Function DlgProc() As Long
Local hMenuEdit As Long, pt AS POINTAPI, cr AS CHARRANGE
SELECT CASE CB.MSG
CASE %WM_INITDIALOG 'first message - initiate
STATIC hEdit AS DWord
CONTROL HANDLE CB.HNDL, %ID_RICHEDIT TO hEdit
CASE %WM_COMMAND
SELECT CASE CB.CTL
CASE %IDCANCEL 'EXIT (also via Escape key)
IF CB.CTLMSG = %BN_CLICKED OR CB.CTLMSG = 1 THEN
DIALOG END CB.HNDL
END IF
'EDIT POPUP MENU COMMANDS
CASE %IDM_UNDO
IF CB.CTLMSG = %BN_CLICKED OR CB.CTLMSG = 1 THEN
SendMessage hEdit, %EM_UNDO, 0, 0
END IF
CASE %IDM_CUT
IF CB.CTLMSG = %BN_CLICKED OR CB.CTLMSG = 1 THEN
SendMessage hEdit, %WM_CUT, 0, 0
END IF
CASE %IDM_COPY
IF CB.CTLMSG = %BN_CLICKED OR CB.CTLMSG = 1 THEN
SendMessage hEdit, %WM_COPY, 0, 0
END IF
CASE %IDM_PASTE
IF CB.CTLMSG = %BN_CLICKED OR CB.CTLMSG = 1 THEN
SendMessage hEdit, %WM_PASTE, 0, 0
END IF
CASE %IDM_DELETE
IF CB.CTLMSG = %BN_CLICKED OR CB.CTLMSG = 1 THEN
SendMessage hEdit, %WM_CLEAR, 0, 0
END IF
CASE %IDM_SELALL
IF CB.CTLMSG = %BN_CLICKED OR CB.CTLMSG = 1 THEN
cr.cpMin = 0 : cr.cpMax = -1 'Select all
SendMessage hEdit, %EM_EXSETSEL, 0, VARPTR(cr)
END IF
END SELECT
CASE %WM_CONTEXTMENU ' if right-click
IF CB.WPARAM = hEdit THEN ' inside RichEdit
pt.x = LO(INTEGER, CB.LPARAM) ' mouse x, y coordinates
pt.y = HI(INTEGER, CB.LPARAM)
IF pt.x > -1 AND pt.y > -1 THEN
MENU NEW POPUP TO hMenuEdit
MENU ADD STRING, hMenuEdit, "&Undo" & $TAB & "Ctrl + Z", %IDM_UNDO, _
IIF&(SendMessage(hEdit, %EM_CANUNDO, 0, 0), %MF_ENABLED, %MF_GRAYED)
MENU ADD STRING, hMenuEdit, "-", 0, 0
MENU ADD STRING, hMenuEdit, "Cu&t" & $TAB & "Ctrl + X", %IDM_CUT, _
IIF&(cr.cpMin <> cr.cpMax, %MF_ENABLED, %MF_GRAYED)
MENU ADD STRING, hMenuEdit, "&Copy" & $TAB & "Ctrl + C", %IDM_COPY, _
IIF&(cr.cpMin <> cr.cpMax, %MF_ENABLED, %MF_GRAYED)
MENU ADD STRING, hMenuEdit, "&Paste" & $TAB & "Ctrl + V", %IDM_PASTE, _
IIF&(SendMessage(hEdit, %EM_CANPASTE, 0, 0), %MF_ENABLED, %MF_GRAYED)
MENU ADD STRING, hMenuEdit, "&Delete" & $TAB & "Del", %IDM_DELETE, _
IIF&(cr.cpMin <> cr.cpMax, %MF_ENABLED, %MF_GRAYED)
MENU ADD STRING, hMenuEdit, "-", 0, 0
MENU ADD STRING, hMenuEdit, "&Select all" & $TAB & "Ctrl + A", %IDM_SELALL, _
IIF&(SendMessage(hEdit, %WM_GETTEXTLENGTH, 0, 0), %MF_ENABLED, %MF_GRAYED)
TrackPopupMenu hMenuEdit, 0, pt.x, pt.y, 0, CB.HNDL, ByVal %NULL
END IF
END IF
END SELECT
End Function
'gbs_01074
'Date: 03-10-2012
http://www.garybeene.com/sw/gbsnippets.htm