GetMenu_() returns number of StringGadget?
Actually MSDN says that for child windows the return value is undefined. Child windows cannot host a menu. But this is a context menu, something different. You can make your own, but if you prefer to modify the system context menu for a control, I'll show you a trick. The code works in two steps: One, a message hook catches the menu being created and records the hwnd for its window. But at this point it's too early in the process to retrieve a hMenu. So then Two, a message containing this hwnd is sent asynchronously (PostMessage not SendMessage) to the winproc of the main window. By the time this message is received and processed, a hMenu is available for retrieval via MN_GETHMENU. From here Bob's your uncle and you can use RemoveMenu, ModifyMenu, SetMenuItemInfo, etc. Basically any command that requires a hMenu.
Code: Select all
; Yet another useless program from netmaestro
#WM_HOLYCONTEXTMENUBATMAN = #WM_APP + 1
Procedure HookProc(nCode, wParam, lParam)
If (nCode = #HC_ACTION)
*cwps.CWPSTRUCT = lParam
Select *cwps\message
Case #WM_CREATE
szClass.s=Space(128)
GetClassName_(*cwps\hwnd, @szClass, 127)
If szClass="#32768"
PostMessage_(WindowID(0), #WM_HOLYCONTEXTMENUBATMAN, *cwps\hwnd, 0) ; Must institute a delay before executing MN_GETMENU
EndIf
EndSelect
EndIf
ProcedureReturn CallNextHookEx_(0, nCode, wParam, lParam)
EndProcedure
Procedure WinProc(hwnd, msg, wparam, lparam)
Static g_hHook
Select msg
Case #WM_PARENTNOTIFY
If wparam & $FFFF = #WM_RBUTTONDOWN ; Detect right-click on child windows
With wp.POINT
\x = lparam & $FFFF
\y = lparam >> 16
EndWith
MapWindowPoints_( WindowID(0), #HWND_DESKTOP, @wp, 1)
If WindowFromPoint_(PeekQ(@wp)) = GadgetID(0) ; Modify menu for only selected controls
g_hHook = SetWindowsHookEx_(#WH_CALLWNDPROC, @HookProc(), #Null, GetCurrentThreadId_())
EndIf
EndIf
ProcedureReturn 0
Case #WM_HOLYCONTEXTMENUBATMAN
hwnd = wparam
hmenu = SendMessage_(hwnd, #MN_GETHMENU,0,0) ; Get the hMenu and modify it
RemoveMenu_(hmenu, 6, #MF_BYPOSITION) ; Remove menubar
RemoveMenu_(hmenu, 5, #MF_BYPOSITION) ; Remove 'Delete'
RemoveMenu_(hmenu, 4, #MF_BYPOSITION) ; Remove 'Paste'
RemoveMenu_(hmenu, 3, #MF_BYPOSITION) ; Remove 'Copy'
RemoveMenu_(hmenu, 2, #MF_BYPOSITION) ; Remove 'Cut'
UnhookWindowsHookEx_(g_hHook) ; So as not to affect submenus
EndSelect
ProcedureReturn #PB_ProcessPureBasicEvents
EndProcedure
OpenWindow(0,0,0,320,240,"", #PB_Window_SystemMenu|#PB_Window_ScreenCentered)
SetWindowCallback(@WinProc())
StringGadget(0,20,40,200,20,"stuff")
Repeat
EventID = WaitWindowEvent()
Until EventID=#PB_Event_CloseWindow