delphi组件顺序和图层选项

时间:2014-11-25 10:44:20

标签: delphi components layer

我是delphi编程的新手:( 我试图制作一个带有透明背景图层和带圆圈形状的顶层的自定义组件。但是,下面的代码在添加到表单时工作正常。 例外情况是,有另一个组件与自定义组件重叠或在其上方,它位于下方并且不显示。 我在下面的表单上尝试了

 BadgeTest1.BringToFront;
 BadgeTest1.ComponentIndex:=2;
 IndexVal:= BadgeTest1.ComponentIndex;

然而,仍然没有工作。无论如何,定制组件是否显示在其他组件之上?只有圆形部分? 此外,我一直试图在自定义组件的中心(水平和垂直)放置一个标题,我尝试过TextOut()程序。如果有更好的选择,请告诉我? 下面是我的名为BadgeTest的自定义组件的代码。 请帮忙, 非常感谢你!

type
 TBadgeTest=class(TGraphicControl)

private
  FCaption:TCaption;
  FColor:TColor;
  FLayers:TLayerCollection;
  FHeight:Integer;
  FWidth:Integer;

protected
 procedure Paint; override;  
  procedure SetBkgLayer;      
  procedure SetSecondLayer;       
public
  constructor Create(AOwner: TComponent); override;
  destructor Destroy; override;
published
  property Caption:TCaption read FCaption write FCaption; 
end;

procedure Register;

implementation

  procedure Register;
begin
  RegisterComponents('Sample', [TBadgeTest]);
end;

constructor TBadgeTest.Create(AOwner: TComponent);
var
  ACanvas:TcxCanvas;
   begin
   inherited;
   FHeight:=20;
  Self.Height:=FHeight;
  Constraints.MaxHeight:=20;
  Constraints.MinHeight:=20;
  FHeight:=20;
  Self.Width:=FWidth;
  Constraints.MaxWidth:=20;
  Constraints.MinWidth:=20;
end;

destructor TBadgeTest.Destroy;
begin
  inherited;
end;

 procedure TBadgeTest.SetBkgLayer;   
 var
  Bitmap:TBitmap32;
  Layer: TCustomLayer;
 begin
  FLayers := TLayerCollection.Create(Self);
  Layer := FLayers.Add(TBitmapLayer);
  Layer.Index:=0;
  Bitmap:= TBitmap32.Create;
  Bitmap.DrawMode:=dmOpaque;
  Bitmap.SetSize(Width, Height);
  Bitmap.clear($00000000);

  Bitmap.Canvas.Pen.Width:=0;
  Bitmap.Canvas.Brush.Color:=$00107EFF;
  Bitmap.Canvas.Brush.Style:=bsClear;
  Bitmap.Canvas.Ellipse(Rect(0,0,20,20));
end;

  procedure TBadgeTest.SetSecondLayer;
var
  Bitmap:TBitmap32;
  Layer: TCustomLayer;
 begin
  Layer := FLayers.Add(TBitmapLayer);
  Layer.Index:=1;
  Layer.LayerOptions:= LOB_VISIBLE;
  Bitmap:=TBitmap32.Create;
  Bitmap.DrawMode:=dmCustom;
  Bitmap.SetSize(Width, Height);
  Bitmap.clear($00000000);

  Bitmap.Canvas.Pen.Width:=0;
  Bitmap.Canvas.Brush.Color:=$00107EFF;   //FF7E10
  Bitmap.Canvas.Brush.Style:=bsSolid;
  Bitmap.Canvas.Ellipse(Rect(0,0,Self.Width,Self.Height));

  Layer.BringToFront;
  Layer.BringToFront;
  //Layer.Scaled:=true;
  //  Layer.Bitmap:=Bitmap;
  end;

    procedure TBadgeTest.Paint;
var
    R:TRect;
    borderColor : Integer;
    fillCircle : Integer;  
    fontColor : Integer;  
    fontSize : Integer;   
    Bitmap:TBitmapImage;
  const
  _FF7E10_COLOR:Integer = $00107EFF; //#FF7E10           

 begin
  inherited;
  borderColor:=_FF7E10_COLOR;
  fillCircle:=_FF7E10_COLOR;

  Canvas.Pen.Create;
  Canvas.Pen.Style:=psClear;
  Canvas.Pen.Color:=borderColor;
  Canvas.Pen.Width:=0;

  SetBkgLayer;
  SetSecondLayer;

  Canvas.Brush.Create;
  Canvas.Brush.Style:= bsClear;
  Canvas.Brush.Color:=fillCircle;
  Canvas.Ellipse(0,0,Self.Width,Self.Height);
  Canvas.Font.Color:=clWhite;
  Canvas.Font.Name:='Arial';
  Canvas.Font.Size:=8;
  Canvas.Font.Quality := fqNonAntialiased;
  Canvas.Font.Style := [fsBold];

  R.Create(0, 0, Self.Width, Self.Height);
  //DrawText(Canvas.Handle, PChar(FCaption), -1, R, vaCenter);
 // Canvas.DrawText(FCaption, R, taCenter, vaCenter, False, False);
  Canvas.TextOut(5, 5, FCaption);
  //SetTextAlign(Canvas.Handle, ta_center);
  //DrawText(Canvas.Handle, PChar(FCaption),
  //R.Create(1, 10, 2, 26);
   //  Self.Width := Canvas.TextWidth(FCaption) + 30;
end;

1 个答案:

答案 0 :(得分:5)

TGraphicControl没有窗口句柄,只是在其父DC上绘制 您无法在TGraphicContol后代(例如TPanelTButtonTEdit等)前面加TWinContol

使用<{3}} 可以将其带到其他子TWinControl之前,或者重新设计您的用户界面以消除另一个{{1}的情况与自定义图形控件重叠或顶部重叠。

P.S:视觉控制被称为“控制”,而不是“组件”(非视觉)