slloyd has asked for the wisdom of the Perl Monks concerning the following question:

I have been messing around with different Window Styles and with setting the transparency of windows created with Win32::GUI.
The following code demonstrates a few of the things I have been able to pull together from my research. I had to use the Win32::API module to help with setting the transparency stuff.
Usage:
The first argument is the style index number. This defaults to 0.
The second argument (optional) is the percent of transparency you want the window to be.
Example:
c:\>windemo.pl 4 65
This would set the style to "borderless rounded" and make the window 65 percent opaque.

Feel free to play around with it and add to it. I look forward to seeing your permutations

#!perl #windemo.pl #Created by Steve Lloyd, http://www.basgetti.com #Feel free to modify and use this code as you wish. use strict; use Win32::GUI; use Win32::API; my $MainWidth=400; #Width of Window my $MainHeight=400; #Height of Window my @styleStrings=( "WS_SYSMENU WS_MINIMIZE WS_MINIMIZEBOX WS_MAXIMIZE WS_MAXIMIZEBOX" +, # Normal Window "WS_SYSMENU WS_MINIMIZE WS_MINIMIZEBOX WS_MAXIMIZE WS_MAXIMIZEBOX +WS_VSCROLL", # Normal Window with vertical scroll "WS_SYSMENU", # Only System Menu and Close "WS_SYSMENU WS_MINIMIZE WS_MINIMIZEBOX", #System Menu, Close, a +nd Minimize "borderless rounded", #Borderless Window (no Menu) with a round +ed edge "borderless WS_BORDER", #Borderless Window (no Menu) with a sma +ll sharp edge ); my $index=shift || 0; #pass first argument to set what style of win +dow to show my $alpha=shift; #pass second argument to set transparency perc +ent (0-100) if(!defined $styleStrings[$index]){$index=0;} my $styleString=$styleStrings[$index]; my $MainWindow = new Win32::GUI::Window( -name => "MainWindow", -text => 'WinDemo.pl', -width => $MainWidth, -height => $MainHeight, -pos => [300, 100 ], -style => &setWindowStyle($styleString), -topmost=> 1, ); $MainWindow->AddButton( -text => "Close", -name => "MyButton", -pos => [10, 10 ], -size => [100,20], -tip => "Close this window", ); $MainWindow->AddTextfield( -text => $styleString, -name => "MyStyle", -pos => [10, 110 ], -size => [360,30], -readonly => 1, ); if(length($alpha)){&makeTransparent($MainWindow,$alpha);} $MainWindow->Show(); my $dialog=Win32::GUI::Dialog(); sub MyButton_Click{ return -1; } sub setWindowStyle{ my $str=shift; my %Style=( WS_BORDER => 0x800000, WS_CAPTION => 0xC00000, WS_CHILD => 0x40000000, WS_CLIPCHILDREN => 0x2000000, WS_CLIPSIBLINGS => 0x4000000, WS_DISABLED => 0x8000000, WS_DLGFRAME => 0x400000, WS_GROUP => 0x20000, WS_HSCROLL => 0x100000, WS_MAXIMIZE => 0x1000000, WS_MAXIMIZEBOX => 0x10000, WS_MINIMIZE => 0x20000000, WS_MINIMIZEBOX => 0x20000, WS_OVERLAPPED => 0x0, WS_POPUP => 0x80000000, WS_SYSMENU => 0x80000, WS_TABSTOP => 0x10000, WS_THICKFRAME => 0x40000, WS_VISIBLE => 0x10000000, WS_VSCROLL => 0x200000, ); $Style{WS_CHILDWINDOW} = $Style{WS_CHILD}; $Style{WS_ICONIC} = $Style{WS_MINIMIZE}; $Style{WS_OVERLAPPEDWINDOW} = $Style{WS_OVERLAPPED} || $Style{WS_C +APTION} || $Style{WS_SYSMENU} || $Style{WS_THICKFRAME} || $Style{WS_M +INIMIZEBOX} || $Style{WS_MAXIMIZEBOX}; $Style{WS_POPUPWINDOW} = $Style{WS_POPUP} || $Style{WS_BORDER} || +$Style{WS_SYSMENU}; $Style{WS_SIZEBOX} = $Style{WS_THICKFRAME}; $Style{WS_TILED} = $Style{WS_OVERLAPPED}; $Style{WS_TILEDWINDOW} = $Style{WS_OVERLAPPEDWINDOW}; #uppercase the string coming in my $ustr=uc($str); #if the string matches a value in the hash, return the hash value if(defined $Style{$ustr}){return $Style{$ustr};} #split the string up and do a little fuzzy logic to determine what + they want my @parts=split(/[\ \t\;\:\,]+/,$str); my $rval; foreach my $part (@parts){ if($part=~/^borderless$/is){$part="WS_POPUP";} elsif($part=~/vscroll/is){$part="WS_VSCROLL";} elsif($part=~/hscroll/is){$part="WS_HSCROLL";} elsif($part=~/rounded/is){$part="WS_THICKFRAME";} my $upart=uc($part); if(defined $Style{$upart}){ $rval=($rval | $Style{$upart}); } } return $rval; } sub makeTransparent{ #usage my $ck=makeTransparent($MainWindow,35); my $Window=shift || return 0; my $pcnt=shift || return 0; #Get handle of object passed in my $topHwnd=$Window->{-handle}; #Convert percent value to alpha value my $alpha=int(($pcnt/100)*255); #Set some varialves my $WS_EX_LAYERED = hex(80000); my $GWL_STYLE = (-16); my $GWL_EXSTYLE = (-20); my $LWA_COLORKEY = 1; my $LWA_ALPHA = 2; my $GetWindowLong = new Win32::API("user32","GetWindowLong",['N',' +I'],'N') || return $^E; my $SetWindowLong = new Win32::API("user32","SetWindowLong",['N',' +I','N'],'N') || return $^E; my $SetLayeredWindowAttributes = new Win32::API("user32","SetLayer +edWindowAttributes",['N','N','C','N'],'N') || return $^E; my $ck = $SetWindowLong->Call($topHwnd, $GWL_EXSTYLE, ($GetWindowL +ong->Call($topHwnd, $GWL_EXSTYLE) | $WS_EX_LAYERED)); my $alphaValue=chr($alpha); #0 to 255, where 0=transparent an +d 255=opaque $ck = $SetLayeredWindowAttributes->Call($topHwnd, 0, $alphaValue, +$LWA_ALPHA); return $ck; } exit;

