Recent

Author Topic: how do I make the form transparent  (Read 23572 times)

jw

  • Full Member
  • ***
  • Posts: 120
how do I make the form transparent
« on: January 26, 2010, 11:53:01 pm »
how do I make the form transparent?

I've got experience with vb6/vb.net and programmed in turbo pascal 7 a long time ago.

To get a transparent form in vb6 I had to make an api call vb.net was even easier.  But free pascal which is not supposed to be platfom specific wouldn't want me to use a windows specific api call besides I don't know how to setup an API call in free pascal yet anyway.  So how do I make the form transparent?

Marc

  • Administrator
  • Hero Member
  • *
  • Posts: 2496
Re: how do I make the form transparent
« Reply #1 on: January 27, 2010, 12:25:17 am »
This is not (yet) cross platform supported.
On windows you can make the same api call as you did in vb6. Add uses windows to your  uses clause
//--
{$I stdsig.inc}
//-I still can't read someones mind
//-Bugs reported here will be forgotten. Use the bug tracker

skalogryz

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 2280
    • havefunsoft.com
Re: how do I make the form transparent
« Reply #2 on: January 27, 2010, 09:24:23 am »
So how do I make the form transparent?
if you use svn version of Lazarus or tomorrows daily snapshot (after 27-jan-2001).
You can adjust form's transparency by changing AlphaBlendValue property.
Patron Cocoa Widgetset development https://www.patreon.com/skalogryz

LazaruX

  • Hero Member
  • *****
  • Posts: 597
  • Lazarus original cheetah.The cheetah doesn't cheat
Re: how do I make the form transparent
« Reply #3 on: January 27, 2010, 12:00:15 pm »
Regarding the windows way, search in the forum there are already example on how to do that on windows.

jw

  • Full Member
  • ***
  • Posts: 120
Re: how do I make the form transparent
« Reply #4 on: January 27, 2010, 07:22:34 pm »
here's workining code for setting the forms background transparent in windows thank to all for the help


unit Unit1;

{$mode objfpc}{$H+}

interface

uses
  Classes, SysUtils, FileUtil, LResources, Forms, Controls, Graphics, Dialogs,
  StdCtrls, windows;

  const
  LWA_COLORKEY = 1;
  LWA_ALPHA = 2;
  LWA_BOTH = 3;
  WS_EX_LAYERED = $80000;
  GWL_EXSTYLE = -20;


 {Function SetLayeredWindowAttributes Lib "user32" (ByVal hWnd As Long, ByVal Color As Long, ByVal X As Byte, ByVal alpha As Long) As Boolean }
 function SetLayeredWindowAttributes (hWnd:Longint; Color:Longint; X:Byte; alpha:Longint):bool stdcall; external 'USER32';

 {not sure how to alias these functions here ????   alias setwindowlonga!!}
 {Function SetWindowLong Lib "user32" Alias "SetWindowLongA" (ByVal hWnd As Long, ByVal nIndex As Long, ByVal dwNewLong As Long) As Long }
 Function SetWindowLongA (hWnd:Longint; nIndex:longint; dwNewLong:longint):longint stdcall; external 'USER32';


 {Function GetWindowLong Lib "user32" Alias "GetWindowLongA" (ByVal hWnd As Long, ByVal nIndex As Long) As Long }
 Function GetWindowLongA ( hWnd:Longint; nIndex:longint):longint stdcall; external 'user32';




type

  { TForm1 }

  TForm1 = class(TForm)
    procedure FormCreate(Sender: TObject);
  private
    { private declarations }

  public
    { public declarations }
  end;

var
  Form1: TForm1;

implementation

{ TForm1 }

procedure SetTranslucent(ThehWnd: Longint; Color: Longint; nTrans: Integer);
var
attrib:longint;

 begin

    {SetWindowLong and SetLayeredWindowAttributes are API functions, see MSDN for details }

    attrib := GetWindowLongA(ThehWnd, GWL_EXSTYLE);
    SetWindowLongA (ThehWnd, GWL_EXSTYLE, attrib Or WS_EX_LAYERED);
    {anything with color value color will completely disappear if flag = 1 or flag = 3  }
    SetLayeredWindowAttributes (ThehWnd, Color, nTrans,1);
end;




procedure TForm1.FormCreate(Sender: TObject);
var
   transparency:longint;

begin

     {the color were going to make transparent the red that the form backgroud is set to}
     transparency:=  $000002EE;


     {call the function to do it}
     SetTranslucent (form1.Handle, transparency, 0);


end;

initialization
  {$I unit1.lrs}

end.



all newbs this hung me up be sure and goto the form events tab oncreate click it and make the drop down menu say formcreate.  Also set the form color to $000002EE that's the only color the form will go transparent to unless you change the transparency =


