程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> 更多編程語言 >> Delphi >> Delphi實用代碼:自繪XP風格菜單 (1)

Delphi實用代碼:自繪XP風格菜單 (1)

編輯:Delphi
這是以前寫的代碼,自繪XP風格的菜單,硬盤壞了後以為沒了,最後寫的一個軟件要自定義風格,“翻箱倒櫃”的終於在我可愛的古董機^_^上找到了一個應用的例子。

  把主菜單作以下調整:

  OnMeasureItem指向MeasureMainItem、

  OnAdvancedDrawItem指向DrawMainItem,

  子菜單的OnMeasureItem指向MeasureSubItem、

  OnAdvancedDrawItem指向DrawSubItem,

  調整顏色或使用默認顏色即可達到效果


  //調整主菜單項尺寸
  procedure MeasureMainItem(Sender: TObject; ACanvas: TCanvas; var Width, Height: Integer);
  begin
  Width := Width + 6;
  Height := Height + 2;
  end;

  //調整子菜單項尺寸
  procedure MeasureSubItem(Sender: TObject; ACanvas: TCanvas; var Width, Height: Integer);
  begin
  Width := Width + 20;
  Height := Height + 2;
  end;

  //繪制主菜單內容
  procedure DrawMainItem(Sender: TObject; ACanvas: TCanvas; ARect: TRect; State: TOwnerDrawState);
  const
  MainMenuBackColor      : TColor = $DEEDEF;
  MainMenuBorderColor    : TColor = $DEEDEF;
  MainMenuSelectedBackColor  : TColor = $DFA988;
  MainMenuSelectedBorderColor: TColor = $C08000;
  MainMenuHotLightBackColor  : TColor = $DEEDEF;
  MainMenuHotLightBorderColor: TColor = $800080;
  MainMenuGrayedBackColor  : TColor = $DEEDEF;
  var
  BrushColor, PenColor: TColor;
  TextRect: TRECT;
  begin
  if odGrayed in State then
  begin
  BrushColor := MainMenuGrayedBackColor;
  PenColor := MainMenuGrayedBackColor;
  end
  else
  if odHotLight in State then
  begin   //鼠標劃過
  BrushColor := MainMenuHotLightBackColor;
  PenColor := MainMenuHotLightBorderColor;
  end


  else
  if odSelected in State then
  begin
  BrushColor := MainMenuSelectedBackColor;
  PenColor := MainMenuSelectedBorderColor;
  end
  else
  begin
  BrushColor := MainMenuBackColor;
  PenColor := MainMenuBackColor;
  if TMenuItem(Sender).Caption = '幫助(&H)' then ARect.Right := ARect.Right + 1600;
  end;
  ACanvas.Brush.Color := BrushColor;
  ACanvas.Pen.Color := PenColor;
  //ACanvas.FillRect(ARect);
  ACAnvas.Rectangle(ARect);
  ACanvas.Brush.Style := bsClear;
  //繪出文字
  if odGrayed in State then
  ACanvas.Font.Color := clBtnShadow
  else
  ACanvas.Font.Color := clBlack;
  SetRect(TextRect, ARect.left+10, ARect.top+3, ARect.right, ARect.bottom);
  DrawText(ACanvas.Handle, PChar(TMenuItem(Sender).Caption), Length(TMenuItem(Sender).Caption),
  TextRect, DT_LEFT);
  end;

  //繪制子菜單內容
  procedure DrawSubItem(Sender: TObject; ACanvas: TCanvas; ARect: TRect; State: TOwnerDrawState);
  const
  SubMenuBackColor      : TColor = $F7F8F9;
  SubMenuBorderColor    : TColor = $DEEDEF;
  SubMenuSelectedBackColor  : TColor = $EED2C1;
  SubMenuSelectedBorderColor: TColor = $C08000;
  SubMenuLineColor      : TColor = $C8D0D4;
  //SubMenuHotLightBorderColor: TColor = $C08000;
  SubMenuGrayedBackColor  : TColor = $F7F8F9;//$DEEDEF;
  var
  BrushColor, PenColor: TColor;
  TextRect: TRECT;
  str: String;
  ImageList: TCustomImageList;
  begin
  if (odGrayed in State) and not(TMenuItem(Sender).IsLine) then
  begin
  BrushColor := SubMenuGrayedBackColor;
  {if odSelected in State then
  PenColor := SubMenuSelectedBorderColor
  else}

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved