1

我创建了一个例程来使 Delphi 可视控件的角变圆。

现在我要做的是确保每个视觉对象都像TMemo,TEdit并且TPanel是圆的,而不必在创建表单时为每个人调用函数。

如何从我的代码(表单单元)中为这些类中的每一个扩展 create 方法,以便它们保留类的名称和其他单元上的正常行为?

procedure RoundCornersOf(Control: TWinControl) ;
var
   R: TRect;
   Rgn: HRGN;
begin
   with Control do
   begin
     R := ClientRect;
     rgn := CreateRoundRectRgn(R.Left, R.Top, R.Right, R.Bottom, 20, 20) ;
     Perform(EM_GETRECT, 0, lParam(@r)) ;
     InflateRect(r, - 4, - 4) ;
     Perform(EM_SETRECTNP, 0, lParam(@r)) ;
     SetWindowRgn(Handle, rgn, True) ;
     Invalidate;
   end;
end;
4

1 回答 1

2

存在在运行时修改类的构造或技巧,例如,请参阅在 delphi 中替换组件类在运行时按需更改组件类。但是,据我了解,您必须声明所有出现的控件类型的单独类型。

Controls另一种方法是在创建表单后使用和ControlCount属性遍历所有控件:

  public
    procedure AfterConstruction; override;
  end;

procedure ModifyControls(Window: TWinControl);
var
  I: Integer;
begin
  for I := 0 to Window.ControlCount - 1 do
    if Window.Controls[I] is TWinControl then
    begin
      ModifyControls(TWinControl(Window.Controls[I]));
      RoundCorners(TWinControl(Window.Controls[I]));
    end;
end;

procedure TForm1.AfterConstruction;
begin
  inherited AfterConstruction;
  ModifyControls(Self);
end;

但要小心控制娱乐,这比你想象的要多。例如,更改 Edit 的BorderStyle属性会导致重新创建 Edit,这会撤消您的修改。在这些情况下重做修改,前提是您可以跟踪它们。

于 2012-06-29T23:04:56.397 回答