bpsoftware I did find your webpage in the forums thank you however setting windows regions and blending the colors to create transparency is very different. I admire your work to create a transparent bunny etc by scanning for and makeing the region for controls however I didn't think that would be easy or work for me as I want the control to animate.  this is the starting point for me to animate pictures across my desktop.  Like makeing an active desktop.


all the rest of you guys I'll be sure and look at the new svn version however I think it's going to make the form fade in and out and the controls along with it and there was already code to do that in the forums however please correct me if I'm wrong.  I guess I poorly worded my original question,  It should have been how do I set the tforms backgroud color to transparent?

                                                       
« Last Edit: January 28, 2010, 06:20:41 am by jw »

cdbc

  • Full Member
  • ***
  • Posts: 236
    • http://www.cdbc.dk
Re: how do I make the form transparent
« Reply #5 on: January 28, 2010, 02:25:16 am »
Hi Jw
Code: [Select]
SetTranslucent (form1.hWnd, transparency, g_nTransparency, flag);
SetTranslucent (form1.Handle transparency, g_nTransparency, flag);

HTH

Regards Benny
If it ain't broke, don't fix it ;)

Paul Ishenin

  • Sr. Member
  • ****
  • Posts: 274
Re: how do I make the form transparent
« Reply #6 on: January 28, 2010, 02:31:48 am »
Since yesterday you can use

Form1.AlphaBlendValue := 100; // 0..255
Form1.AlphaBlend := True;

jw

  • Full Member
  • ***
  • Posts: 120
Re: how do I make the form transparent
« Reply #7 on: March 19, 2010, 04:54:41 pm »
here's the easiest way to set the a color on the form transperent much easier then my code above
credit to xpete
http://bugs.freepascal.org/view.php?id=1804

just include windows in uses and these 2 commands

SetWindowLong(Handle, GWL_EXSTYLE, GetWindowLong(Handle, GWL_EXSTYLE) or WS_EX_LAYERED);
SetLayeredWindowAttributes(Handle,clWhite,255,LWA_COLORKEY);



Leledumbo

  • Hero Member
  • *****
  • Posts: 8111
  • Programming + Glam Metal + Tae Kwon Do = Me
Re: how do I make the form transparent
« Reply #8 on: March 25, 2010, 01:03:49 pm »
Quote
here's the easiest way to set the a color on the form transperent much easier then my code above
Windows only code. Paul's solution is cross platform and even MUCH easier.

jw

  • Full Member
  • ***
  • Posts: 120
Re: how do I make the form transparent
« Reply #9 on: April 14, 2010, 12:19:27 am »
leledumbo

Pauls code fades the form in and out, controls and all.

Yes my code is windows only.


Don't confuse form transparency with setting a single color on the form as a transparent color which is what my code does.  This api call will also do what Pauls code does with the right parameters passed to it.


My api call is setting form regions according to a particular color clwhite!


I've exparmented with setting form regions useing only the LCL from code in the lazarus examples directory shapedcontrols.

unfortunatly it requires a tbitmap and will not use a tcustiombitmap which means you cant easily draw complex regions useing the color that just happens to be clwhite, our mask color.

The idea was to draw all the sprites to the form.canvas and then draw the masked regions on my mask.canvas and have what I needed but as I said it no worky it was slower then useing the API call and unable to use tcustombitmaps.


krzynio

  • Jr. Member
  • **
  • Posts: 73
Re: how do I make the form transparent
« Reply #10 on: February 27, 2014, 09:47:50 pm »
Since yesterday you can use

Form1.AlphaBlendValue := 100; // 0..255
Form1.AlphaBlend := True;

It is nice option but it doesn't work as expected. Childs of the form ale also blended i.e. images.
I am looking for solution on Linux.
Regards,
Krzysztof
Lazarus version: trunk. FPC version: 3.0.4. OS: Linux Debian Testing x64 with XFCE 4.12 and Windows 10 x64 version: 1709

Conn

  • Newbie
  • Posts: 2
Re: how do I make the form transparent
« Reply #11 on: September 13, 2015, 07:02:11 pm »
Krzysztof,

Did you find a solution to this?..

Cheers,
Conn

taazz

  • Hero Member
  • *****
  • Posts: 5363
Re: how do I make the form transparent
« Reply #12 on: September 13, 2015, 07:59:31 pm »
for windows there is the alpha blended technique that can be used for a per pixel transparency on other systems I guess there is something similar but I have no  experince outside windows.
Good judgement is the result of experience … Experience is the result of bad judgement.

OS : Windows 7 64 bit
Laz: Lazarus 1.4.4 FPC 2.6.4 i386-win32-win32/win64