<form id="hz9zz"></form>
  • <form id="hz9zz"></form>

      <nobr id="hz9zz"></nobr>

      <form id="hz9zz"></form>

    1. 明輝手游網中心:是一個免費提供流行視頻軟件教程、在線學習分享的學習平臺!

      美化你的菜單

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

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

      OnMeasureItem事件的定義如下:

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

      var Width, Height: Integer) of object;

      property OnMeasureItem: TMenuMeasureItemEvent;

      該事件在菜單條監測自身的尺寸時產生,其中參數Acanvas定義繪制的繪圖對象,參數Width、Height制定菜單項的默認尺寸,注意到這兩個定義前的var了嗎,說明你可以在OnMeasureItem事件處理函數中改變這兩個值,也就是改變菜單的大小。

      OnDrawItem事件的定義如下:



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

      ARect: TRect; Selected: Boolean) of object;

      property OnDrawItem: TMenuDrawItemEvent;

      該事件在菜單繪制時引發,其中參數Acanvas定義菜單繪制對象,參數Arect制定菜單的繪制區域,參數Selected定義當前菜單項是否被選中。

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

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

      下面來介紹具體的程序,首先利用圖像軟件建立一個長條型的位圖文件(你可以根據你的需要設定圖像的高寬比,在我的圖像中是10:1)。在Delphi中建立一個新的工程,在Form1中加入一個TImage控件,將控件的AutoSize屬性設置為True。然后在Form1中加入一個TMainMenu控件,將它的OwnerDraw屬性設置為True(這一點很重要,否則程序無法實現)在該TMainMenu下加入6個TMenuItem對象(鼠標右健點擊TMainMenu控件,然后點擊彈出菜單的Menu Designer 項,就可以在設計窗口中添加菜單條了),將它們的Name屬性分別設置為 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上相應位置的位圖復制到菜單上

      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 //該菜單項被選中

      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;

      //設置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個程序同

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

      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.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.


      日韩精品一区二区三区高清