Replies are listed 'Best First'.
Re: Win32::GUI Window Styles and Transparency
by BrowserUk (Patriarch) on Jun 15, 2005 at 06:48 UTC

    Intriguing, but you should try -w occasionally. You have a bunch of Useless use of hash element in void context at P:\test\466797.pl line 76. errors:

    $Style{WS_CHILDWINDOW} => $Style{WS_CHILD}; # ^ Should be = not =>

    Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
    Lingua non convalesco, consenesco et abolesco. -- Rule 1 has a caveat! -- Who broke the cabal?
    "Science is about questioning the status quo. Questioning authority".
    The "good enough" maybe good enough for the now, and perfection maybe unobtainable, but that should not preclude us from striving for perfection, when time, circumstance or desire allow.
      That is what I get for hurrying... I have updated the code to reflect the changes to the hash values. Thanks.
Re: Win32::GUI Window Styles and Transparency
by Anonymous Monk on Jun 15, 2005 at 11:35 UTC
    Interesting - I've been searching from time to time for information on how to do GUI stuff in Windows perl. Everytime I looked, there was some important piece missing though. Looks like it's more complete now. I tested your script with ActiveState build 638 (perl 5.6.1).

    One thing: when I maximise the window, it seems to set the correct size, but the window is not moved. If I do move it before maximising, it jumps back to the original location. Anyone know what could be causing this?