求助在VB6+XP环境下,该如何使用API函数动态创建菜单?

具体涉及哪几个API函数?如何使用这些API函数?

BIG阳
浏览 274回答 1
1回答

慕婉清6462132

请右击测试效果动态创建菜单,鼠标右键弹出菜单,执行对应鼠标事件'窗体Option ExplicitPrivate Sub Form_Load()hMenu = CreateMenu()hmenupopup = CreatePopupMenu()result = AppendMenu(hmenupopup, MF_STRING, 300, "新建")result = AppendMenu(hmenupopup, MF_STRING, 301, "保存")result = AppendMenu(hmenupopup, MF_STRING, 302, "另存为")result = AppendMenu(hMenu, MF_POPUP, hmenupopup, "文件")oldwinproc = GetWindowLong(Me.hWnd, GWL_WNDPROC)SetWindowLong Me.hWnd, GWL_WNDPROC, AddressOf OnMenuEnd SubPrivate Sub Form_MouseDown(Button As Integer, Shift As Integer, x As Single, y As Single)Dim r As RECTDim p As POINTAPIIf Button = vbRightButton ThenGetCursorPos pTrackPopupMenu hmenupopup, 0, p.x, p.y, 0, Me.hWnd, rEnd IfEnd SubPrivate Sub Form_Unload(Cancel As Integer)SetWindowLong Me.hWnd, GWL_WNDPROC, oldwinprocEnd Sub'模块Option ExplicitPublic 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 "user32" (lpPoint As POINTAPI) As LongPublic Const MF_STRING = &H0&Public Const MF_POPUP = &H10&Public Const WM_USER = &H400Public Type RECTLeft As LongTop As LongRight As LongBottom As LongEnd TypePublic Type POINTAPIx As Longy As LongEnd TypePublic Const GWL_WNDPROC = (-4)Public hMenu As LongPublic hmenupopup As LongPublic result As LongPublic oldwinproc As LongPublic Const WM_COMMAND = &H111Public Function OnMenu(ByVal hWnd As Long, ByVal wMsg As Long, ByVal wParam As Long, ByVal lParam As Long) As LongSelect Case wMsgCase WM_COMMANDSelect Case wParamCase 300MsgBox "u select new", vbInformation, "hello, world!"Case 301MsgBox "u select save", vbInformation, "hello, world!"Case 302MsgBox "u select save as", vbInformation, "hello, world!"End SelectEnd SelectOnMenu = CallWindowProc(oldwinproc, hWnd, wMsg, wParam, lParam)End Function
打开App,查看更多内容
随时随地看视频慕课网APP