【问题标题】:delphi component order and layer optionsdelphi 组件顺序和层选项
【发布时间】:2015-01-23 07:51:30
【问题描述】:

我对 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;

【问题讨论】:

    标签: delphi components layer


    【解决方案1】:

    TGraphicControl 没有窗口句柄,只是在其父 DC 上绘制。
    您不能将TGraphicContol 放在TWinContol 后代的前面(例如TPanelTButtonTEdit 等)。

    要么使用TWinControl descendant as shown in your previous question,它可以出现在其他孩子TWinControl 面前,要么重新设计你的用户界面以消除另一个TWinControl 重叠或重叠的情况自定义图形控件的顶部。

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

    【讨论】:

      猜你喜欢
      • 2010-11-23
      • 2012-12-24
      • 2017-10-12
      • 1970-01-01
      • 2018-06-27
      • 1970-01-01
      • 1970-01-01
      • 2014-12-04
      • 1970-01-01
      相关资源
      最近更新 更多