Hi,
    I searched in this mailing list how to create a semi-transparent looking 
window and a free-shaped one (see Win32 SDK - Layered Windows 
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dnwui/html/layerwin.asp)
 and found nothing about it, so i searched on MSDN and found the API code and 
tried until i got it.

Note that this functionality is fully supported by Windows 2000/XP only.

Here is the code, hope it will be useful to someone else...

#-------------------------------------------------------------
# use Win32::GUI etc... 

use Win32;
use Win32::API;

use constant GWL_EXSTYLE             => -20;
use constant WS_EX_LAYERED           => 0x00080000;
use constant LWA_COLORKEY            => 0x00000001;
use constant LWA_ALPHA               => 0x00000002;

# BOOL SetLayeredWindowAttributes(
# HWND hwnd,           // handle to the layered window
# COLORREF crKey,      // specifies the color key
# BYTE bAlpha,         // value for the blend function
# DWORD dwFlags        // action
# );
my $SetLayeredWindowAttributes = new Win32::API("user32", 
"SetLayeredWindowAttributes", [ 'N', 'N', 'I', 'N' ], 'N') or die "Cannot 
create SetLayeredWindowAttributes()";

# ... here create your window object ($winObj) as usual...

# set alpha channel value aka opaqueness for your window, range 0-255 
(transparent-opaque)
my $alpha = 200;

# set color key value for your window, format is 0x00rrggbb, range 
0x00000000-0x00ffffff
# window regions painted with $colKey color will be completely trasparent (to 
mouse events too..)
my $colKey = 0x0000FF00;

# set the flags needed for alpha, colorkey or both...
# LWA_ALPHA activates alpha opaqueness using $alpha value
# LWA_COLORKEY activates color key using $colKey value
my $flags = LWA_ALPHA | LWA_COLORKEY;

# add the WS_EX_LAYERED extended style to your window (needed to use 
alpha/colorkey properties)
$objWin->SetWindowLong(GWL_EXSTYLE, Win32::GUI::GetWindowLong($objWin, 
GWL_EXSTYLE) | WS_EX_LAYERED );

# uncomment this and your window wil be COMPLETELY transparent to mouse 
events...
#$objWin->SetWindowLong(GWL_EXSTYLE, Win32::GUI::GetWindowLong($objWin, 
GWL_EXSTYLE) | WS_EX_TRANSPARENT );

# activate alpha/colorkey
$SetLayeredWindowAttributes->Call($objWin->{-handle}, $colKey, $alpha, $flags) 
or die "\n".Win32::GetLastError();

# ... now $objWin->Show() your window as usual...
#-----------------------------------------------------

Bye.

    Luigino Masarati
    OutSys snc.

Reply via email to