【发布时间】:2012-08-14 14:38:42
【问题描述】:
我正在尝试通过创建我自己的从该组件派生的组件来扩展 TActionMainMenuBar。
我正在尝试做的一件事是允许沿菜单栏绘制渐变。我已经设法做到了,但是当分配的 TActionManager 设置为 XPStyle 时,菜单项会出现问题。
查看这些带有不同 TActionManager 样式集的图像:
平台默认(鼠标悬停并按下):
当 TActionManager 设置为 Platform Default 时,背景正确显示,您可以看到渐变很好地显示出来。
但是当TActionManager设置为XPStyle时,有些item的渐变没有正确绘制,只看第一个item:
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.
我不确定从哪里开始或改变什么,所以我期待看到您的 cmets 和答案。
更新
例如,将 TActionMainMenuBar 放置在渐变面板中,将 ParentBackground 设置为 True 更合适。所以我应该考虑使用 SetSubComponent 并将我的 TMyActionMenu 放入一个类似的面板容器中,然后以这种方式将渐变绘制到面板上。
与此同时,我将把这个问题留待进一步的 cmets 和建议。
【问题讨论】:
-
什么delphi版本?我怀疑如果你在 XE2 上,你最好通过 VCL 风格的钩子来解决这个问题。