Recent

Author Topic: Transparent form - how?  (Read 5695 times)

Davo

  • Full Member
  • ***
  • Posts: 134
Transparent form - how?
« on: August 21, 2011, 10:13:20 pm »
How can I create a transparent form.  For example one that contains a shape so that when the form is activated only its frame and the shape are displayed against the background of any pre-existing windows?

typo

  • Hero Member
  • *****
  • Posts: 3051

Davo

  • Full Member
  • ***
  • Posts: 134
Re: Transparent form - how?
« Reply #2 on: August 21, 2011, 11:48:33 pm »
Thanks Typo for the pointer.  Have read what's there and now I'm totally confused having only limited skills.  There just doesn't seem to be a simple easy way to implement this task which I would have thought was a fairly basic requirement.  But I'll plough through the forum threads again to see if some light will dawn.

User137

  • Hero Member
  • *****
  • Posts: 1791
    • Nxpascal home
Re: Transparent form - how?
« Reply #3 on: August 22, 2011, 07:10:28 am »
This is what i used with Delphi sometime. It is my own experiment with polygons but it worked back then... It's basically making a region from horizontal line segments. (you may need Windows in uses clause, as this is not cross platform):
Code: [Select]
// Use this with SetWindowRgn to reshape your components and forms!
function CreateBitmapRgn(c: TCanvas; Width,Height: Word): HRGN;
var p: array[0..30000] of TPoint;
    vertex: array[0..10000] of integer;
    trc,i,j,pm: integer; drawing: boolean;
begin
     pm:=-1; drawing:=false; trc:=c.Pixels[0,0];
     for j:=0 to height-1 do
       for i:=0 to width-1 do
         if (c.Pixels[i,j]=trc) or (i>=width-1) then begin
           if drawing then begin
             drawing:=false;
             p[pm*3+1]:=point(i,j); p[pm*3+2]:=point(i,j+1);
           end;
         end else if not drawing then begin
           drawing:=true;
           inc(pm); vertex[pm]:=3; p[pm*3]:=point(i,j);
         end;
     result:=CreatePolyPolygonRgn(p,vertex,pm+1,WINDING);
end;

You can call with like:
Code: [Select]
var rgn: HRGN; bmp: TBitmap;
begin
  bmp:=TBitmap.Create;
  bmp.LoadFromFile('WindowShape.bmp');
  CreateBitmapRgn(bmp.canvas, bmp.width, bmp.height);
  SetWindowRgn(form1.handle, rgn, true);
  bmp.Free;
end;

Beauty of the function is that you can use it to also reshape any other component on form too. A circle panel for instance.
« Last Edit: August 22, 2011, 07:14:24 am by User137 »

 

TinyPortal © 2005-2018