Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I put a semi transparent layer on my form

I have read some questions about this in the last week or so, on stackoverflow.

My requirement is more or less the same.

I need to put a semi-transparent layer on top my form, but this form may have several other components: Lists, Edits, Labels, Images ,etc

I need this semi-transparent layer to be on top of all that.

The idea is to fade areas of the form that the use those not, or cannot access in that moment.

I use Delphi 2007.

Thanks

like image 407
Jlouro Avatar asked Aug 08 '12 14:08

Jlouro


People also ask

How to make a semi-transparent background image?

Put a white rectangle over the background image and make that semi-transparent. Put these both on the same layer and lock it and draw on a new layer above. Set the transparency of the image by selecting it then use the transparency tool, use uniform and the desired transparency level. Here ya go. I hadn't look at the file.

How do you make a transparent background in Photoshop?

Put a white rectangle over the background image and make that semi-transparent. Put these both on the same layer and lock it and draw on a new layer above. Set the transparency of the image by selecting it then use the transparency tool, use uniform and the desired transparency level.

How to make a small part of a layer transparent?

There may be times though when you want only a small part of one layer to be transparent or semi-transparent. The trick, my friends, is that you need to firstly select the part of the layer that you want to adjust the transparency of and then make your adjustment.

How do I make a transparent bitmap?

In the middle payer add a rectangle covering the entire bitmap. Color it white, make that layer have transparency. Lock the bitmap and middle layer. Trace away. Put a white rectangle over the background image and make that semi-transparent. Put these both on the same layer and lock it and draw on a new layer above.


1 Answers

Create a new VCL project. Add a few sample buttons and other controls to the main form. Create a new form, set AlphaBlend to true and AlphaBlendValue to 128. Perhaps Color = clSkyBlue will suffice? Then add the following procedure to your main form:

procedure TForm1.UpdateShadow;
var
  pnt: TPoint;
  rgn, rgnCtrl: HRGN;
  i: Integer;
begin
  if not Assigned(Form2) then Exit;
  Form2.Show;
  pnt := ClientToScreen(Point(0, 0));
  Form2.SetBounds(pnt.X, pnt.Y, ClientWidth, ClientHeight);
  rgn := CreateRectRgn(0, 0, Form2.Width, Form2.Height);
  for i := 0 to ControlCount - 1 do
    if Controls[i].Tag = 1 then
    begin
      if not (Controls[i] is TWinControl) then Continue;
      with Controls[i] do
        rgnCtrl := CreateRectRgn(Left, Top, Left+Width, Top+Height);
      CombineRgn(rgn, rgn, rgnCtrl, RGN_DIFF);
      DeleteObject(rgnCtrl);
    end;
    SetWindowRgn(Form2.Handle, rgn, true);
    DeleteObject(rgn);
end;

and call this on resize,

procedure TForm1.FormResize(Sender: TObject);
begin
  UpdateShadow;
end;

and form move:

procedure TForm1.WMMove(var Message: TWMMove);
begin
  inherited;
  UpdateShadow;
end;

Finally, set the Tag to 1 on the controls (on your main form) that are to be accessible.

Sample screenshot
(source: rejbrand.se)

Hint: You might also wish to set the Cursor of the 'shadow form' to crNo.

like image 56
Andreas Rejbrand Avatar answered Nov 15 '22 22:11

Andreas Rejbrand