我试图通过创建一个继承自TActionMainMenuBar的组件来扩展它。
其中一件事是允许在菜单栏上绘制渐变。我已经成功实现了这一点,但在设置TActionManager为XPStyle时会出现菜单项的问题。
请看下面这些不同TActionManager样式的图片:
平台默认(鼠标悬停和按下):
当TActionManager设置为“平台默认”时,背景会正确显示,并且可以清晰地看到渐变效果。
然而,当TActionManager设置为XPStyle时,某些菜单项的渐变将无法正常绘制,只需要查看第一项即可:
XP样式(鼠标悬停和按下):
如您所见,这样的效果并不理想。
我已经在Paint中编辑了一些图像,大致说明了我希望效果如何:
鼠标悬停和选定
我认为这将允许创建一些有趣的菜单样式。
以下是我到目前为止的代码:
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放入类似的面板容器中,然后以这种方式在面板上绘制渐变。
与此同时,我将保持这个问题开放,等待进一步的评论和建议。