我使用了DevExpress来美化我的软件,但是发现标签不透明,导致它们有灰色背景。
因为表单很多,所以我想知道是否有自动将标签设置为透明的方法。
我之前做过类似的事情,在表单上的DevExpress控件中设置了LookAndFeel.NativeStyle = True属性,然后我使用Grep搜索来将所有dfm表单中的该属性替换为False。然而,在标签的情况下,透明属性不存在。
谢谢。
我使用了DevExpress来美化我的软件,但是发现标签不透明,导致它们有灰色背景。
因为表单很多,所以我想知道是否有自动将标签设置为透明的方法。
我之前做过类似的事情,在表单上的DevExpress控件中设置了LookAndFeel.NativeStyle = True属性,然后我使用Grep搜索来将所有dfm表单中的该属性替换为False。然而,在标签的情况下,透明属性不存在。
谢谢。
Screen
变量跟踪所有表单:procedure MakeLabelsTransparent(AParent: TWinControl);
var
I: Integer;
begin
with AParent do
for I := 0 to ControlCount - 1 do
if Controls[I] is TLabel then
TLabel(Controls[I]).Transparent := True
else if Controls[I] is TWinControl then
MakeLabelsTransparent(TWinControl(Controls[I]));
end;
procedure TMainForm.ActiveFormChange(Sender: TObject);
begin
with Screen do
if (ActiveCustomForm <> nil) and (ActiveCustomForm.Tag = 0) then
begin
MakeLabelsTransparent(ActiveCustomForm);
ActiveCustomForm.Tag := 1;
end;
end;
procedure TMainForm.FormCreate(Sender: TObject);
begin
Screen.OnActiveFormChange := ActiveFormChange;
end;
如果你必须要对特定表单使用Tag属性,那么省略这个检查也不会使程序变得太慢。
在设计时,您可以解析所有的 .dfm 文件,然后添加
Transparent = True
在任何一行后面添加
object MyLabel : TLabel
行。
在运行时,您可以重写 TCustomForm.DoCreate
和 TCustomFrame.Create
方法,如下所示:
type
THookedForm = class(TCustomForm)
procedure HookedDoCreate;
end;
THookedFrame = class(TCustomFrame)
constructor Create(AOwner: TComponent); override;
end;
var
PatchForm, OriginalForm: TPatchEvent;
PatchPositionForm: PPatchEvent = nil;
PatchFrame, OriginalFrame: TPatchEvent;
PatchPositionFrame: PPatchEvent = nil;
procedure PatchCreate;
var ov: cardinal;
begin
// hook TForm:
PatchPositionForm := PPatchEvent(@THookedForm.DoCreate);
OriginalForm := PatchPositionForm^;
PatchForm.Jump := $E9; // Jmp opcode
PatchForm.Offset := PtrInt(@THookedForm.HookedDoCreate)-PtrInt(PatchPositionForm)-5;
if not VirtualProtect(PatchPositionForm, 5, PAGE_EXECUTE_READWRITE, @ov) then
RaiseLastOSError;
PatchPositionForm^ := PatchForm; // enable Hook
// hook TFrame:
PatchPositionFrame := PPatchEvent(@TCustomFrame.Create);
OriginalFrame := PatchPositionFrame^;
PatchFrame.Jump := $E9; // Jmp opcode
PatchFrame.Offset := PtrInt(@THookedFrame.Create)-PtrInt(PatchPositionFrame)-5;
if not VirtualProtect(PatchPositionFrame, 5, PAGE_EXECUTE_READWRITE, @ov) then
RaiseLastOSError;
PatchPositionFrame^ := PatchFrame; // enable Hook
end;
{ THookedForm }
procedure THookedForm.HookedDoCreate;
var i: integer;
begin
// enumerate all labels, then set Transparent := true
for i := 0 to Components.Count-1 do
if Components[i] is TLabel then
TLabel(Components[i]).Transparent := true;
DoCreate; // call initial code
end;
{ THookedFrame }
constructor THookedFrame.Create(AOwner: TComponent);
var i: integer;
begin
// enumerate all labels, then set Transparent := true
for i := 0 to Components.Count-1 do
if Components[i] is TLabel then
TLabel(Components[i]).Transparent := true;
inherited Create(AOwner); // call normal constructor
end;
....
initialization
PatchCreate;
一条相关的提示(我总是忘记使用这个方便的功能):
组件/创建组件模板
;从此以后,该模板将作为新的组件类型出现在您的工具面板中,并带有您喜欢的设置。
(是的,我知道这不会改变当前标签)
您可以将BackColor
属性设置为Color.Transparent
。
Label.Transparent:=True
,但真正的问题在于如何自动化这个变化。 - David Heffernan