Many articles on the Internet have mentioned how to add background images to the menu. However, there is no complete answer or complete example. Of course, a senior person in csdn once said this problem and added it to the favorites in the programmer Base Camp version 2000. I have referenced some methods and techniques, including those outside China. I wrote the following code to meet your requirements, but it is not perfect. If any of you have modified the code, you may wish to share the modified Code! In order to add background images to the menu control, there is no direct method. You can draw them by yourself. Because the menu control does not have the canvas attribute, you can only do it yourself! This thing I am also a rookie, can not say anything advanced, if you have any questions can send to me, we discuss together: cqwty@sina.com, source code is as follows:
Unit fmain;
Interface
Uses
Windows, graphics, forms, menus, classes;
Type
Tfrmmain = Class (tform)
Mnupopup: tpopupmenu;
Mainmenu1: tmainmenu;
Sdfsdf1: tmenuitem;
Sdfsdf2: tmenuitem;
Dfgdfg1: tmenuitem;
Dfgdfg2: tmenuitem;
N1: tmenuitem;
Werwer1: tmenuitem;
Procedure drawmenu (Sender: tobject; acanvas: TCanvas; arect: trect; State: townerdrawstate );
Procedure measuremenu (Sender: tobject; acanvas: TCanvas; var width, height: integer );
Procedure formcreate (Sender: tobject );
Procedure formclose (Sender: tobject; var action: tcloseaction );
End;
VaR
Frmmain: tfrmmain;
BMP 1: tbitmap;
Implementation
{$ R *. DFM}
Procedure tfrmmain. drawmenu (Sender: tobject; acanvas: TCanvas; arect: trect; State: townerdrawstate );
VaR
Ctemp: TCanvas;
Stext: string;
Mwnd: hwnd;
Rmenu: trect;
Begin
Acanvas. brushcopy (arect, BMP 1, arect, clblack );
Acanvas. Brush. Style: = bsclear;
Stext: = tmenuitem (sender). Caption;
Acanvas. Font. Color: = clred;
With acanvas do begin
If odselected in State then begin
Pen. Style: = psinsideframe;
Brush. Color: = RGB (110,131,184 );
Pen. Color: = RGB (47, 60, 93 );
Rectangle (arect );
End;
If stext = '-' Then begin
// Draw line
Acanvas. Pen. Color: = RGB (0, 0, 0 );
MoveTo (arect. Left, arect. Top + (arect. Bottom-arect. Top) Div 2 ));
Lineto (arect. Right, arect. Top + (arect. Bottom-arect. Top) Div 2 ));
End else begin
// Draw text
INC (arect. Left, 12 );
Drawtext (handle, pchar (stext), length (stext), arect, dt_left or dt_vcenter or dt_singleline );
End;
End;
// Draw the border, and the effect is flat
Mwnd: = windowfromdc (acanvas. Handle );
If mwnd <> self. Handle then begin
Ctemp: = TCanvas. Create ();
Ctemp. Handle: = getdc (0 );
Windows. getwindowrect (mwnd, rmenu );
Ctemp. Brush. Color: = RGB (120,120,120 );
Ctemp. framerect (rmenu );
Inflaterect (rmenu,-1,-1 );
Ctemp. Brush. Color: = RGB (240,240,240 );
Ctemp. framerect (rmenu );
Inflaterect (rmenu,-1,-1 );
Ctemp. framerect (rmenu );
Releasedc (0, ctemp. Handle );
Ctemp. Free ();
End;
End;
Procedure tfrmmain. measuremenu (Sender: tobject; acanvas: TCanvas; var width, height: integer );
Begin
INC (width, 50); // adjust the menu width
INC (height, 15); // adjust the height of each item. This sentence is optional and the default value is used.
End;
Procedure tfrmmain. formcreate (Sender: tobject );
Begin
BMP 1: = tbitmap. Create;
BMP 1.loadfromfile ('e:/aaa.bmp ');
End;
Procedure tfrmmain. formclose (Sender: tobject; var action: tcloseaction );
Begin
BMP 1.free;
End;
End.