If we use the tmainmenu control to design the following menu:
This menu is stored in the form source file as follows:
Object mainmenu1: tmainmenuleft = 160top = 104 object file1: tmenuitem caption = '& file' object new1: tmenuitem caption =' & new' end object open1: tmenuitem caption = '& open' end object save1: tmenuitem caption =' & save' end object saveas1: tmenuitem caption = 'Save & as 'end object N1: tmenuitem caption = '-'end object exit1: tmenuitem caption = 'e & xit' endendobject edit1: tmenuitem caption =' & edit' object cut1: tmenuitem caption = 'cu & T' end object copy1: tmenuitem caption = '& copy' end object paste1: tmenuitem caption =' & Paste 'endendobject help1: tmenuitem caption = '& help' object about1: tmenuitem caption =' & about' endend
In VC, the menu is designed to be saved in the resource source file (*. in RC), No matter what temporary storage method is used by VC or Delphi, it will eventually be compiled into the same resource file (*. res ).
If you want to use menus more flexibly in Delphi, you cannot avoid using resource files.
I used to have a simple access to menu resources when I was learning resource files (refer to the previous content). However, this time I am going to start from the perspective of windows, involving more content!
The following is the resource file corresponding to the preceding example.
Procedure:
1. Create a console project and save it! Then create a resource file;
2. Create a resource file: file-> New-> other...-> text, and write the following resource fileCode, And save it as *. RC in the same folder as the project. Here I use: testres. RC;
3. Import the source file from the Project (Project Manager window, right-click the project name-add );
In this case, the project code will automatically add one line: {$ R 'testres. res '}
4. Note: After a Resource Name is modified in the project, the project code changes accordingly.Source codeIt must be saved before compilation is effective!
5. In addition, the writing of menu resources is complex and powerful.
Mymenu1 menubegin popup "& file" begin menuitem "& New" menuitem "& Open" menuitem "& Save" menuitem separator menuitem "E & xit" End popup "& edit" begin menuitem" cu & T "menuitem" & copy "menuitem" & Paste "End popup" & Help "begin menuitem" & about "endend
Call code:
And the originalProgramOnly two rows are different:
1. {$ R 'testres. res' 'testres. RC '} --- this is the resource file to be inserted.
2. Cls. lpszmenuname: = 'mymenu1'; --- this is the specified menu
Program project1; {$ R 'testres. res ''testres. RC '} uses Windows, messages, sysutils; function wndproc (WND: hwnd; MSG: uint; wparam: integer; lparam: integer): integer; stdcall; begin result: = 0; case MSG of 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'; {specify the menu resource here, And mymenu1 is named when editing the resource file} 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.
: