Saturday, June 2, 2012

TRANSPARENT WINDOWS APPLICATION

unit layeredwin;

interface

uses
  Windows, Messages, Forms;

type
  TSetLayeredWindowAttributes = function(hwnd:HWND; crKey:Longint; bAlpha:byte; dwFlags:longint ) :BOOL ; stdcall;

const
  WS_EX_LAYERED = $80000;
  AC_SRC_OVER = $0;
  AC_SRC_ALPHA = $1;
  AC_SRC_NO_PREMULT_ALPHA = $1;
  AC_SRC_NO_ALPHA = $2;
  AC_DST_NO_PREMULT_ALPHA = $10;
  AC_DST_NO_ALPHA = $20;
  LWA_COLORKEY = $1;
  LWA_ALPHA = $2;
  ULW_COLORKEY = $1;
  ULW_ALPHA = $2;
  ULW_OPAQUE = $4;

implementation

end.

Once you've saved that as layeredwin.pas put layeredwin in your form's uses clause.
And for the form's OnCreate put:

procedure TForm1.FormCreate(Sender: TObject);
var
  SetlayeredWindowAttributes: TSetLayeredWindowAttributes;
  hUser32: hInst;
begin
  SetWindowLong(Handle, GWL_EXSTYLE, GetWindowLong(Handle, GWL_EXSTYLE) or WS_EX_LAYERED);
  hUser32 := LoadLibrary('user32.dll');
  if hUser32 <> 0 then
  begin
    try
      SetLayeredWindowAttributes := GetProcAddress(hUser32, 'SetLayeredWindowAttributes');
      if Assigned(@SetLayeredWindowAttributes) then
        SetLayeredWindowAttributes(handle, 0, 180, LWA_ALPHA);  
//180 is the amount of transparency, from 0 to 255
     finally
      FreeLibrary(hUser32);
    end;
  end;
end;

There is a more simple way of doing it, but this way allows it to run on Windows 95/98.

To completely turn off the transparency use SetWindowLong and remove the WS_EX_LAYERED flag.
Check http://msdn.microsft.com for more info.

0 comments:

Post a Comment

 
Related Posts Plugin for WordPress, Blogger...