Mega Code Archive

 
Categories / Delphi / Forms
 

Creating Transparent Forms

Title: Creating Transparent Forms Question: How can I create a transparent form? Answer: The reason of this article is to create a transparent form. What that means? A form that has a custom look. First of all we need some basic concepts. The window is a rectangle where you put your controls. This is a bad definition, but good for our starts. In fact, in windows, any control you use is a window that has a region, named client region. We will use the windows regions to create our custom form. Look at the bellow code: procedure TForm1.FormCreate(Sender: TObject); var MyRgn, ClientRgn, ButtonRgn: THandle; Margin, X, Y: Integer; begin Margin := (Width - ClientWidth) div 2; MyRgn := CreateRectRgn(0, 0, Width, Height); X := Margin; Y := Height - ClientHeight - Margin; ClientRgn := CreateRectRgn(X, Y, X + ClientWidth, Y + ClientHeight); CombineRgn(MyRgn, MyRgn, ClientRgn, RGN_XOR); X := X + Button1.Left; Y := Y + Button1.Top; ButtonRgn := CreateRectRgn(X, Y, X + Button1.Width, Y + Button1.Height); //-----------!!----------- //Insert your additional controls here! CombineRgn(MyRgn, MyRgn, ButtonRgn, RGN_XOR); SetWindowRgn(Handle, MyRgn, True); end; MyRegion, ClientRegion and ButtonRegion are regions. What the code do? First it create a region with our forms with and height. Then create a region for a button component on the form. Finally it combine the button region with form region using XOR. And, in the final it use api function SetWindowRgn for set the form region. In order to create a more sophisticated forms you need a mask (a image in black and white) with your form look. First, create a function that will scan your mask point by point. If the point is black, add point region to MyRgn using XOR. Then, set the form region like in our example code. The main goal of this algorithm is speed. If you want to have better performance, is good to handle the entire work for seting regions in design time, and at runtime only to load the region.