How do I put a semi transparent layer on my form

前端 未结 2 1757
猫巷女王i
猫巷女王i 2020-12-08 03:38

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 lay

2条回答
  •  长情又很酷
    2020-12-08 04:06

    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.


    (source: rejbrand.se)

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

提交回复
热议问题