How to make transparent form from image file in Delphi ? – Visual Component Library.
Hello again! How is going Delphi Development Learning! Mine is awesome!
Yeah, It is awesome. Because, every day in my school, I am learning algorithms using Pascal and Delphi programming languages. It’s so cool. We are solving programming tasks from Programming Task-book.
Let’s start our interesting tutorial… In this tutorial (or lesson) you can learn how to make transparent form from image file (PNG images).
First of all, you need to create VCL Application – Delphi. Then, create another VCL Form.
My Form and Units :
Design your main form like this one :
So yeah, we need OpenDialog component to open image from hard disk.
Second form you should change the BorderStyle property to bsNone of TForm.
Add PNGImage module(library) to your main uses list
Copy and paste this code (procedure) to your main Unit.
procedure CreateSplashFromImage(MyForm: TForm; FileName: String); var FormImage: TPngImage; pptDst, pptSrc: TPoint; Size: TSize; BlendFunction: TBlendFunction; BMP: TBitmap; begin FormImage := TPngImage.Create; BMP := TBitmap.Create; Try FormImage.LoadFromFile(FileName); BMP.Assign(FormImage); pptDst := Point(MyForm.Left, MyForm.Top); pptSrc := Point(0, 0); Size.cx := FormImage.Width; Size.cy := FormImage.Height; MyForm.ClientWidth := FormImage.Width; MyForm.ClientHeight := FormImage.Height; BlendFunction.BlendOp := AC_SRC_OVER; BlendFunction.BlendFlags := 0; BlendFunction.SourceConstantAlpha := $FF; BlendFunction.AlphaFormat := AC_SRC_ALPHA; SetWindowLong(MyForm.Handle, GWL_EXSTYLE, GetWindowLong(MyForm.Handle, GWL_EXSTYLE) or WS_EX_LAYERED); UpdateLayeredWindow(MyForm.Handle, MyForm.Canvas.Handle, @pptDst, @Size, BMP.Canvas.Handle, @pptSrc, 0, @BlendFunction, ULW_ALPHA); Finally BMP.Free(); FormImage.Free; End; end;
Create OnClick event on TButton. Write this one too.
procedure TFormMain.Button1Click(Sender: TObject); begin if OpenDialog1.Execute then begin CreateSplashFromImage(Form1, OpenDialog1.Filename); Form1.ShowModal; end; end;