明輝手游網(wǎng)中心:是一個(gè)免費(fèi)提供流行視頻軟件教程、在線學(xué)習(xí)分享的學(xué)習(xí)平臺(tái)!

美化你的菜單

[摘要]Windows下的很多程序都有十分漂亮的菜單,例如Windows“開始”菜單左方從上到下的長(zhǎng)條形的Windows Banner 又或者是向Word那樣在每一個(gè)菜單條左邊都有一個(gè)小圖標(biāo),看到這些很Cool的菜單,你是否覺得自己的菜單顯得單調(diào)乏味呢?不需要第三方控件,利用Delphi就可以實(shí)現(xiàn)上面的功...
Windows下的很多程序都有十分漂亮的菜單,例如Windows“開始”菜單左方從上到下的長(zhǎng)條形的Windows Banner 又或者是向Word那樣在每一個(gè)菜單條左邊都有一個(gè)小圖標(biāo),看到這些很Cool的菜單,你是否覺得自己的菜單顯得單調(diào)乏味呢?不需要第三方控件,利用Delphi就可以實(shí)現(xiàn)上面的功能。

如果要實(shí)現(xiàn)自定義菜單就需要在繪制菜單時(shí)改變菜單的大小以適應(yīng)在菜單上繪制圖形,然后再在上面繪制自己所需要的菜單效果。在Delphi中,每一個(gè)菜單項(xiàng)對(duì)應(yīng)一個(gè)TmenuItem控件,這類控件都有兩個(gè)事件:OnDrawItem和OnMeasureItem,要實(shí)現(xiàn)自定義菜單,首先要介紹一下這兩個(gè)事件:

OnMeasureItem事件的定義如下:

type TMenuMeasureItemEvent = procedure (Sender: TObject; ACanvas: TCanvas;

var Width, Height: Integer) of object;

property OnMeasureItem: TMenuMeasureItemEvent;

該事件在菜單條監(jiān)測(cè)自身的尺寸時(shí)產(chǎn)生,其中參數(shù)Acanvas定義繪制的繪圖對(duì)象,參數(shù)Width、Height制定菜單項(xiàng)的默認(rèn)尺寸,注意到這兩個(gè)定義前的var了嗎,說(shuō)明你可以在OnMeasureItem事件處理函數(shù)中改變這兩個(gè)值,也就是改變菜單的大小。

OnDrawItem事件的定義如下:



type TMenuDrawItemEvent = procedure (Sender: TObject; ACanvas: TCanvas;

ARect: TRect; Selected: Boolean) of object;

property OnDrawItem: TMenuDrawItemEvent;

該事件在菜單繪制時(shí)引發(fā),其中參數(shù)Acanvas定義菜單繪制對(duì)象,參數(shù)Arect制定菜單的繪制區(qū)域,參數(shù)Selected定義當(dāng)前菜單項(xiàng)是否被選中。

從上面的介紹可以看到,要實(shí)現(xiàn)自定義的菜單,只要在OnMeasureItem事件中編寫代碼改變菜單項(xiàng)的尺寸,然后在OnDrawItem事件中繪制自己需要的效果就可以了。

下面我痛過具體的范例來(lái)做說(shuō)明,這個(gè)范例是使自己的菜單實(shí)現(xiàn)象Windows開始菜單一樣的顯示Banner條的功能。同時(shí)這個(gè)程序還能實(shí)現(xiàn)對(duì)被選中的菜單條進(jìn)行漸變色填充(就象3721中文網(wǎng)址軟件的任務(wù)欄菜單那樣)。程序的思路是這樣的,首先建立一個(gè)長(zhǎng)條型的位圖,然后在每一個(gè)菜單條的OnMeasureItem事件中根據(jù)要顯示在菜單上的文本和圖像以及程序的需要改變菜單項(xiàng)的寬度和高度,然后在OnDrawItem事件中將位圖中的相應(yīng)部分拷貝到菜單項(xiàng)上。如果該菜單條被選中,首先要改變Acanvas參數(shù)的畫刷顏色,然后再依次填充菜單條上的相應(yīng)部分,這樣就實(shí)現(xiàn)了對(duì)選中的菜單條實(shí)現(xiàn)漸變色填充。最后將文本輸出到菜單條上。

