这里有一篇文章!
上面图片是这些代码对照图片。
在Delphi中做这种菜单关键就在于怎么画分隔符,因为分隔符在属性面板我们是输入“-”表示的,但在delphi中它却不是按普通字符处理的,打开库源代码可以看到,它是将“-”转化为系统中真正的分隔符,它的类型(MenuItemInfo)是MFT_SEPARATOR而一般的字符串的类型是MFT_STRING的,所以我们在重画的时候就要注意,否则会出现1的那种情况,因为分隔符不要用一般的重画过程,如果这样处理它会割断图片,如果我们按字符串形式(和其他菜单项一样看待)呢?那么它会画成图2的样子,怎么画成图3的样子呢?我们这里用个小的技巧,不要系统处理,我们来自己画它!
下面是全部代码:(可能由于这里的断行问题,你要仔细看哟)
只是在我认为重点的部分加了部分注释!
unit Myapp;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, ExtCtrls, Menus, StdActns, ExtActns, ActnList, StdCtrls;
type
TForm1 = class(TForm)
MainMenu1: TMainMenu;
ActionList1: TActionList;
FileOpen1: TFileOpen;
FileSaveAs1: TFileSaveAs;
FileRun1: TFileRun;
FileExit1: TFileExit;
file1: TMenuItem;
Open1: TMenuItem;
Run1: TMenuItem;
SaveAs1: TMenuItem;
Exit1: TMenuItem;
Image1: TImage;
N1: TMenuItem;
Image2: TImage;
procedure Open1DrawItem(Sender: TObject; ACanvas: TCanvas;
ARect: TRect; Selected: Boolean);
procedure Run1DrawItem(Sender: TObject; ACanvas: TCanvas; ARect: TRect;
Selected: Boolean);
procedure SaveAs1DrawItem(Sender: TObject; ACanvas: TCanvas;
ARect: TRect; Selected: Boolean);
procedure Exit1DrawItem(Sender: TObject; ACanvas: TCanvas;
ARect: TRect; Selected: Boolean);
procedure Open1MeasureItem(Sender: TObject; ACanvas: TCanvas;
var Width, Height: Integer);
procedure Run1MeasureItem(Sender: TObject; ACanvas: TCanvas; var Width,
Height: Integer);
procedure SaveAs1MeasureItem(Sender: TObject; ACanvas: TCanvas;
var Width, Height: Integer);
procedure Exit1MeasureItem(Sender: TObject; ACanvas: TCanvas;
var Width, Height: Integer);
procedure N1DrawItem(Sender: TObject; ACanvas: TCanvas; ARect: TRect;
Selected: Boolean);
procedure N1MeasureItem(Sender: TObject; ACanvas: TCanvas; var Width,
Height: Integer);
procedure file1DrawItem(Sender: TObject; ACanvas: TCanvas;
ARect: TRect; Selected: Boolean);
private
{ Private declarations }
public
procedure DrawItem(Sender: TObject; ACanvas: TCanvas;
ARect: TRect; Selected: Boolean;StrOut:String);//这是画菜单的函数
procedure MeasureItem(Sender: TObject; ACanvas: TCanvas;
var Width, Height: Integer;StrOut:String);//这是定位菜单的函数
procedure DrawItem1(Sender: TObject; ACanvas: TCanvas;
ARect: TRect; Selected: Boolean);//这是画分隔符的函数
procedure MeasureItem1(Sender: TObject; ACanvas: TCanvas;
var Width, Height: Integer);//这是定位分隔符的函数
{ Public declarations }
end;
var
Form1: TForm1;
i,ih,ind,iw,irate:integer;
rtemp:trect;
ig1,ig2:integer;
canvas1:tcanvas;
implementation
{$R *.dfm}
procedure TForm1.DrawItem(Sender: TObject; ACanvas: TCanvas;
ARect: TRect; Selected: Boolean;StrOut:String);
var j,q:integer;
begin
q:=file1.Count;
i:=arect.Bottom-arect.Top;
ind:=TMenuItem(sender).MenuIndex;
ih:=round(image1.Height/q*ind);
OffsetRect(ARect,0,0);
stretchBlt(acanvas.Handle,arect.Left,arect.Top,iw,i,image1.Canvas.Handle,0,ih,image1.Width,round(image1.Height/q),srccopy);
if selected then
begin
acanvas.Font.Color:=clwhite;
rtemp:=arect;
rtemp.Left:=rtemp.Left+iw;
ig1:=round((rtemp.Right-rtemp.Left)/10);
rtemp.Right:=rtemp.Left+ig1;
for j:=0 to 9 do
begin
acanvas.Brush.Color:=rgb(0,0,j*25);
acanvas.FillRect(rtemp);
rtemp.Left:=rtemp.Left+ig1;
rtemp.Right:=rtemp.Left+ig1;
end;
end
else
begin
acanvas.Brush.Color:=cl3dlight;
rtemp:=arect;
rtemp.Left:=rtemp.Left+iw;
acanvas.FillRect(rtemp);
acanvas.Font.Color:=clblack;
end;
acanvas.Brush.Style:=bsclear;
acanvas.TextOut(arect.Left+iw+5,arect.Top,strout);
end;
procedure TForm1.DrawItem1(Sender: TObject; ACanvas: TCanvas;
ARect: TRect; Selected: Boolean);
var q:integer;
begin
q:=file1.Count;
i:=arect.Bottom-arect.Top;
ind:=TMenuItem(sender).MenuIndex;
ih:=round(image1.Height/q*ind);
OffsetRect(ARect,0,0);
stretchBlt(acanvas.Handle,arect.Left,arect.Top,iw,i,image1.Canvas.Handle,0,ih,image1.Width,round(image1.Height/q),srccopy);//图片照样贴上来
if selected then//对于分隔符,其实这个条件可以不要,但复制上面的代码,就懒得改了,呵呵
begin
acanvas.Font.Color:=clwhite;
rtemp:=arect;
rtemp.Left:=rtemp.Left+iw;
ig1:=round((rtemp.Right-rtemp.Left)/10);
rtemp.Right:=rtemp.Left+ig1;
end
else
begin
acanvas.Brush.Color:=clBtnFace;//第一层为系统颜色
rtemp:=arect;
rtemp.Left:=rtemp.Left+iw+3;
rtemp.Right:=arect.Right-3;
acanvas.FillRect(rtemp);
acanvas.Font.Color:=clMedGray;
end;
acanvas.Brush.Style:=bsSolid;
OffsetRect(rtemp,0,2);//下移2象素
acanvas.Brush.Color:=rgb(128,128,128
|