#include <memory>
/// Da al form la forma del bitmap dado, usando como color
// transparente el de la esquina inferior izquierda
void BitmapToRgn(TForm *form, TImage *img)
{
form->BorderStyle = bsNone;
Graphics::TBitmap *bmp =
dynamic_cast< Graphics::TBitmap* >( img->Picture->Bitmap );
form->SetBounds( form->Left, form->Top, bmp->Width, bmp->Height );
img->SetBounds( 0, 0, bmp->Width, bmp->Height );
form->Brush->Bitmap = bmp;
int tamanyoImagen = bmp->Width * bmp->Height * 4;
std::auto_ptr< BITMAPINFO > lpbmi( new BITMAPINFO );
std::auto_ptr< BYTE > bits( new BYTE[tamanyoImagen] );
ZeroMemory( lpbmi.get(), sizeof(BITMAPINFO) );
lpbmi->bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
lpbmi->bmiHeader.biWidth = bmp->Width;
lpbmi->bmiHeader.biHeight = bmp->Height;
lpbmi->bmiHeader.biPlanes = 1;
lpbmi->bmiHeader.biBitCount = 32;
lpbmi->bmiHeader.biCompression = BI_RGB;
lpbmi->bmiHeader.biSizeImage = tamanyoImagen;
GetDIBits( bmp->Canvas->Handle, bmp->Handle, 0, bmp->Height,
bits.get(), lpbmi.get(), DIB_RGB_COLORS );
DWORD colorTransparente = reinterpret_cast<DWORD*>
( bits.get() )[ bmp->Width * (bmp->Height-1) ];
HRGN rgn = CreateRectRgn( 0, 0, form->Width, form->Height );
HRGN rgnTemp = CreateRectRgn( 0, 0, form->Width, form->Height );
CombineRgn( rgn, rgn, rgnTemp, RGN_DIFF );
DeleteObject( rgnTemp );
for (int y=0; y < bmp->Height; ++y)
{
DWORD *pixel = &reinterpret_cast<DWORD*>
( bits.get() )[ bmp->Width * (bmp->Height - 1 - y) ];
int ancho = 0;
for (int x=0; x <= bmp->Width; ++x)
{
if ( x != bmp->Width && pixel[x] != colorTransparente )
ancho++;
else
if ( ancho > 0 )
{
rgnTemp = CreateRectRgn( x - ancho, y, x, y + 1 );
CombineRgn( rgn, rgn, rgnTemp, RGN_OR );
DeleteObject( rgnTemp );
ancho = 0;
}
}
}
SetWindowRgn( form->Handle, rgn, true );
DeleteObject( rgn );
}
//---------------------------------------------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner)
: TForm(Owner)
{
BitmapToRgn( this, Image1 );
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Image1MouseDown(TObject *Sender, TMouseButton Button,
TShiftState Shift, int X, int Y)
{
//Para poder mover el formulario haciendo click en cualquier lugar del mismo
if ( mbLeft == Button )
{
ReleaseCapture();
Perform( WM_SYSCOMMAND, 0xF012, 0 );
}
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Cerrar1Click(TObject *Sender)
{
Close();
}
//---------------------------------------------------------------------------
//_Leo