自画PopMenu弹出

   BorderColor:TColor; //边框颜色

  FillColor:TColor;   //未选中填充颜色

  TextColor:TColor;   //未选中字体颜色

  SelectTextColor:TColor; //选中字体颜色

  SelectFillColor:TColor; //选中填充颜色

  SideBuffer:Integer;  //边框宽度



procedure TForm1.FormCreate(Sender: TObject);

begin

  BorderColor:=clRed;

  FillColor:=clWhite;

  TextColor:=clBlue;

  SelectTextColor:=clYellow;

  SelectFillColor:=clGray;

  SideBuffer:=1;

end;



procedure TForm1.Button1Click(Sender: TObject);

var

  po:TPoint;

begin

  po.X:=TControl(Sender).Left;

  po.Y:=TControl(Sender).Top+TControl(Sender).Height;

  po:=ClientToScreen(po);

  PopupMenu1.Popup(po.X,po.Y);

end;



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

  ARect: TRect; Selected: Boolean);

var

  FocusRectBorder:TRect;

  FocusRectFill:TRect;

  TextRect:TRect;

  MenuItem:TMenuItem;

  Title:string;

begin

  TextRect:=ARect;

  MenuItem:=(Sender) as TMenuItem;

  Title:=MenuItem.Caption;

  //   填充菜单项背景颜色

  ACanvas.Brush.Color:=FillColor;

   ACanvas.FillRect(ARect);

  //没有菜单内容就返回。

  if Title='' then exit;



  //选中菜单

  if selected then

   begin

    //画菜单外面边框。

    FocusRectBorder:=ARect;

      ACanvas.Brush.Color := BorderColor;

      ACanvas.FrameRect(FocusRectBorder);

    //填充菜单内部

    FocusRectFill := ARect;

    //设置内部边框比外面要小点。

    FocusRectFill.Top :=  FocusRectFill.Top + SideBuffer;

      FocusRectFill.Left := FocusRectFill.Left + SideBuffer;

      FocusRectFill.Right := FocusRectFill.Right - SideBuffer;

      FocusRectFill.Bottom := FocusRectFill.Bottom - SideBuffer;

    //设置为高度显示的颜色

    ACanvas.Brush.Color := SelectFillColor;

      ACanvas.FillRect(FocusRectFill);



    //设置当菜单选中后字体的颜色

    ACanvas.Font.Color :=  SelectTextColor;

    ACanvas.Font.Style := ACanvas.Font.Style+[fsBold];

  end

  else  //没有选中

  begin

    //设置当菜单字体的颜色

    ACanvas.font.Color := TextColor;

    ACanvas.Font.Style := ACanvas.Font.Style+[fsBold];

  end;



  //画图标

  if MenuItem.ImageIndex<> -1 then

  begin

    ACanvas.Font.Style := ACanvas.Font.Style-[fsBold];

    ImageList1.Draw(ACanvas,0,(MenuItem.ImageIndex)*27,MenuItem.ImageIndex);

  end;



  //写文字

  TextRect.Left := TextRect.Left+5+24;

   TextRect.Top := TextRect.Top + 1;

   DrawText(ACanvas.Handle,PChar(Title),Length(title),TextRect,0);

end;

 

你可能感兴趣的:(menu)