Tip:
1. The identifier of the menu item is stored in the lower two of the lparam parameter of the wm_menuselect message.
2. wm_menuselect also includes messages sent from the system menu.
3. Because the top-level menu (file edit help) generally has the same identifier (for example, 65535) in the resource file, the system regards their location (or order) as the identifier; therefore, when getmenustring is executed, use the mf_byposition parameter with a non-default value (not 0.
4. Because the system menu IDs are greater than $ f000, We need to customize the menu IDs to be smaller than this number.ProgramWe also use this principle to differentiate menu sources.
In this example:
Resource file (testres. Rc) used in this example ):
# Define idm_new 101 # define idm_open 102 # define idm_save 103 # define idm_exit 104 # define idm_cut 201 # define idm_copy 202 # define idm_paste 203 # define idm_about extends menuex extends popup "& file ", 65535 begin menuitem "& New", idm_new menuitem "& Open", idm_open menuitem "& Save", idm_save menuitem separator menuitem "E & xit", idm_exit end popup "& edit ", 65535 begin menuitem "Cu & T", idm_cut menuitem "& copy", idm_copy menuitem "& Paste", idm_paste end popup "& help", 65535 begin menuitem "& about ", idm_about endend
This exampleCodeFile:
Program project1; {$ R 'testres. res ''testres. RC '} uses Windows, messages; {work to be done when you receive the wm_menuselect message} procedure onmenuselect (H: hwnd; wparam: integer); var Buf: array [0 .. 255] of char; s: string; W: word; begin W: = loword (wparam); STR (W, S); Case W of 0 .. 99: getmenustring (getmenu (H), W, Buf, length (BUF), mf_byposition); 100 .. $ f000: getmenustring (getmenu (H), W, Buf, length (BUF), mf_bycommand); $ f001 .. $ FFFF: getmenustring (getsystemmenu (H, false), W, Buf, length (BUF), mf_bycommand); end; s: = S + '-' + Buf; setwindowtext (H, pchar (s); end; function wndproc (WND: hwnd; MSG: uint; wparam, lparam: integer): integer; stdcall; begin result: = 0; case MSG of Role: onmenuselect (WND, wparam); {call onmenuselect process after receiving the Response Message} wm_destroy: postquitmessage (0); else result: = defwindowproc (WND, MSG, wparam, lparam); end; function regmywndclass: Boolean; var CLS: twndclass; begin Cls. style: = cs_hredraw or cs_vredraw; Cls. lpfnwndproc: = @ wndproc; Cls. cbclsextra: = 0; Cls. cbwndextra: = 0; Cls. hinstance: = hinstance; Cls. hicon: = 0; Cls. hcursor: = loadcursor (0, idc_arrow); Cls. hbrbackground: = hbrush (color_window + 1); Cls. lpszmenuname: = 'mymenu1'; Cls. lpszclassname: = 'mywnd'; Result: = registerclass (CLS) 0; end; {program entry} const tit = 'new form'; Ws = ws_overlappedwindow; X = 100; y = 100; W = 300; H = 180; var hwnd: thandle; MSG: tmsg; begin regmywndclass; hwnd: = createwindow ('mywnd', tit, WS, X, y, W, H, 0, 0, hinstance, nil); showwindow (hwnd, sw_shownormal); updatewindow (hwnd); While (getmessage (MSG, 0, 0, 0 )) do begin translatemessage (MSG); dispatchmessage (MSG); end.