Delphi改变主菜单的颜色

时间:2014-06-21 13:30:33

标签: delphi menu

我正在创建自己的OnAdvancedDrawItem来更改MainMenu的颜色。它运作良好,但我在底部有一条恼人的白线。

line under main menu

在菜单上运行鼠标时会消失,但在选择其他应用程序时会返回。我怎么能摆脱它?

这是我背景着色的基本代码。

unit MenMain;

interface

uses Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,Dialogs, Menus, ImgList, StdCtrls;

type
  TForm1 = class(TForm)
  MainMenu1: TMainMenu;
  File2: TMenuItem;
  Edit1: TMenuItem;
  Window1: TMenuItem;
  procedure Window1AdvancedDrawItem(Sender: TObject; ACanvas: TCanvas; ARect: TRect; State: TOwnerDrawState);
private

public

end;

var
  Form1: TForm1;

implementation

{$R *.dfm}

procedure TForm1.Window1AdvancedDrawItem(Sender: TObject; ACanvas: TCanvas;  ARect: TRect; State: TOwnerDrawState);
begin
  with TMenuItem(Sender) do
  begin
    with ACanvas do
    begin
      Brush.Color := clMoneyGreen;
      Inc(ARect.Bottom,1);
      FillRect(ARect);
      Font.Color := clBlue;
      DrawText(ACanvas.Handle, PChar(Caption),Length(Caption),ARect,          DT_SINGLELINE or DT_VCENTER);
    end;
  end;
end;

end.

2 个答案:

答案 0 :(得分:6)

ARect事件处理程序的OnAdvancedDrawItem参数是rcItem传递给DRAWITEMSTRUCT消息的WM_DRAWITEMdocumentation对矩形有这个说法:

  

一个矩形,用于定义要绘制的控件的边界。   此矩形位于 hDC 成员指定的设备上下文中。   系统会自动剪辑所有者窗口绘制的所有内容   组合框,列表框和按钮的设备上下文,但确实如此   不剪辑菜单项。绘制菜单项时,所有者窗口必须   不要在 rcItem 定义的矩形边界之外绘制   构件。

因此,虽然设备上下文没有剪切到矩形,但您负责不在其外部绘图。在填充矩形之前执行Inc(ARect.Bottom,1);时会发生这种情况。

答案 1 :(得分:0)

您可以更改灰色区域的颜色。在OnCreate和OnCanResize

中使用它
global var - fMenuBrushHandle: THandle;

var
  lMenuInfo: TMenuInfo;
  lMenuColor: TColor;
begin
  lMenuColor := clRed;

  DeleteObject(fMenuBrushHandle);
  fMenuBrushHandle := CreateSolidBrush(ColorToRGB(lMenuColor));

  FillChar(lMenuInfo, SizeOf(lMenuInfo), 0);

  lMenuInfo.cbSize := SizeOf(lMenuInfo);
  lMenuInfo.hbrBack := fMenuBrushHandle;
  lMenuInfo.fMask := MIM_BACKGROUND;
  SetMenuInfo(MainMenu1.Handle, lMenuInfo);
end;

global var - FBrush: TBrush;

var
  lMenuInfo: TMenuInfo;
begin
  if not Assigned(FBrush) then
     FBrush := TBrush.Create;
  FBrush.Color := clRed;
  FBrush.Style := bsSolid;

  lMenuInfo.cbSize := SizeOf(lMenuInfo);
  lMenuInfo.fMask := MIM_BACKGROUND;
  lMenuInfo.hbrBack := FBrush.Handle;
  SetMenuInfo(MainMenu1.Handle, lMenuInfo);
end;

甚至绘制位图

global var
  fMenuHandle:THandle;
  fBitmap:Tbitmap;

var
  lMenuInfo:TMenuInfo;
begin
  if Assigned(fBitmap) then
    fBitmap.Free;
  fBitmap:=TBitmap.Create;
  fBitmap.Width:=21;
  fBitmap.Height:=Form1.Width;

  DeleteObject(fMenuHandle);
  fMenuHandle:=CreatePatternBrush(fBitmap.Handle);
  Fillchar(lMenuInfo,SizeOf(lMenuInfo),0);

  lMenuInfo.cbSize:=SizeOf(lMenuInfo);
  lMenuInfo.fMask:=MIM_BACKGROUND;
  lMenuInfo.hbrBack:=fMenuHandle;
  SetMenuInfo(MainMenu1.Handle,lMenuInfo);
end;