There are many articles on the Internet how to add a background image in the menu, but there is no complete answer, nor a relatively complete example. Of course, 9CBS has said this issue, and there is also a collection in the programmer's capital 2000. I will refer to some methods and skills, of course, including foreign, write the following code, I hope to meet everyone's request, but not very perfect, if any heroes have been modified, I will also modify the code post Come out, everyone share! In order to add a background image in the MENU control, there is no direct method, you can do your hand, because the Menu control doesn't have a Canvas property, so you can only do it yourself! I am also a rookie. I can't say anything about. If there is any problem, I can send it to me, we discuss: cqwwty@sina.com, the 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: Tclosection;
Var frmmain: tfrmmain; bmp1: tbitmap; importation
{$ R * .dfm}
Procedure tfrmmain.drawmenu (sender: Tcanvas; allvas: tcanvas; limited; var ctemp: tcanvas; stext: string; mWnd: hwnd; rmenu: TRECT;
Begin
ACanvas.BrushCopy (ARect, bmp1, 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; Brush.Color: = RGB (110, 131, 184); Pen.Color: = RGB (47, 60, 93); Rectangle (all);
If Stext = '-' Then Begin // Draw line acanvas.pen.color: = RGB (0, 0, 0); MoveTo (all, allt, all (all (all.bottom - all.top) DIV 2 )); Lineto (all (all (all (all (all "); Else Begin // Draw Text Inc (all, pchar (handle, pchar); Stext), length (stext), all, dt_singline); end; end; //, the effect is the MWND: = WindowFromDC (Acanvas.Handle);
IF mWnd <> self.handle kiln 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);
Inflatrect (RMENU, -1, -1); CTEMP.FrameRect (RMenu);
ReleaseDC (0, ctemp.handle); ctyp.free ();
END;
END;
Procedure tfrmmain.measureMenu (Sender: Tobject; Acanvas: Tcanvas; Var Width, Height: Integer; Begin Inc (Width, 50); // Adjust the width of the menu
INC (Height, 15); // Adjusts the height of each Item, this sentence can not, use the default value END;
Procedure tfrmmain.formcreate (sender: TOBJECT); begin bmp1: = tbitmap.create; bmp1.loadfromfile ('E: /AAA.BMP'); END;
Procedure tfrmmain.formclose (sender: Tobject; var Action: tclosection); beginbmp1.free; end;
End.