下面來(lái)介紹具體的程序,首先利用圖像軟件建立一個(gè)長(zhǎng)條型的位圖文件(你可以根據(jù)你的需要設(shè)定圖像的高寬比,在我的圖像中是10:1)。在Delphi中建立一個(gè)新的工程,在Form1中加入一個(gè)TImage控件,將控件的AutoSize屬性設(shè)置為True。然后在Form1中加入一個(gè)TMainMenu控件,將它的OwnerDraw屬性設(shè)置為True(這一點(diǎn)很重要,否則程序無(wú)法實(shí)現(xiàn))在該TMainMenu下加入6個(gè)TMenuItem對(duì)象(鼠標(biāo)右健點(diǎn)擊TMainMenu控件,然后點(diǎn)擊彈出菜單的Menu Designer 項(xiàng),就可以在設(shè)計(jì)窗口中添加菜單條了),將它們的Name屬性分別設(shè)置為 Caption1、Caption2、…、Caption6。

下面是具體的程序清單:



unit OwnerMenu;







interface







uses



Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,

Menus, ExtCtrls, StdCtrls, ImgList;





type



TForm1 = class(TForm)

MainMenu1: TMainMenu;

Main1: TMenuItem;



Caption1: TMenuItem;

Caption2: TMenuItem;

Caption3: TMenuItem;

Caption4: TMenuItem;

Caption5: TMenuItem;

Caption6: TMenuItem;

Image1: TImage;



procedure Caption1MeasureItem(Sender: TObject; ACanvas: TCanvas;

var Width, Height: Integer);

procedure Caption2MeasureItem(Sender: TObject; ACanvas: TCanvas;

var Width, Height: Integer);

procedure Caption3MeasureItem(Sender: TObject; ACanvas: TCanvas;

var Width, Height: Integer);

procedure Caption4MeasureItem(Sender: TObject; ACanvas: TCanvas;

var Width, Height: Integer);

procedure Caption5MeasureItem(Sender: TObject; ACanvas: TCanvas;

var Width, Height: Integer);

procedure Caption6MeasureItem(Sender: TObject; ACanvas: TCanvas;

var Width, Height: Integer);

procedure Caption1DrawItem(Sender: TObject; ACanvas: TCanvas;

ARect: TRect; Selected: Boolean);

procedure Caption2DrawItem(Sender: TObject; ACanvas: TCanvas;

ARect: TRect; Selected: Boolean);

procedure Caption3DrawItem(Sender: TObject; ACanvas: TCanvas;

ARect: TRect; Selected: Boolean);

procedure Caption4DrawItem(Sender: TObject; ACanvas: TCanvas;

ARect: TRect; Selected: Boolean);

procedure Caption5DrawItem(Sender: TObject; ACanvas: TCanvas;

ARect: TRect; Selected: Boolean);

procedure Caption6DrawItem(Sender: TObject; ACanvas: TCanvas;

ARect: TRect; Selected: Boolean);

private



{ Private declarations }

public



procedure DrawItem(Sender: TMenuItem; ACanvas: TCanvas;ARect: TRect;

Selected: Boolean;strOUt:String);

{ Public declarations }

end;

var

Form1: TForm1;

i,iH,Ind,iW,iRate:Integer;

rTemp:TRect;

iG1,iG2:Integer;

implementation

{$R *.DFM}

procedure TForm1.DrawItem(Sender: TMenuItem; ACanvas: TCanvas;ARect: TRect;

Selected: Boolean;strOut:String);

var

j:Integer;

begin

i:=ARect.Bottom -ARect.Top; //獲得貼圖的高度和寬度

Ind:=Sender.MenuIndex;

iH:=Round(Image1.Height/6*Ind); //獲得貼圖位置

//將Image上相應(yīng)位置的位圖復(fù)制到菜單上

StretchBlt(ACanvas.Handle,ARect.Left,ARect.Top,iW,i,Image1.Canvas.Handle,0,iH,

Image1.Width,Round(Image1.Height/6),SRCCOPY);

if Selected then begin //該菜單項(xiàng)被選中

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 //通過循環(huán)設(shè)置色彩漸變效果

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 //該菜單項(xiàng)沒有被選中

ACanvas.Brush.Color := cl3DLight; //設(shè)置背景色為淺灰

rTemp:=ARect;

rTemp.Left := rTemp.left+iW;

ACanvas.FillRect(rTemp);

ACanvas.Font.Color := clBlack;

end;

//設(shè)置Canvas的畫筆填充模式為透明

ACanvas.Brush.Style:=bsClear;

