Jump to content
Tuts 4 You

Irregular Shape Form [delphi]


Recommended Posts

Has anyone made irregular shaped forms with delphi? and if so do you have any examples?

I have searched google and came up with some componates that I managed to install, But they dont work for me, Im using delphi 2007, I want to take either jpeg or png picture that i ahve done in photoshop and turn it into the actual form, Just like dup and uppp does. If someone has an example with delphi it would greatly appericiated.

Link to comment

Ehm for jpg's and png's dunno, I'll try to get the code for PNG but here's it for BMP

1. First, make or choose any background bitmap you want your form to have. Then fill areas you want to go transparent with a distinct color (In this example, it is white). NOTE: The bitmap's size must be the actual size you want on your form. No stretching in Delphi will work.

2. In Delphi, add a TImage(Image1) component on the form. Choose your bitmap and put the component where you want it. Autosize must be true. Other visual components must be on top of the "visible" part of the picture so that they will be seen.

3. Add the following code (...I mean short code) to your FormCreate procedure. I know I should have made a component for it so that no code would be needed. But just to show you how, I guess this would suffice.

procedure TForm1.FormCreate(Sender: TObject);
// Image Color to be made transparent
MASKCOLOR = clWhite; // Cutting adjustments
ADJ_TOP = 22;{TitleBar}
ADJ_BOTTOM = 22;{TitleBar}
ADJ_LEFT = 3;{Border}
ADJ_RIGHT = 3;{Border}
ShowRegion,CutRegion: HRgn;
begin ShowRegion:=CreateRectRgn(Image1.Left+ADJ_LEFT,Image1.Top+ADJ_TOP,
Image1.Left+Image1.Width+ADJ_RIGHT,Image1.Top+Image1.Height+ADJ_BOTTOM); // Cut the parts whose color is equal to MASKCOLOR by rows
for y:=0 to Image1.Picture.Bitmap.Height-1 do
x1:=0; // starting point of cutting
x2:=0; // end point of cutting
// the above will return -1 if x2 reached beyond the image
if (PixelColor=MaskColor) then
//do following if pixel reached beyond image or if color of pixel is not MaskColor
if x1 <> x2 then
// Create the region to be cut. The region will be one line of pixels/a pixel with color of // MaskColor
X1+Image1.Left+ADJ_LEFT ,
X2+Image1.Left+ADJ_RIGHT ,
Y+Image1.Top+ADJ_TOP+1); try
// RGN_DIFF will get the difference of ShowRegion
end; Inc(x2);
until PixelColor=-1;
end; // Set the window to have the above defined region
SetWindowRgn(Form1.Handle,ShowRegion,True); // NOTE : Do not free close/delete ShowRegion because it will become owned
// by the operating system // You can manually disable the showing of the whole
//form while dragging, with the following line but
// just leave it since it is dependent on your
// windows settings. Some people may want to have their
// windows show its contents while dragging. // SystemParametersInfo(SPI_SETDRAGFULLWINDOWS, 0, nil, 0); {Disable drag showing}
// SystemParametersInfo(SPI_SETDRAGFULLWINDOWS, 1, nil, 0); {Enable drag showing}

NOW FOR THE FORM DRAGGING PART (Thanks to delphi.about.com)

1. Add this line to the private declarations of your Form:

procedure WMNCHitTest(var Msg: TWMNCHitTest); message wm_NCHitTest;

2. In the implementation part. Add the following (assuming your Form name is Form1):

procedure TForm1.WMNCHitTest(var Msg: TWMNCHitTest);
if Msg.Result = htClient then
Msg.Result := htCaption;

Also, add a button to close the form because the title bar cannot be seen. Hope this

helps you, I'll try to get back with you about the PNG code ;) Here's a little bonus too,

about how to make a transparant shadow, so it LOOKS like png work haha :D

Override the CreateParams method of your form and add the

CS_DROPSHADOW flag to the WindowClass.Style.

CS_DROPSHADOW Enables the drop shadow effect on a window.

The effect is turned on and off through SPI_SETDROPSHADOW.

Typically, this is enabled for small, short-lived windows such as

menus to emphasize their Z order relationship to other windows.

TForm1 = class(TForm)
procedure CreateParams(var Params: TCreateParams); override;
Form1: TForm1;implementation{$R *.dfm}procedure TForm1.CreateParams(var Params: TCreateParams);
CS_DROPSHADOW = $00020000;
Params.WindowClass.Style := Params.WindowClass.Style or CS_DROPSHADOW;
Edited by F0X
Link to comment

Thank you very much for the effort you have put in to this, I will try it and get back with results, The main reason I wanted to use png file is because making a single colour transparent will still show the anti alaising(how ever its spelt) So you end up with a 1 pixel odd colour border, Now this can be eliminated by either using straight lines when making the irregular shape or manually going around the border in photoshop and adding a defined colour border with every single pixel (very time consuming). I will try this code tonight but just to let you know Delphi by default has an option the pick a colour that will be transparent thus making irregular shape forms, But the pixel problem arrises. Also there is a componate that allows .png but it does'nt seem to work with delphis transparent colour option.

Many thanks for your time and effort :)

Link to comment

Just a quick up with results fromy our code, It seems to work perfectly, But because it only supports bmp format and not alphs blending png I still will need to go around the edge of the picture using a 1 pixel pencil tool to define the border so I dont get the ugly Transparent colour which has been blending in to the border, Im gussing proberly a good way would use a similar colour for the transparent colour to the border it might blend in a little better. Also you might want to note that the drop shadow trick does'nt work It makes the form back to original with transparent colour showing although the actual drop shadow it self does work, It stop the other code from working

Link to comment

Hey Departure :D . There is a software called ITAZ EdgeTracer which can help with creating Regions for delphi VC++ and Visual Basic.

Link to comment

Looks like a good tool but it seems the hosted download links are dead now, I have gone to 5 pages worth of google links claiming to have the download but they are ALL just redirected itaz, And they have abbanded the product. If you find a download link I would be greatful because it looks like a very useful tool.


here a screen shot of What i want to use and then what i have to use because of the pixel problems, The first screen shot is what i did in photoshop and the interface i want to use, I made the transparent colour white here just for demo, and place it on a black desktop so you better a better view of how pixelated it becomes.


This next Picture I had to madify my interface, Well pretty much just make a complete new one, this one uses straight lines and 45 degree lines so it was easyer to define a border, This is work in progress so I just dropped a standard button on the form to show you how it works and as you can see with black stroke to image on a white background there is no real pixelated dogging out line, But I would of really liked to use a .png file format to allow for the alpha blending anti alias.


Anyway I will use this kind of interface untill I work a solution to .png's Thanks again for your help to the both of you's

Edited by Departure
Link to comment
  • 3 weeks later...

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Create New...