I want to create a Delphi project with a custom Style, like the below image.
As you can see, the MainForm is transparent, and there are some components inside of it.
How can I create this transparent MainForm?
P.S:I want to create VCL project and i drawn this image by Photoshop.
CodePudding user response:
Assuming you're using Firemonkey since you dont specify VCL or FMX.
You don't really need to create a style for this. Rather a simpler way would be to:
- Set the form transparent property to
True
- Place a
TRectangle
on the form and make it align toMost left
and adjust the color etc to your liking. - Place another
TRectangle
on your form and set the alignment toClient
. You can then adjust theOpacity
property for transparency, thefill
property forcolor
and then thestroke
for theborder
. - I see a small space between the left and right in the photo. This can be done by setting a
margin left
value on the secondTRectangle
you placed.
Now this should look like you what want it to be provided you disabled the border of the form. There is 1 more thing we need to do to avoid having the controls show as transparent. We need to place a layout on the form. Not the rectangle. If you place the controls on the rectangle with opacity the controls will also be transparent. If you want the controls to have the same transparency value place it on the rectangle. Otherwise do the following:
- Place a
TLayout
on the form. Not the rectangle. - Set the align property to Contents. This will make it overlap everything.
- Set the
margin left
property of the layout to the width of the left rectangle you placed the margin value of the client rectangle you placed to compensate for the little space. - Place your controls on the layout and they should show with default opacity.
One last thing to do is implement window drag. Assuming you disabled the border, your users will not be able to move the window at all since there is no border to drag. On the MouseDown event of the component you want the drag to start add the following code:
procedure TForm1.rctngl1MouseDown(Sender: TObject; Button: TMouseButton;
Shift: TShiftState; X, Y: Single);
begin
StartWindowDrag;
end;
You should be all done now.