Create the Image

The first thing to do is to create some stunning graphics for your splash screen. My artistic skills are somewhat, um, limited, so I usually just go with a logo or thematic symbol of some sort and apply some effects. Since we would like to show of the alpha blending, a fat drop shadow, some glow and maybe a lense flare is probably a good choice.

If you are lazy there’s plenty of free goodies you can use to get started. The important thing for now is that we should end up with an image that contains some semitransparent areas.

Creating a premultiplied bitmap in PhotoShop

With your PhotoShop image at hand you should now be ready to practice the black art of creating a premultiplied bitmap. From my research on this topic it appears to be a closely guarded secret known only to a select few ninja PhotoShoppers, as none of the instructions I have found worked for me. The following however works:

  1. Open or create a transparent image in PhotoShop.
  2. Merge Visible Layers (Shift+Ctrl+E).
    This flattens the image while keeping the transparency.
  3. Create a new Solid Color Fill Layer (Layer, New Fill Layer, Solid Color…).
    Set the fill color to black.
  4. Move the Fill Layer to the bottom (Shift+Ctrl+[).
  5. Auto-select the Image Layer (Ctrl+Click on the layer or Right-click on the layer+Select Pixels).
  6. Switch to the Channels tab and Save selection as channel (PhotoShop - The “Save selection as channel” button).
  7. Save the image as a BMP (make sure Alpha Channels is enabled and checked).
    Under Advanced Modes, select the 32-bit, A8R8G8B8 format.
  8. Presto!

With any luck you should now have created the magic bitmap. Now we just need an application to display it, so without further ado we move on to…

Displaying an Alpha Blended Bitmap

Ready? OK, fire up Delphi and… download the source code. I won’t guide you through the steps needed to create this tiny application. Instead let me explain the key points while you browse through the code:

The project file has been modified so the splash form isn’t created with TApplication.CreateForm. This is important because the first form that is created with TApplication.CreateForm automatically becomes the application’s main form and we don’t want that to happen. Why? Because the application terminates once the main form is destroyed. Instead the splash form is created anonymously and its Execute method is called.

program AlphaSplashDemo;
...
begin
  Application.Initialize;
  Application.MainFormOnTaskbar := True;
  with TFormSplash.Create(Application) do
    Execute;
  Application.CreateForm(TForm1, Form1);
  Application.Run;
end.

Moving on to the splash form, the Position property has been set to poScreenCenter so the form will center itself on the screen and the FormStyle property has been set to fsStayOnTop so it will stay on top of other forms.

The OnFormClose event handler makes sure the form is destroyed when it it closed and the OnKeyPress event handler just makes it easier to close the form during tests.

procedure TFormSplash.FormClose(Sender: TObject; var Action: TCloseAction);
begin
  Action := caFree;
end;
 
procedure TFormSplash.FormKeyPress(Sender: TObject; var Key: Char);
begin
  Close;
end;

The WM_NCHITTEST message handler makes it possible to move the form even though it hasn’t got a caption or a border.

procedure TFormSplash.WMNCHitTest(var Message: TWMNCHitTest);
begin
  Message.Result := HTCAPTION;
end;

The Execute method contains the meat. In it we set the WS_EX_LAYERED window flag, load the bitmap and resize the window to fit the bitmap. Then the alpha blending parameters are prepared and UpdateLayeredWindow is called. Finally we start a timer to close the splash form after a short while. Don’t display the splash form for too long or you will just annoy your users.

procedure TFormSplash.Execute;
var
  BlendFunction: TBlendFunction;
  BitmapPos: TPoint;
  BitmapSize: TSize;
  exStyle: DWORD;
  Bitmap: TBitmap;
begin
  // Enable window layering
  exStyle := GetWindowLongA(Handle, GWL_EXSTYLE);
  if (exStyle and WS_EX_LAYERED = 0) then
    SetWindowLong(Handle, GWL_EXSTYLE, exStyle or WS_EX_LAYERED);
 
  Bitmap := TBitmap.Create;
  try
    Bitmap.LoadFromFile('splash.bmp');
 
    ASSERT(Bitmap.PixelFormat = pf32bit,
      'Wrong bitmap format - must be 32 bits/pixel');
 
    // Resize form to fit bitmap
    ClientWidth := Bitmap.Width;
    ClientHeight := Bitmap.Height;
 
    // Position bitmap on form
    BitmapPos := Point(0, 0);
    BitmapSize.cx := Bitmap.Width;
    BitmapSize.cy := Bitmap.Height;
 
    // Setup alpha blending parameters
    BlendFunction.BlendOp := AC_SRC_OVER;
    BlendFunction.BlendFlags := 0;
    BlendFunction.SourceConstantAlpha := 255;
    BlendFunction.AlphaFormat := AC_SRC_ALPHA;
 
    // ... and action!
    UpdateLayeredWindow(Handle, 0, nil, @BitmapSize, Bitmap.Canvas.Handle,
      @BitmapPos, 0, @BlendFunction, ULW_ALPHA);
 
    Show;
  finally
    Bitmap.Free;
  end;
  // Start timer to hide form after a short while
  TimerSplash.Enabled := True;
end;

The Splash Screen Demo

When you run the demo application, with the sample bitmap, it should look something like this:

You can replace the bitmap I have provided with your own. Just copy it to the demo folder and name it splash.bmp.

What’s next?

In this part we used a windows bitmap for the splash screen. While this works just fine, 32-bit bitmaps can easily get quite big (the demo bitmap is almost 1Mb in size) and they add a considerable overhead to an application - with little benefit to the end user. Wouldn’t it be nice if we could compress the bitmap to minimize the overhead?

In the second part of this article, I will modify the demo application to use a compressed PNG image instead of a BMP. I will also move the bitmap to a resource file, demonstrate run-time premultiplication and enhance the splash screen with a few visual gimmicks.

References

API documentation

Related stuff

Requirements

The source code that accompany this article are known to be compatible with the following versions of Delphi:

D1 D2 D3 D4 D5 D6 D7 D2005 D2006 D2007
Fail Fail Fail Fail Fail Unknown Pass Unknown Pass Pass

License

Creative Commons License
This work is licensed under a
Creative Commons Attribution-Share Alike 3.0 Unported License.

Download

download
Download: Alpha Blended Splash Form - Part 1
Version: 1.1
Updated: 27 May, 2008
Size: 56.7 KB
Notes: Includes source and sample images.
Downloads: 15,341

Change log

  • 2008-05-25
    • Replaced the sample bitmap and PhotoShop image with a smaller one.