嘻嘻一样的贴子,一样的答复: 这是我以前写的一个XPMenu组件,写得很不好。就算给你一个思路吧,你看看吧,主要是写自画事件(OnDrawItem): unit XPMenu;interfaceuses Windows, Messages,SysUtils, Classes,Menus,Graphics;type TXPMenu = class(TComponent) private { Private declarations } FSelColor,FBdyColor,FIcnColor,FFntColor:TColor; procedure SetSelColor(SelColor:TColor); function GetSelColor:TColor; procedure SetBdyColor(BdyColor:TColor); function GetBdyColor:TColor; procedure SetFntColor(FntColor:TColor); function GetFntColor:TColor; procedure SetIcnColor(IcnColor:TColor); function GetIcnColor:TColor; procedure DrawItem(Sender: TObject; ACanvas: TCanvas; ARect: TRect; Selected: Boolean); procedure MeasureItem(Sender: TObject; ACanvas: TCanvas; var Width, Height: Integer); protected { Protected declarations } public { Public declarations } constructor Create(AOwner:TComponent);override; published { Published declarations } property BodyColor:TColor read GetBdyColor write SetBdyColor; property SelectedColor:TColor read GetSelColor write SetSelColor; property FontColor:TColor read GetFntColor write SetFntColor; property GlyphColor:TColor read GetIcnColor write SetIcnColor; end;procedure Register;implementationprocedure Register; begin RegisterComponents('BlazingFire', [TXPMenu]); end;{ TXPMenu }procedure TXPMenu.MeasureItem(Sender: TObject; ACanvas: TCanvas; var Width, Height: Integer); var Txt:String; begin ACanvas.Font.Size:=8; Txt:=(Sender as TMenuItem).Caption; Width :=ACanvas.TextWidth(Txt)+20; Height:=ACanvas.TextHeight(Txt)+6; if Txt='-' then Height:=ACanvas.TextHeight(Txt) div 2; end;constructor TXPMenu.Create(AOwner: TComponent); var icount:integer; begin inherited create(AOwner); FBdyColor:=$00E6E6E6; FIcnColor:=$0074F1B9; FFntColor:=clBlack; FSelColor:=$00FFD8CE; for icount:=0 to Owner.ComponentCount-1 do begin if Owner.Components[icount] Is TMenu then (Owner.Components[icount] as TMenu).OwnerDraw:=True; if Owner.Components[icount] Is TMenuItem then begin (Owner.Components[icount] as TMenuItem).OnDrawItem:=DrawItem; (Owner.Components[icount] as TMenuItem).OnMeasureItem:=MeasureItem; end; end; end;procedure TXPMenu.DrawItem(Sender: TObject; ACanvas: TCanvas; ARect: TRect; Selected: Boolean); var MenuItem:TMenuItem; IcnRect,TxtRect:TRect; Txt:String; B:TBitmap; icount:integer; IsTopItem:Boolean; begin MenuItem:=Sender as TMenuItem; Txt:=MenuItem.Caption; IcnRect:=Rect(ARect.Left,ARect.Top, ARect.Left+20,ARect.Bottom); TxtRect:=Rect(ARect.Left+22,ARect.Top,ARect.Right,ARect.Bottom); IsTopItem:=False; if MenuItem.GetParentMenu Is TMainMenu then for icount:=0 to MenuItem.GetParentMenu.Items.Count-1 do if MenuItem.GetParentMenu.Items[icount]=MenuItem then begin IsTopItem:=True; break; end; if IsTopItem then begin ACanvas.Brush.Color:=clBtnFace; ACanvas.Pen.Color:=clBtnFace; ACanvas.Rectangle(IcnRect); end else begin ACanvas.Brush.Color:=FIcnColor; ACanvas.Pen.Color:=FIcnColor; ACanvas.Rectangle(IcnRect); ACanvas.Brush.Color:=FBdyColor; ACanvas.Pen.Color:=FBdyColor; end; ACanvas.Rectangle(TxtRect.Left-2,TxtRect.Top,TxtRect.Right,TxtRect.Bottom); if Selected then begin ACanvas.Brush.Color:=FSelColor; ACanvas.Pen.Color :=clBlack; ACanvas.Rectangle(ARect); end; if Txt='-' then begin ACanvas.Pen.Width :=1; ACanvas.Pen.Color:=clGray; ACanvas.MoveTo(TxtRect.Left -1 ,TxtRect.Top+(TxtRect.Bottom-TxtRect.Top) div 2); ACanvas.LineTo(TxtRect.Right-2 ,TxtRect.Top+(TxtRect.Bottom-TxtRect.Top) div 2); end else begin ACanvas.Font.Size:=8; if MenuItem.Enabled then ACanvas.Font.Color:=FFntColor else ACanvas.Font.Color:=clGray; SetBkMode(ACanvas.Handle,Transparent); DrawText(ACanvas.Handle, Pchar(Txt), Length(Txt), TxtRect, DT_VCenter or DT_Left or DT_SingleLine ); end; if MenuItem.Checked then begin ACanvas.Pen.Color :=clGray; ACanvas.Pen.Width :=1; ACanvas.Polyline([Point(IcnRect.Left+2 ,IcnRect.Bottom-2), Point(IcnRect.Left+2 ,IcnRect.Top+2 ), Point(IcnRect.Right-2,IcnRect.Top+2 )]); ACanvas.Pen.Color :=clWhite; ACanvas.Pen.Width :=1; ACanvas.Polyline([Point(IcnRect.Right-2,IcnRect.Top+2 ), Point(IcnRect.Right-2,IcnRect.Bottom-2), Point(IcnRect.Left+2 ,IcnRect.Bottom-2)]); ACanvas.Pen.Color :=clRed ; ACanvas.Pen.Width :=2; ACanvas.Polyline([Point(IcnRect.Left+4 ,IcnRect.Top+10 ), Point(IcnRect.Left+8 ,IcnRect.Top+14 ), Point(IcnRect.Right-5,IcnRect.Top+5 )]); end else if (MenuItem.GetParentMenu.Images<>nil)and(Txt<>'-') then begin B:=TBitmap.Create; B.Transparent:=True; MenuItem.GetParentMenu.Images.GetBitmap(MenuItem.ImageIndex,B); IcnRect:=Rect(IcnRect.Left+2,IcnRect.Top+2,IcnRect.Right-2,IcnRect.Bottom-2); ACanvas.StretchDraw(IcnRect,B); B.Free; end; end;function TXPMenu.GetSelColor: TColor; begin Result:=FSelColor; end;procedure TXPMenu.SetSelColor(SelColor: TColor); begin FSelColor:=SelColor; end;function TXPMenu.GetBdyColor: TColor; begin Result:=FBdyColor; end;procedure TXPMenu.SetBdyColor(BdyColor: TColor); begin FBdyColor:=BdyColor; end;function TXPMenu.GetFntColor: TColor; begin Result:=FFntColor; end;procedure TXPMenu.SetFntColor(FntColor: TColor); begin FFntColor:=FntColor; end;function TXPMenu.GetIcnColor: TColor; begin Result:=FIcnColor; end;procedure TXPMenu.SetIcnColor(IcnColor: TColor); begin FIcnColor:=IcnColor; end;end.
在Additional页拖个TActionManager和TActionMainMenuBar,在TActionManager里编写菜单内容,然后拖到TActionMainMenuBar上,就可以了
如果想再改颜色,再拖个TXPColorMap下来
这是我以前写的一个XPMenu组件,写得很不好。就算给你一个思路吧,你看看吧,主要是写自画事件(OnDrawItem):
unit XPMenu;interfaceuses
Windows, Messages,SysUtils, Classes,Menus,Graphics;type
TXPMenu = class(TComponent)
private
{ Private declarations }
FSelColor,FBdyColor,FIcnColor,FFntColor:TColor;
procedure SetSelColor(SelColor:TColor);
function GetSelColor:TColor;
procedure SetBdyColor(BdyColor:TColor);
function GetBdyColor:TColor;
procedure SetFntColor(FntColor:TColor);
function GetFntColor:TColor;
procedure SetIcnColor(IcnColor:TColor);
function GetIcnColor:TColor;
procedure DrawItem(Sender: TObject; ACanvas: TCanvas;
ARect: TRect; Selected: Boolean);
procedure MeasureItem(Sender: TObject; ACanvas: TCanvas;
var Width, Height: Integer);
protected
{ Protected declarations }
public
{ Public declarations } constructor Create(AOwner:TComponent);override;
published
{ Published declarations }
property BodyColor:TColor read GetBdyColor write SetBdyColor;
property SelectedColor:TColor read GetSelColor write SetSelColor;
property FontColor:TColor read GetFntColor write SetFntColor;
property GlyphColor:TColor read GetIcnColor write SetIcnColor;
end;procedure Register;implementationprocedure Register;
begin
RegisterComponents('BlazingFire', [TXPMenu]);
end;{ TXPMenu }procedure TXPMenu.MeasureItem(Sender: TObject; ACanvas: TCanvas;
var Width, Height: Integer);
var
Txt:String;
begin
ACanvas.Font.Size:=8;
Txt:=(Sender as TMenuItem).Caption;
Width :=ACanvas.TextWidth(Txt)+20;
Height:=ACanvas.TextHeight(Txt)+6;
if Txt='-' then
Height:=ACanvas.TextHeight(Txt) div 2;
end;constructor TXPMenu.Create(AOwner: TComponent);
var
icount:integer;
begin
inherited create(AOwner);
FBdyColor:=$00E6E6E6;
FIcnColor:=$0074F1B9;
FFntColor:=clBlack;
FSelColor:=$00FFD8CE;
for icount:=0 to Owner.ComponentCount-1 do
begin
if Owner.Components[icount] Is TMenu then
(Owner.Components[icount] as TMenu).OwnerDraw:=True;
if Owner.Components[icount] Is TMenuItem then
begin
(Owner.Components[icount] as TMenuItem).OnDrawItem:=DrawItem;
(Owner.Components[icount] as TMenuItem).OnMeasureItem:=MeasureItem;
end;
end;
end;procedure TXPMenu.DrawItem(Sender: TObject; ACanvas: TCanvas; ARect: TRect;
Selected: Boolean);
var
MenuItem:TMenuItem;
IcnRect,TxtRect:TRect;
Txt:String;
B:TBitmap;
icount:integer;
IsTopItem:Boolean;
begin
MenuItem:=Sender as TMenuItem;
Txt:=MenuItem.Caption;
IcnRect:=Rect(ARect.Left,ARect.Top, ARect.Left+20,ARect.Bottom);
TxtRect:=Rect(ARect.Left+22,ARect.Top,ARect.Right,ARect.Bottom);
IsTopItem:=False;
if MenuItem.GetParentMenu Is TMainMenu then
for icount:=0 to MenuItem.GetParentMenu.Items.Count-1 do
if MenuItem.GetParentMenu.Items[icount]=MenuItem then
begin
IsTopItem:=True;
break;
end;
if IsTopItem then
begin
ACanvas.Brush.Color:=clBtnFace;
ACanvas.Pen.Color:=clBtnFace;
ACanvas.Rectangle(IcnRect);
end
else
begin
ACanvas.Brush.Color:=FIcnColor;
ACanvas.Pen.Color:=FIcnColor;
ACanvas.Rectangle(IcnRect);
ACanvas.Brush.Color:=FBdyColor;
ACanvas.Pen.Color:=FBdyColor;
end;
ACanvas.Rectangle(TxtRect.Left-2,TxtRect.Top,TxtRect.Right,TxtRect.Bottom);
if Selected then
begin
ACanvas.Brush.Color:=FSelColor;
ACanvas.Pen.Color :=clBlack;
ACanvas.Rectangle(ARect);
end;
if Txt='-' then
begin
ACanvas.Pen.Width :=1;
ACanvas.Pen.Color:=clGray;
ACanvas.MoveTo(TxtRect.Left -1 ,TxtRect.Top+(TxtRect.Bottom-TxtRect.Top) div 2);
ACanvas.LineTo(TxtRect.Right-2 ,TxtRect.Top+(TxtRect.Bottom-TxtRect.Top) div 2);
end
else
begin
ACanvas.Font.Size:=8;
if MenuItem.Enabled then
ACanvas.Font.Color:=FFntColor
else
ACanvas.Font.Color:=clGray;
SetBkMode(ACanvas.Handle,Transparent);
DrawText(ACanvas.Handle,
Pchar(Txt),
Length(Txt),
TxtRect,
DT_VCenter or DT_Left or DT_SingleLine );
end;
if MenuItem.Checked then
begin
ACanvas.Pen.Color :=clGray;
ACanvas.Pen.Width :=1;
ACanvas.Polyline([Point(IcnRect.Left+2 ,IcnRect.Bottom-2),
Point(IcnRect.Left+2 ,IcnRect.Top+2 ),
Point(IcnRect.Right-2,IcnRect.Top+2 )]);
ACanvas.Pen.Color :=clWhite;
ACanvas.Pen.Width :=1;
ACanvas.Polyline([Point(IcnRect.Right-2,IcnRect.Top+2 ),
Point(IcnRect.Right-2,IcnRect.Bottom-2),
Point(IcnRect.Left+2 ,IcnRect.Bottom-2)]);
ACanvas.Pen.Color :=clRed ;
ACanvas.Pen.Width :=2;
ACanvas.Polyline([Point(IcnRect.Left+4 ,IcnRect.Top+10 ),
Point(IcnRect.Left+8 ,IcnRect.Top+14 ),
Point(IcnRect.Right-5,IcnRect.Top+5 )]);
end
else if (MenuItem.GetParentMenu.Images<>nil)and(Txt<>'-') then
begin
B:=TBitmap.Create;
B.Transparent:=True;
MenuItem.GetParentMenu.Images.GetBitmap(MenuItem.ImageIndex,B);
IcnRect:=Rect(IcnRect.Left+2,IcnRect.Top+2,IcnRect.Right-2,IcnRect.Bottom-2);
ACanvas.StretchDraw(IcnRect,B);
B.Free;
end;
end;function TXPMenu.GetSelColor: TColor;
begin
Result:=FSelColor;
end;procedure TXPMenu.SetSelColor(SelColor: TColor);
begin
FSelColor:=SelColor;
end;function TXPMenu.GetBdyColor: TColor;
begin
Result:=FBdyColor;
end;procedure TXPMenu.SetBdyColor(BdyColor: TColor);
begin
FBdyColor:=BdyColor;
end;function TXPMenu.GetFntColor: TColor;
begin
Result:=FFntColor;
end;procedure TXPMenu.SetFntColor(FntColor: TColor);
begin
FFntColor:=FntColor;
end;function TXPMenu.GetIcnColor: TColor;
begin
Result:=FIcnColor;
end;procedure TXPMenu.SetIcnColor(IcnColor: TColor);
begin
FIcnColor:=IcnColor;
end;end.
哪个XPMenu源代码我看了,但是有很多函数、技巧不是很懂???
真希望,那位仁兄能详细解释一下(标明注释也行,但是要讲解)!!!