In this example:
In this example, three menus are dynamically added when the form is created, and the functions of Self-deletion are granted to them.
Resource file (testres. Rc) used in this example ):
Mymenu1 menuexbegin popup "& file" begin menuitem "E & xit", 101 endend
This exampleCodeFile:
Program project1; {$ R 'testres. res ''testres. RC '} uses Windows, messages; const idm_1 = 1; {define the constants of the three menu items} idm_2 = 2; idm_3 = 3; {Add three menu items when receiving the wm_create message} procedure oncreate (H: hwnd); var HM: hmenu; begin HM: = getmenu (h ); {get window main menu handle} appendmenu (HM, mft_string, idm_1, 'menu & 1'); {Add menu item} appendmenu (HM, mft_string, idm_2, 'menu & 2'); appendmenu (HM, mft_string, idm_3, 'menu & 3'); end; {work required to receive the wm_command message} procedure oncommand (h: hwnd; wparam: integer); var W: word; begin W: = loword (wparam); Case W of 101: destroywindow (h); {close the window; 101 is the identifier specified for the exit menu in the resource file} idm_1, idm_2, idm_3: begin {when you click the three newly created menus ...} deletemenu (getmenu (H), W, 0); {Delete this menu} drawmenubar (h); {redraw menu} end; function wndproc (WND: hwnd; MSG: uint; wparam: integer; lparam: integer): integer; stdcall; begin result: = 0; Case MSG of wm_create: oncreate (WND ); {oncreate process called after wm_create message received} wm_command: oncommand (WND, wparam); {oncommand process called after wm_command message received} 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.