【问题标题】:Delphi change color of main menuDelphi改变主菜单的颜色
【发布时间】:2015-06-20 15:22:38
【问题描述】:

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

It disappears when running the mouse over the menu but comes back when another application is selected.我怎样才能摆脱它?

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

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.

【问题讨论】:

  • 这似乎证明了'ARect'的用途,指定你可以绘制的区域。你在外面画的东西以后可能会透支。底线可能是项目区域之外的NC区域。
  • 请问你为什么不用TMainActionMenuBar?你可以连接到TStandardColorMap,达到你想要的效果。

标签: delphi menu


【解决方案1】:

OnAdvancedDrawItem 事件处理程序的ARect 参数是传递给WM_DRAWITEM 消息的DRAWITEMSTRUCTrcItemdocumentation 对矩形有这样的说法:

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

因此,尽管设备上下文没有被裁剪到矩形,但您有责任不要在矩形之外进行绘制。当您在填充矩形之前执行 Inc(ARect.Bottom,1); 时会发生这种情况。

【讨论】:

  • 你的回答很有启发性。但是,当重新绘制不是由菜单操作启动时,它仍然给我留下了阻止窗口在菜单栏下绘制白线的问题。
  • @Rudi - 嗯,答案基本上暗示“你无法摆脱它”。至少不容易。您将无法使用所有者绘制的菜单来实现它。您必须自己假设非客户区绘画。我觉得这是一个复杂的主题,超出了这个问题的范围。我当然也不推荐它。正如您自己所见,操作系统会在非常奇怪的时候干预 NC 绘制,例如窗口停用。
【解决方案2】:

您可以更改灰色区域的颜色。在 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;

【讨论】:

    猜你喜欢
    • 2022-01-07
    • 2012-10-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-11-22
    • 2020-03-31
    • 1970-01-01
    相关资源
    最近更新 更多