Delphi Programming

  1. Home
  2. Computing & Technology
  3. Delphi Programming
Transparency in Delphi 6
How to create a transparent form with Delphi 6 - allowing windows behind the form to completely show through.
 Join the Discussion
"Post your views, comments, questions and doubts to this article."
Discuss!
 Related Resources
• What's new in Delphi 6
• Win/API programming
• Enhancing Delphi forms
• Graphics programming
• Custom shaped forms

One of the great new features Delphi 6 introduced to Windows developers is the ability to create forms (windows) that are (semi) transparent. In Delphi 6, the TForm class supports layered forms with AlphaBlend, AlphaBlendValue, TransparentColor, and TransparentColorValue properties.

For a form being transparent means that a user can see through the form - the form merges its content with what's behind it on the screen.

To "prepare" a form for transparency you need to set the AlphaBlend property to True. Once AlphaBlend is True, the AlphaBlendValue property specifies the degree of transparency. This property accepts values from 0 to 255. A value of 0 indicates a completely transparent window. A value of 255 indicates complete opacity.

As with all published properties you set the AlphaBlend and AlphaBlendValue at design time (or run time) with the Object Inspector.

The following About box form has the AlphaBlendValue set to 210.

Semy transparent about box

You might think that you'll seldom need this new Delphi feature in a business application, here is an example that, even in a business application, will attract your users:

procedure TAboutBox.FormClose
(Sender: TObject; var Action: TCloseAction);
var
  i, cavb : 0..255;
begin
  if AlphaBlend=False then
  begin
    AlphaBlendValue:=255;
    AlphaBlend:=True;
  end;
  cavb:=AlphaBlendValue;

  for i := cavb downto 0 do
  begin
    AlphaBlendValue := i;
    Application.ProcessMessages;
  end
end;

The above code, the OnClose event of an about box form, creates a fading effect. When the user tries to close the form - the form slowly vanishes. The trick is simple, just decrease the AlphaBlendValue to 0.

Another two new form properties in Delphi 6 are TransparentColor and TransparentColorValue. The TransparentColor is a boolean property that specifies whether a color on a form, specified in the TransparentColorValue property, appears transparent.

As an example, set TransparentColor for our About box form to True. Let the TransparentColorValue be clWhite - that is white. The Memo component has a white background, this is how our About box now looks:

TransparentColor

And finally, there's one glitch. All the properties discussed in this article have no effect if your application is not running under Windows 2000 or better, with the machine P90 or better.

If you have any questions, or you know of a great example where AlphaBlending can be handy, please post to the Delphi Programming Forum.

Explore Delphi Programming

About.com Special Features

Build Your Own Website

Step-by-step advice on how to do everything from choosing a Web host to promoting your content. More >

Connect Your Home Computers

Easy ways to connect two computers for networking purposes. More >

Delphi Programming

  1. Home
  2. Computing & Technology
  3. Delphi Programming

©2009 About.com, a part of The New York Times Company.

All rights reserved.