2014-11-25 38 views
1

我對delphi編程非常陌生:( 我想用一個透明的背景層和一個帶有圓形的頂層做一個自定義的組件,但是,下面的代碼工作正常添加到表單上。 Exception,它是有另一種成分的事實與或對定製組件的頂部重疊,它下面的謊言和不顯示。 我已經試過以下表單上delphi組件順序和圖層選項

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

然而,仍然doens't工作。有無論如何定製組件顯示在其他組件頂部?只有圓形部分? 另外,我'我一直試圖在定製組件的中心(水平和垂直)放置一個標題,我嘗試過使用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; 

回答

5

A TGraphicControl沒有窗口句柄,只是簡單地畫在它的父DC上。
您不能在TWinContol後代(例如TPanel,TButton, TEdit等)之前攜帶TGraphicContol

要麼使用TWinControl descendant as shown in your previous question可以在其他孩子TWinControl面前提起,或者重新設計消除的情況下另一個TWinControl用或您的自定義圖形控件的頂部重疊這樣你的UI。

P.S:視覺控件被稱爲「控件」,而不是「組件」(它是非可視的)