在1.000表单中将多个标签设置为透明?

时间:2011-05-25 15:02:15

标签: delphi delphi-2010 delphi-7 delphi-xe

我使用Devexpress为我的软件设置了皮肤,我发现标签不透明,导致它们具有灰色背景。

只有无穷无尽的形式,所以我想知道是否有办法自动完成这项任务(将标签设置为透明)。

我之前做了类似的事情,表单上的Devexpress控件有LookAndFeel.NativeStyle = True,我在所有dfm表单上使用Grep Search将其替换为False。但是在标签的情况下,透明属性不存在。

谢谢。

6 个答案:

答案 0 :(得分:14)

全局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属性,那么省略这个检查:它实际上不会那么慢。

答案 1 :(得分:9)

对于此类任务,GExperts包含设置组件属性工具:

  

此工具在后台等待   直到你编译一个项目。然后呢   扫描当前项目的表单   检查某些组件   属性和改变那些   属性到定义的值。这个   工具可用于停用数据集   或之前的数据库连接   编译你的应用程序,但它可以   用于任何类似的情况   好。要激活扫描,   启用旁边的复选框   GExperts配置专家   屏幕。

它也可用于设置尚未包含在DFM中的属性,并且只需要在GExpert配置中添加一个附加条目,然后重新编译。

我刚测试过它,它按预期工作。

答案 2 :(得分:5)

在设计时,您只需解析所有.dfm,然后添加

  Transparent = True

在任何

之后排队
  object MyLabel : TLabel

线。

在运行时,您可以覆盖TCustomForm.DoCreateTCustomFrame.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;

答案 3 :(得分:5)

相关提示(我总是忘记使用这个方便的功能):

  1. 按照您希望的方式配置标签;
  2. 在表单上选择;
  3. 转到Component/Create component template;
  4. 然后,您可以为模板命名:
  5. enter image description here

    从那时起,模板在工具调色板中显示为新的组件类型,并带有您喜欢的设置。

    (是的,我知道这不会改变当前的标签)

答案 4 :(得分:1)

您可以将BackColor属性设置为Color.Transparent

答案 5 :(得分:1)

以下内容应该有效:只有当值不是默认值时,透明属性才会出现在DFM文件中。因此,您可以在Grep-Search中将“Transparent = TRUE”插入“= TLabel”之后的下一行。我自己没试过,但很容易尝试......