TActionMainMenuBar渐变绘制

3

我试图通过创建一个继承自TActionMainMenuBar的组件来扩展它。

其中一件事是允许在菜单栏上绘制渐变。我已经成功实现了这一点,但在设置TActionManager为XPStyle时会出现菜单项的问题。

请看下面这些不同TActionManager样式的图片:

平台默认(鼠标悬停和按下):

enter image description here enter image description here

当TActionManager设置为“平台默认”时,背景会正确显示,并且可以清晰地看到渐变效果。

然而,当TActionManager设置为XPStyle时,某些菜单项的渐变将无法正常绘制,只需要查看第一项即可:

XP样式(鼠标悬停和按下):

enter image description here enter image description here

如您所见,这样的效果并不理想。

我已经在Paint中编辑了一些图像,大致说明了我希望效果如何:

鼠标悬停和选定

enter image description here enter image description here

我认为这将允许创建一些有趣的菜单样式。

以下是我到目前为止的代码:

unit uMyMenu;

interface

uses
  Classes,
  Types,
  Graphics,
  GraphUtil,
  ActnMan,
  ActnCtrls,
  ActnMenus,
  PlatformDefaultStyleActnCtrls,
  XPActnCtrls,
  XPStyleActnCtrls;

type
  TMyActionMenu = class(TActionMainMenuBar)
  private
    FColorStart: TColor;
    FColorEnd: TColor;

    procedure SetColorStart(AValue: TColor);
    procedure SetColorEnd(AValue: TColor);
  protected
    procedure Paint(Sender: TObject);
  public
    constructor Create(AOwner: TComponent); override;
    destructor Destroy; override;
  published
    property ColorStart: TColor read FColorStart write SetColorStart default clSkyBlue;
    property ColorEnd: TColor read FColorEnd write SetColorEnd default clHighlight;
  end;

procedure Register;

implementation

{ TMyActionMenu }

constructor TMyActionMenu.Create(AOwner: TComponent);
begin
  inherited Create(AOwner);

  SetColorStart(clSkyBlue);
  SetColorEnd(clHighlight);
  ParentBackground := True;
  ParentColor := False;
  OnPaint := Paint;
end;

destructor TMyActionMenu.Destroy;
begin
  inherited;
end;

procedure TMyActionMenu.SetColorStart(AValue: TColor);
begin
  if FColorStart <> AValue then
  begin
    FColorStart := AValue;
    Invalidate;
  end;
end;

procedure TMyActionMenu.SetColorEnd(AValue: TColor);
begin
  if FColorEnd <> AValue then
  begin
    FColorEnd := AValue;
    Invalidate;
  end;
end;

procedure TMyActionMenu.Paint(Sender: TObject);
var
  Rect: TRect;
begin
  Rect := GetClientRect;

  GradientFillCanvas(TMyActionMenu(Sender).Canvas, FColorStart,
    FColorEnd, Rect, gdVertical);
end;

procedure Register;
begin
  RegisterComponents('Standard', [TMyActionMenu]);
end;

end.

我不确定应该从哪里开始改变,期待看到您的评论和答案。

更新

举例来说,将TActionMainMenuBar放置在渐变面板内,ParentBackground设置为True,看起来更加适合。因此,我应该考虑使用SetSubComponent并将我的TMyActionMenu放入类似的面板容器中,然后以这种方式在面板上绘制渐变。

与此同时,我将保持这个问题开放,等待进一步的评论和建议。


什么Delphi版本?我怀疑如果你使用的是XE2,最好通过VCL样式钩子来处理。 - Warren P
1个回答

2

我还没有完全研究这个问题,但无论如何我会接受你的答案,谢谢。 - user1175743

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接