//在菜單上輸出文字

ACanvas.TextOut(ARect.Left+iW+5,ARect.Top,strOut);

end;

procedure TForm1.Caption1MeasureItem(Sender: TObject; ACanvas: TCanvas;

var Width, Height: Integer);

begin



//在OnMeasureItem事件中改變菜單的寬度和高度,下面5個(gè)程序同

//改變菜單的寬度和高度以容納文本

Height:=ACanvas.TextHeight('Caption1')+5;

Width:=ACanvas.TextWidth('Caption1')+5;

iRate:=Round(Image1.Height/(Height*6));

iW:=Round(Image1.Width /iRate);

Width:=Width+iW; //根據(jù)計(jì)算改變菜單寬度以容納附加的文本

end;







procedure TForm1.Caption2MeasureItem(Sender: TObject; ACanvas: TCanvas;

var Width, Height: Integer);

begin



Height:=ACanvas.TextHeight('Caption1')+5;

Width:=ACanvas.TextWidth('Caption1')+5;

iRate:=Round(Image1.Height/(Height*6));

iW:=Round(Image1.Width /iRate);

Width:=Width+iW;



end;







procedure TForm1.Caption3MeasureItem(Sender: TObject; ACanvas: TCanvas;

var Width, Height: Integer);

begin



Height:=ACanvas.TextHeight('Caption1')+5;

Width:=ACanvas.TextWidth('Caption1')+5;

iRate:=Round(Image1.Height/(Height*6));

iW:=Round(Image1.Width /iRate);

Width:=Width+iW;



end;







procedure TForm1.Caption4MeasureItem(Sender: TObject; ACanvas: TCanvas;

var Width, Height: Integer);

begin



Height:=ACanvas.TextHeight('Caption1')+5;

Width:=ACanvas.TextWidth('Caption1')+5;

iRate:=Round(Image1.Height/(Height*6));

iW:=Round(Image1.Width /iRate);

Width:=Width+iW;



end;







procedure TForm1.Caption5MeasureItem(Sender: TObject; ACanvas: TCanvas;

var Width, Height: Integer);

begin



Height:=ACanvas.TextHeight('Caption1')+5;

Width:=ACanvas.TextWidth('Caption1')+5;

iRate:=Round(Image1.Height/(Height*6));

iW:=Round(Image1.Width /iRate);

Width:=Width+iW;



end;







procedure TForm1.Caption6MeasureItem(Sender: TObject; ACanvas: TCanvas;

var Width, Height: Integer);

begin



Height:=ACanvas.TextHeight('Caption1')+5;

Width:=ACanvas.TextWidth('Caption1')+5;

iRate:=Round(Image1.Height/(Height*6));

iW:=Round(Image1.Width /iRate);

Width:=Width+iW;



end;







procedure TForm1.Caption1DrawItem(Sender: TObject; ACanvas: TCanvas;

ARect: TRect; Selected: Boolean);

begin



DrawItem(TMenuItem(Sender),ACanvas,ARect,Selected,'Caption1');

end;







procedure TForm1.Caption2DrawItem(Sender: TObject; ACanvas: TCanvas;

ARect: TRect; Selected: Boolean);

begin



DrawItem(TMenuItem(Sender),ACanvas,ARect,Selected,'Caption2');

end;



procedure TForm1.Caption3DrawItem(Sender: TObject; ACanvas: TCanvas;

ARect: TRect; Selected: Boolean);

begin



DrawItem(TMenuItem(Sender),ACanvas,ARect,Selected,'Caption3');

end;

procedure TForm1.Caption4DrawItem(Sender: TObject; ACanvas: TCanvas;

ARect: TRect; Selected: Boolean);

begin



DrawItem(TMenuItem(Sender),ACanvas,ARect,Selected,'Caption4');

end;


procedure TForm1.Caption5DrawItem(Sender: TObject; ACanvas: TCanvas;

ARect: TRect; Selected: Boolean);

begin

DrawItem(TMenuItem(Sender),ACanvas,ARect,Selected,'Caption5');

end;

procedure TForm1.Caption6DrawItem(Sender: TObject; ACanvas: TCanvas;

ARect: TRect; Selected: Boolean);

begin

DrawItem(TMenuItem(Sender),ACanvas,ARect,Selected,'Caption6');

end;

end.


標(biāo)簽:美化你的菜單 

相關(guān)文章