在 Delphi XE2 中,我成功地为我创建的自定义组件类创建了 VCL 样式的覆盖。但我发现,这些样式似乎在控件的运行时创建期间不适用。
具体来说,我已经扩展了 TPanel 并正在用动态创建的面板填充 TScrollBox,将每个面板设置为特定的颜色。我还使用 API 在创建过程中暂停对 ScrollBox 的重绘。
加载完成后,我将 TPanel 设置为 clWindow(视觉上),但是当我将 TPanel 拖放到另一个位置/控制我在代码中设置的颜色时“开始”。所以有些东西不允许/允许这些颜色应用......或者面板根本不刷新。
因此,我不太确定是否需要在动态组件创建时使用 VCL 样式覆盖调用“刷新”,或者是否在 TScrollBox 上暂停重绘会干扰创建时面板上未更新的颜色。因为它是挂起的 ScrollBox 的子级。
我想知道是否只有一个简单且已知的“陷阱”我忽略了我正在尝试做的事情。
我已经将项目简化为基本要素,但仍然存在问题。
这是 TPanel 添加标签的简单扩展。
unit InfluencePanel;
interface
uses
System.SysUtils, System.Classes, Vcl.Forms, Vcl.Controls, Vcl.StdCtrls, Vcl.ExtCtrls,
Vcl.Graphics;
type
TInfluencePanel = class(TPanel)
private
{ Private declarations }
oCaptionLabel : TLabel;
FLabelCaption : String;
procedure SetLabelCaption(sCaption : String);
protected
{ Protected declarations }
public
{ Public declarations }
constructor Create(AOwner: TComponent); override;
property LabelCaption : string read FLabelCaption write SetLabelCaption;
published
{ Published declarations }
end;
procedure Register;
implementation
constructor TInfluencePanel.Create(AOwner: TComponent);
begin
inherited;
oCaptionLabel := TLabel.Create(Self);
with oCaptionLabel do
begin
Caption := 'Caption';
Top := 0;
Left := 0;
Align := alTop;
WordWrap := True;
Parent := Self;
end;
end;
procedure TInfluencePanel.SetLabelCaption(sCaption: string);
begin
FLabelCaption := sCaption;
if oCaptionLabel <> nil then oCaptionLabel.Caption := FLabelCaption;
end;
procedure Register;
begin
RegisterComponents('Influence Elements', [TInfluencePanel]);
end;
end.
这是应该显示问题的简单项目。按钮 1 将 TInfluencePanel 的五个实例加载到 ScrollBox1 中。它们以默认的窗口颜色和无样式而不是代码中的颜色出现。Button2 将控件移动到 ScrollBox2,它们以编码颜色出现。这已经删除了所有暂停的重绘,等等。
unit Unit1;
interface
uses
Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants,
System.Classes, Vcl.Graphics, Vcl.Controls, Vcl.Forms, Vcl.Dialogs,
Vcl.StdCtrls, Vcl.ExtCtrls, Vcl.Themes, InfluencePanel;
type
TInfluencePanelStyleHookColor = class(TEditStyleHook)
private
procedure UpdateColors;
protected
procedure WndProc(var Message: TMessage); override;
public
constructor Create(AControl: TWinControl); override;
end;
type
TForm1 = class(TForm)
ScrollBox1: TScrollBox;
ScrollBox2: TScrollBox;
Button1: TButton;
Button2: TButton;
procedure Button1Click(Sender: TObject);
procedure Button2Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
uses
Vcl.Styles;
type
TWinControlH= class(TWinControl);
constructor TInfluencePanelStyleHookColor.Create(AControl: TWinControl);
begin
inherited;
UpdateColors;
end;
procedure TInfluencePanelStyleHookColor.UpdateColors;
var
LStyle: TCustomStyleServices;
begin
if Control.Enabled then
begin
Brush.Color := TWinControlH(Control).Color;
FontColor := TWinControlH(Control).Font.Color;
end
else
begin
LStyle := StyleServices;
Brush.Color := LStyle.GetStyleColor(scEditDisabled);
FontColor := LStyle.GetStyleFontColor(sfEditBoxTextDisabled);
end;
end;
procedure TInfluencePanelStyleHookColor.WndProc(var Message: TMessage);
begin
case Message.Msg of
CN_CTLCOLORMSGBOX..CN_CTLCOLORSTATIC:
begin
UpdateColors;
SetTextColor(Message.WParam, ColorToRGB(FontColor));
SetBkColor(Message.WParam, ColorToRGB(Brush.Color));
Message.Result := LRESULT(Brush.Handle);
Handled := True;
end;
CM_ENABLEDCHANGED:
begin
UpdateColors;
Handled := False;
end
else
inherited WndProc(Message);
end;
end;
procedure TForm1.Button1Click(Sender: TObject);
var
iPanel, iLastPosition : Integer;
oPanel : TInfluencePanel;
begin
iLastPosition := 0;
for iPanel := 1 to 5 do
begin
oPanel := TInfluencePanel.Create(ScrollBox1);
with oPanel do
begin
Align := alLeft;
Left := iLastPosition;
Width := 90;
Parent := ScrollBox1;
Color := RGB(200,100,iPanel*10);
LabelCaption := 'My Panel ' + IntToStr(iPanel);
Margins.Right := 5;
AlignWithMargins := True;
end;
iLastPosition := iLastPosition + 90;
end;
end;
procedure TForm1.Button2Click(Sender: TObject);
var
iPanel : Integer;
begin
for iPanel := ScrollBox1.ControlCount - 1 downto 0 do
begin
if ScrollBox1.Controls[iPanel].ClassType = TInfluencePanel then
TInfluencePanel(ScrollBox1.Controls[iPanel]).Parent := ScrollBox2;
end;
end;
initialization
TStyleManager.Engine.RegisterStyleHook(TInfluencePanel,TInfluencePanelStyleHookColor);
end.