Mega Code Archive

 
Categories / Delphi / Examples
 

Creating cool transparent splash forms with delphi

Here is a very cool technique that not only gives you transparent forms but transparent forms with insanely complex shapes (or, I should say, the appearance of). The following is only tested in Delphi 1/2, but should also work without problems when using D3/4. And this is probably only good for splash screens, because moving a window doesn't always force a redraw, which is the trick. The trick is altering your form like this: Add the following procedures to your form: procedure paint; override; procedure WMERASEBKGND(var Msg:TMEssage); message wm_erasebkgnd; procedure TSplash.WMERASEBKGND(var Msg:TMEssage); begin msg.result:=1; end; Here is where the trickery comes - For this example I am using a component that I wrote whose only function is holding a number of bitmaps. With this component, several of my graphic controls can share the same bitmaps resources but not be forced into the standard palette - as you would have to if you were using .RES files. Anyways, I have two bitmaps - the first bitmap is my splash form and the second bitmap is an 8-bit grayscale mask. In the mask, black areas will be transparent, white areas will be clear and any shades of gray will be semi transparent. It's important to note, however, that the copymode settings used below do some weird crap, so it's best to use a mask with little to no gray, although a little gray will work and give your edges a more smooth anti-aliased appearance. You also don't have ot use bitmaps, you can draw or whatever you want to do. procedure TSplash.Paint; var t:TBitmap; begin // create an in-memory bitmap the same dimensions as my form t:=TBItmap.Create; t.width:=width; t.height:=height; // copy the area my form takes up from the desktop's image onto my drawing bitmap BitBlt(t.canvas.handle,0,0,width,height,GetDC(GetDesktopWindow),left,top,SRCCOPY); with t.canvas do begin copymode:=cmNotSrcErase; draw(0,0,splashrez.bitmap2); // draw my bitmap mask first copymode:=cmSrcErase; draw(0,0,splashrez.bitmap1); // Draw my splash logo/regular bitmap end; canvas.draw(0,0,t); // draw the entire thing to the form's canvas in one shot to reduce flicker t.free; end;