use strict;
use warnings;
use Win32::GUI;
use Win32::GUI::BorderlessWindow;
use Win32::API;

my $isSkin = 0;

my $SetWindowPos = Win32::API->new("user32","SetWindowPos", "LLLLLLL", "L")
    or die "Failed to load SetWindowPos";

sub SWP_FRAMECHANGED() {32}
sub SWP_NOMOVE() {2}
sub SWP_NOSIZE() {1}
sub SWP_NOZORDER() {4}
sub SWP_NOACTIVATE() {16}
sub WM_NCLBUTTONDOWN() {161}
sub HTCAPTION() {2}

my $skin = Win32::GUI::Bitmap->new("skin3.bmp");

my ($bWidth, $bHeight, $bColors, $bBPP) = $skin->Info;

my $winSkin = Win32::GUI::Window->new(
				      -width  => $bWidth,
				      -height => $bHeight,
				      -name   => "winSkin",
				      -text   => "Skin Test",
				      );


my $dcSkin = Win32::GUI::DC::CreateCompatibleDC(0);


$dcSkin->SelectObject($skin);

# Get the 32bit version of the skin image

my @bitmap32 = unpack ("C*", $skin->GetDIBits($dcSkin));

my $p1 = 0;

my @bitmap24;

print "Creating 24bit image\n";

# Convert it to 24 bits
for (my $p = 0; $p < scalar(@bitmap32); $p+= 4) {
    $bitmap24[$p1++] = ($bitmap32[$p] << 16) | (($bitmap32[$p + 1]) << 8)
	| ($bitmap32[$p + 2]);
}

# The transparent color (white)
my $trans = 0xFFFFFF;


$winSkin->Show();


Win32::GUI::Dialog;

sub winSkin_Paint
{
    my ($dc) = @_;

    $dc->BitBlt(0, 0, 320, 340, $dcSkin, 0, 0, 0xCC0020) if $isSkin;
    $dc->BkMode(1);
    if ($isSkin) {
	$dc->TextOut(25, 50, "Return to normal mode to close!");
	$dc->TextOut(80, 70, "Sorry, can't move the Skin yet!");
	$dc->TextOut(115, 90, "Right Click: Remove Skin!");
    } else {
	$dc->TextOut(115, 90, "Right Click: Show Skin!");
    }
    $dc->Validate;
}

sub winSkin_MouseRightDown
{
    if ($isSkin) {
	removeRegion();
    } else {
	addRegion();
    }
}

sub winSkin_MouseDown
{
    return if !$isSkin;

# Allow the skin to be dragged
    Win32::GUI::SendMessage($winSkin, WM_NCLBUTTONDOWN, HTCAPTION, \0);
}


sub removeRegion
{
    $winSkin->SetWindowRgn(0, 1);

    $winSkin->Change(-pushstyle => WS_CAPTION);
    $winSkin->Change(-pushstyle => WS_SIZEBOX);
    $winSkin->InvalidateRect(1);
    $SetWindowPos->Call($winSkin->{-handle}, 0, 0, 0, 0, 0,
			SWP_FRAMECHANGED|SWP_NOMOVE|SWP_NOSIZE|
			SWP_NOACTIVATE|SWP_NOZORDER);
    $isSkin = 0;
}

sub addRegion
{
    print "Generating region\n";
    my $hRgn = Win32::GUI::Region::CreateRectRgn(0, 0, 0, $bWidth, $bHeight);

    my $pos = 0;

    for (my $y = 0; $y < $bHeight; $y++) {
	for (my $x = 0; $x < $bWidth; $x++) {
# This is a transparent pixel
	    if ($bitmap24[$pos] == $trans) {
		my $x1 = $x;
# For some reason, the image is flipped, so flip it back.
		my $y1 = ($bHeight - $y) - 1;
# Create a region with this pixel
		my $tmpRgn = Win32::GUI::Region::CreateRectRgn(0, $x1, $y1, $x1+1, $y1+1);
# Remove this pixel from the master region
		Win32::GUI::Region::CombineRgn($hRgn, $hRgn, $tmpRgn, 3);
# Delete the temp region
		Win32::GUI::DC::DeleteObject($tmpRgn);
	    }
	    $pos++;
	}
    }
# Offset to region to handle the borders and caption
    $hRgn->OffsetRgn(Win32::GUI::GetSystemMetrics(SM_CXBORDER) * 4,
		     Win32::GUI::GetSystemMetrics(SM_CYCAPTION) + 4);

# Apply the region
    $winSkin->SetWindowRgn($hRgn, 1);

# Remove the caption and border

    $winSkin->Change(-popstyle => WS_CAPTION);
    $winSkin->Change(-popstyle => WS_SIZEBOX);
# Force a redraw
    $winSkin->InvalidateRect(1);
# and refresh the window
    $SetWindowPos->Call($winSkin->{-handle}, 0, 0, 0, 0, 0,
			SWP_FRAMECHANGED|SWP_NOMOVE|SWP_NOSIZE|
			SWP_NOACTIVATE|SWP_NOZORDER);
    $isSkin = 1;
    print "Region generated\n";
}

