Blaatz0r
Blaatz0r

Reputation: 1205

Transparency in component component overlay

First and foremost if more information is required please just ask i'm willing to add more information. (Won't be able to answer earlier until tomorrow 09:00 GMT+1)

Working on an application in delphi with rounded comboboxes and rounded buttons (TCustomControls) The problems that arise are mainly problems with components that are in some way layered over each other as can be seen in image one (below)

The background of the form still shines through around the corners while the components are drawn on top of each other.

For every custom control I am painting the component myself. But I can't seem to get a grasp on how to get this to work as it should.

I've tried the following

 - Params.exStyle := Params.exStyle + WS_EX_TRANSPARENT; 
 - ScanLine and set the pixels equal to the background (which doesn't work as one
   would suspect)
 - WSEraseBackground procedure empty

But nothing seems to fix my problem (WS_EX_TRANSPARENT did but when clicking on a component it seems to flip the Z-order)

I override the paint event and just draw rounded rectangles (shouldn't be a biggy imo)

Current Issue

How I would like to see it work

procedure TRoundedComboBox.Paint;
var
  Rect : TRect;

  procedure DrawFirst();
  begin
    {first}
    Canvas.Pen.Color := FColorArray[0];
    Canvas.Brush.Color := FColorArray[0];
    Canvas.RoundRect(0,
                     0,
                     width,
                     FDefaultComboBoxHeight,
                     20,
                     20);
  end;


  procedure DrawFirstInner();
  begin
    {first inner}
    Canvas.Pen.Color := FColorArray[1];
    Canvas.Brush.Color := FColorArray[1];
    Canvas.RoundRect(0,
                     1,
                     width,
                     FDefaultComboBoxHeight,
                     20,
                     20);
  end;


  procedure DrawSecondInner();
  begin
    {second inner}
    Canvas.Pen.Color := FColorArray[2];
    Canvas.Brush.Color := FColorArray[2] ;
    Canvas.RoundRect(0,
                     round(FDefaultComboBoxHeight /2),
                     width,
                     FDefaultComboBoxHeight,
                     20,
                     20);
  end;


  procedure DrawText();
  begin
    {Text}
    Canvas.Font := FFont;
    Canvas.Font.Color := FColorArray[3];
    Canvas.Brush.Style := bsClear;
    FTextRect := TRect.Create(4, 0, width -20, FDefaultComboBoxHeight);
    Canvas.TextRect(FTextRect,
                    12,
                    round(FTextRect.Height /2) - round(Canvas.TextExtent(FText).Height /2),
                    FText);
  end;


  procedure DrawTriangle();
  begin
    {Triangle}
    Canvas.MoveTo(FPoints[0].x, FPoints[0].y);
    Canvas.Pen.Color := FColorArray[4];
    Canvas.Brush.Color := FColorArray[4];
    Canvas.Polygon(FPoints);
  end;

begin
  //inherited;

  FListBox.Invalidate;
  FListBox.Visible := FEnabledBtnDown;

  if (FEnabledBtnDown) then
  begin
    FlistBOx.SetFocus;
  end;


  Height := IfThen (FEnabledBtnDown, FMaxmimumComboBoxHeight, FDefaultComboBoxHeight);

  DrawFirst;

  DrawFirstInner;

  DrawSecondInner;

  DrawTriangle;

  DrawText;
end;

Upvotes: 2

Views: 697

Answers (1)

Sebastian Z
Sebastian Z

Reputation: 4730

You could derive from TCustomTransparentControl (unit Controls.pas). If that is not an option, take a look at how TCustomTransparentControl works.

Upvotes: 1

Related Questions