Hi, This example demonstrates how to replace the default context menu in an edit control with your own Uses _WinAPI_CallWindowProc, a good function that requires more examples #include <GUIConstantsEx.au3>
#include <WindowsConstants.au3>
#include <Constants.au3>
#include <GuiMenu.au3>
#include <WinAPI.au3>
#include <GUIEdit.au3>
; modified from help example: _WinAPI_CallWindowProc
$hGui = GUICreate("Custom context menu for Edit Control example", 489, 354, -1, -1)
$editBox = GUICtrlCreateEdit("", 8, 40, 473, 305)
GUICtrlSetData(-1, "New text")
$checkBox = GUICtrlCreateCheckbox("Use our own context menu", 8, 8, 241, 17)
GUICtrlSetState($checkBox, $GUI_CHECKED)
;!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
;!! subclass for $editBox
$w_editBoxProcNew = DllCallbackRegister("_MyWindowProc", "ptr", "hwnd;uint;long;ptr")
$w_editBoxProcOld = _WinAPI_SetWindowLong(GUICtrlGetHandle($editBox), $GWL_WNDPROC, DllCallbackGetPtr($w_editBoxProcNew))
_WinAPI_SetWindowLong(GUICtrlGetHandle($editBox), $GWL_WNDPROC, DllCallbackGetPtr($w_editBoxProcNew))
;!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
;!! our context menu for $editBox
$DummyMenu = GUICtrlCreateDummy()
$ContextMenu = GUICtrlCreateContextMenu($DummyMenu)
$TestMenuItem = GUICtrlCreateMenuItem("Our Custom Context Menu", $ContextMenu)
GUICtrlSetState($TestMenuItem, $GUI_DEFBUTTON)
GUICtrlCreateMenuItem("", $ContextMenu)
$UndoMenuItem = GUICtrlCreateMenuItem("&Undo", $ContextMenu)
GUICtrlCreateMenuItem("", $ContextMenu)
$CutMenuItem = GUICtrlCreateMenuItem("&Cut", $ContextMenu)
$CopyMenuItem = GUICtrlCreateMenuItem("&Copy", $ContextMenu)
$PasteMenuItem = GUICtrlCreateMenuItem("&Paste", $ContextMenu)
$DeleteMenuItem = GUICtrlCreateMenuItem("&Delete", $ContextMenu)
GUICtrlCreateMenuItem("", $ContextMenu)
$SelectAllMenuItem = GUICtrlCreateMenuItem("&Select all", $ContextMenu)
GUICtrlCreateMenuItem("", $ContextMenu)
$InsertDateMenuItem = GUICtrlCreateMenuItem("&Insert Date and Time", $ContextMenu)
GUISetState(@SW_SHOW)
While 1
$nMsg = GUIGetMsg()
Switch $nMsg
; toggle our context menu on / off
Case $checkBox
If BitAND(GUICtrlRead($checkBox), $GUI_CHECKED) = $GUI_CHECKED Then
_WinAPI_SetWindowLong(GUICtrlGetHandle($editBox), $GWL_WNDPROC, DllCallbackGetPtr($w_editBoxProcNew))
Else
_WinAPI_SetWindowLong(GUICtrlGetHandle($editBox), $GWL_WNDPROC, $w_editBoxProcOld)
EndIf
Case $GUI_EVENT_CLOSE
;!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
;!! we must free the callback before exit !!
_WinAPI_SetWindowLong(GUICtrlGetHandle($editBox), $GWL_WNDPROC, $w_editBoxProcOld)
DllCallbackFree($w_editBoxProcNew)
Exit
EndSwitch
WEnd
;!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
;!! process our context menu messages
Func MenuItem($iSelected)
Switch $iSelected
Case $TestMenuItem
_GUICtrlEdit_AppendText($editBox, @CRLF & "Our Custom Context Menu")
Case $UndoMenuItem
_GUICtrlEdit_Undo($editBox)
Case $CutMenuItem
MenuItem($CopyMenuItem)
MenuItem($DeleteMenuItem)
Case $CopyMenuItem
Local $aSel, $sText
$aSel = _GUICtrlEdit_GetSel($editBox)
If ($aSel[0] <> $aSel[1]) Then
$sText = StringMid(_GUICtrlEdit_GetText($editBox), $aSel[0] + 1, ($aSel[1] - $aSel[0]) + 1)
ClipPut($sText)
EndIf
Case $PasteMenuItem
Local $sText = ClipGet()
If (Not @error) Then _GUICtrlEdit_ReplaceSel($editBox, $sText, True)
Case $DeleteMenuItem
_GUICtrlEdit_ReplaceSel($editBox, "", True)
Case $SelectAllMenuItem
_GUICtrlEdit_SetSel($editBox, 0, -1)
Case $InsertDateMenuItem
_GUICtrlEdit_ReplaceSel($editBox, @CRLF & @YEAR &"/"& @MON &"/"& @MDAY &" "& @HOUR &":" & @MIN &":" & @SEC)
EndSwitch
EndFunc ;==>ShowMenu
; auxilary function to update menu item states, not required
Func UpdateMenuStates()
Local $aSel = _GUICtrlEdit_GetSel($editBox)
Local $iLen = _GUICtrlEdit_GetTextLen($editBox)
_GUICtrlEnabled($UndoMenuItem, _GUICtrlEdit_CanUndo($editBox))
_GUICtrlEnabled($CutMenuItem, ($aSel[0]<>$aSel[1]))
_GUICtrlEnabled($CopyMenuItem, ($aSel[0]<>$aSel[1]))
_GUICtrlEnabled($PasteMenuItem, (StringLen(ClipGet())>0))
_GUICtrlEnabled($DeleteMenuItem, ($aSel[0]<>$aSel[1]))
_GUICtrlEnabled($SelectAllMenuItem, ($iLen>0) And ($iLen<>$aSel[0]+$aSel[1]))
EndFunc ;==>SetMenuTexts
; auxilary function to enable / disable controls, not required
Func _GUICtrlEnabled($CtrlID, $fTrue = True)
If $fTrue Then
GUICtrlSetState($CtrlID, $GUI_ENABLE)
Else
GUICtrlSetState($CtrlID, $GUI_DISABLE)
EndIf
EndFunc
;!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
;!! our window subclass for $editBox
Func _MyWindowProc($hWnd, $uiMsg, $wParam, $lParam)
Switch $uiMsg
Case $WM_CONTEXTMENU
If $hWnd = GUICtrlGetHandle($editBox) Then
; show our context menu
UpdateMenuStates()
Local $iSelected = _GUICtrlMenu_TrackPopupMenu(GUICtrlGetHandle($ContextMenu), $hWnd, -1, -1, 1, 1, 2)
MenuItem($iSelected)
Return 0
EndIf
EndSwitch
;pass the unhandled messages to default WindowProc
Return _WinAPI_CallWindowProc($w_editBoxProcOld, $hWnd, $uiMsg, $wParam, $lParam)
EndFunc ;==>_MyWindowProc Have a good day