Current location - Recipe Complete Network - Complete cookbook - In VB6+XP environment, how to use API functions to dynamically create menus?
In VB6+XP environment, how to use API functions to dynamically create menus?
Please right-click to test the effect.

Create a menu dynamically, right-click the mouse to pop up the menu, and execute the corresponding mouse event.

form

Option explicit

Private Sub-Form _Load ()

hMenu = CreateMenu()

hmenupopup = CreatePopupMenu()

Result = Appendmenu (h menuppopup,MF _ string,300," New ")。

result = Appendmenu(hmenupup,MF _ string,30 1," Save ")。

Result = Appendmenu(hmenupup, MF _ string, 302, "Save as").

Result = Appendmenu (hmenu, MF _ popup, hmenupopup, "file")

oldwinproc = GetWindowLong(me . hwnd,GWL_WNDPROC)

SetWindowLong Me.hWnd, GWL_WNDPROC, address OnMenu

End joint

Private sub-form _MouseDown (button is integer, Shift is integer, x is single value, y is single value)

As dim as RECT

Dim p As POINTAPI

If Button = vbRightButton, then

GetCursorPos p

TrackPopupMenu hmenupopup,0,p.x,p.y,0,Me.hWnd,r

If ... it will be over.

End joint

Private Sub-Form _Unload (Cancel to Integer)

SetWindowLong Me.hWnd,GWL_WNDPROC,oldwinproc

End joint

package

Option explicit

The public declaration function createmenulib "user 32" () is Long.

The public declaration function appendmenulib "user32" has the alias "AppendMenuA”(ByVal hMenu is Long, ByVal wFlags is Long, ByVal wIDNewItem is Long, ByVal lpNewItem is Any) as Long.

The public declaration function trackupmenu lib "user 32" (byval hmenu is Long, ByVal wFlags is Long, ByVal x is Long, ByVal y is Long, ByVal nReserved is Long, ByVal hWnd is Long, lprc is RECT) is Long.

The public declaration function CreatePopupMenu Lib "user32 "() is Long.

The public declaration function setwindowLonglib "user 32" is alias "setwindowLong a" (byval hwnd is Long, ByVal nIndex is Long, ByVal dwNewLong is long) is long.

The public declaration function callwindowproclib "user32" has a long alias "callwindowproca" (byval lpprevwndfunc as long, ByVal hWnd As Long, ByVal Msg As Long, ByVal wParam As Long, ByVal lParam As Long).

The public declaration function getwindowLonglib "user 32" is alias "getwindowLong a" (byvalhwnd is long, ByVal nIndex is long).

The public declaration function getcursorposlib "user 32" (lppoint as point API) is Long.

Common constant MF _ STRING = & ampH0 & amp;

Common constant MF _ popup = & amph10&;

Public Const WM _ USER = & ampH400

Public type RECT

Left isometric

The top is the same length

be of the same length

The bottom is the same length

End type

Common type POINTAPI

X is the same length

as long as

End type

Common constant GWL_WNDPROC = (-4)

Public hMenu as long

As long as the public hmenupopup.

The public result is long

Public oldwinproc As Long

Public constwm _ command =&111

The public function OnMenu(ByVal hWnd is Long, ByVal wMsg is Long, ByVal wParam is Long, ByVal lParam is Long) is Long.

Select case wMsg

Case WM_COMMAND

Select case wParam

Case 300

MsgBox“u select new”,vbInformation,“hello,world!”

Case 30 1

MsgBox“u select save”,vbInformation,“hello,world!”

Case 302

MsgBox“u select save as ",vbInformation," hello, world! "

End selection

End selection

on menu = CallWindowProc(oldwinproc,hWnd,wMsg,wParam,lParam)

End function