复合部件的选择框未正确绘制

时间:2015-11-20 10:04:07

标签: delphi controls vcl

我有一个复合组件,其中包含TEditTButton(是的,我知道TButtonedEdit),它继承自TCustomControl。编辑和按钮在其构造函数中创建并放置在自身上。

在设计时,选择框没有正确绘制 - 我的猜测是编辑和按钮隐藏了它,因为它是为自定义控件绘制的,然后由它们透支。

这里比较:

enter image description here

我也看到过其他第三方组件(比如TcxGrid也只绘制选择指标的外部部分)

问题:我该如何改变?

最简单的复制案例:

unit SearchEdit;

interface

uses
  Classes, Controls, StdCtrls;

type
  TSearchEdit = class(TCustomControl)
  private
    fEdit: TEdit;
  public
    constructor Create(AOwner: TComponent); override;
  end;

procedure Register;

implementation

procedure Register;
begin
  RegisterComponents('Custom', [TSearchEdit]);
end;

{ TSearchEdit }

constructor TSearchEdit.Create(AOwner: TComponent);
begin
  inherited;
  fEdit := TEdit.Create(Self);
  fEdit.Parent := Self;
  fEdit.Align := alClient;
end;

end.

1 个答案:

答案 0 :(得分:3)

正如我在评论中所说,我能想到的最简单的事情就是在父母中绘制控件并在设计时将它们“隐藏”起来。您可以通过在每个子控件上调用SetDesignVisible(False)来执行此操作。然后使用PaintTo在父级上进行绘画。

使用您的示例我们得到:

type
  TSearchEdit = class(TCustomControl)
  ...
  protected
    procedure Paint; override;
  ...
  end;

constructor TSearchEdit.Create(AOwner: TComponent);
begin
  inherited;
  fEdit := TEdit.Create(Self);
  fEdit.Parent := Self;
  fEdit.Align := alClient;
  fEdit.SetDesignVisible(False);
end;

procedure TSearchEdit.Paint;
begin
  Inherited;
  if (csDesigning in ComponentState) then
    fEdit.PaintTo(Self.Canvas, FEdit.Left, FEdit.Top);
end;