Dynamically create a menu, add a menu, perform your own menu event

zhaozj2021-02-16  47

(for reference only)

Dynamically create a menu, right mouse button pop-up menu, perform the corresponding mouse event 'Form Option ExplicitPrivate Sub Form_Load () hMenu = CreateMenu () hmenupopup = CreatePopupMenu () result = AppendMenu (hmenupopup, MF_STRING, 300, "& New") result = AppendMenu ( hmenupopup, MF_STRING, 301, "& Save") result = AppendMenu (hmenupopup, MF_STRING, 302, "Save & As ...") result = AppendMenu (hMenu, MF_POPUP, hmenupopup, "& File") oldwinproc = GetWindowLong (Me.hWnd, GWL_WNDPROC) SETWINDOWLONG ME.HWND, GWL_WNDPROC, Addressof Onmenuend Sub

Private Sub Form_MouseDown (Button As Integer, Shift As Integer, x As Single, y As Single) Dim r As RECT Dim p As POINTAPI If Button = vbRightButton Then GetCursorPos p TrackPopupMenu hmenupopup, 0, px, py, 0, Me.hWnd, R end ifend sub

Private Sub Form_unload (Cancel As Integer) Setwindowlong Me.hwnd, GWL_WndProc, OldwinProcend Sub 'Module Option Explicit

Public Declare Function CreateMenu Lib "user32" () As LongPublic Declare Function AppendMenu Lib "user32" Alias ​​"AppendMenuA" (ByVal hMenu As Long, ByVal wFlags As Long, ByVal wIDNewItem As Long, ByVal lpNewItem As Any) As LongPublic Declare Function TrackPopupMenu Lib "user32" (ByVal hMenu As Long, ByVal wFlags As Long, ByVal x As Long, ByVal y As Long, ByVal nReserved As Long, ByVal hWnd As Long, lprc As RECT) As LongPublic Declare Function CreatePopupMenu Lib "user32" () As LongPublic Declare Function SetWindowLong Lib "user32" Alias ​​"SetWindowLongA" (ByVal hWnd As Long, ByVal nIndex As Long, ByVal dwNewLong As Long) As LongPublic Declare Function CallWindowProc Lib "user32" Alias ​​"CallWindowProcA" (ByVal lpPrevWndFunc As Long, ByVal hWnd As Long, ByVal Msg As Long, ByVal wParam As Long, ByVal lParam As Long) As LongPublic Declare Function GetWindowLong Lib "user32" Alias ​​"GetWindowLongA" (ByVal hWnd As Long, ByVal nIndex As Long) As LongPublic Declare Function GetCursorPos Lib "use r32 "(lpPoint As POINTAPI) As LongPublic Const MF_STRING = & H0 & Public Const MF_POPUP = & H10 & Public Const WM_USER = & H400Public Type RECT Left As Long Top As Long Right As Long Bottom As LongEnd TypePublic Type POINTAPI x As Long y As LongEnd TypePublic Const GWL_WNDPROC = (- 4) PUBLIC HMENU As Longpublic HMenupopup As Longpublic RESULT AS Longpublic OldwinProc As longpublic const wm_command = & h111

转载请注明原文地址:https://www.9cbs.com/read-24801.html

New Post(0)