Archive

Posts Tagged ‘forms’

How To and Why Use Dynamically Created Forms

December 15th, 1999 m3Rlin 2 comments

You almost never need all your application’s forms in memory all the time. To reduce the amount of memory required at load time and load time, you may want to create some forms only when you need to use them. For example, a dialog box needs to be in memory only during the time a user interacts with it.

Well, to do so you first have to create a form (or take an existing one), move it from the Project|Options auto-create list to the available forms list. You can do this manually too. Select Project|View source or View|Project source and remove the code creating the form. If your form is named Form1 then remove the following line from the projects source:

Application.CreateForm(TForm1, Form1);

Here’s the code that creates the form and after closing it, destroys it:

{ Use this for modal forms }
with TForm1.Create(Self) do
try
  ShowModal;
finally
  Free;
end;
...
{ Use this for non-modal forms }
var
  Form1: TFrom1;
...
begin
  Form1 := TForm1.Create(Self);
  Form1.Show;
end;

With non-modal forms you have to add this code to the form’s OnClose event:

...
Action := caFree;
...

This will free the memory allocated by the form when it is closed.

How to Create Elliptic Forms

December 15th, 1999 m3Rlin No comments

Ever wanted to create forms that had a cool shape? How about an elliptic form? This would make a nice splash screen, wouldn’t it? Sure. But how can I create one of these? Hmmmm… Well, Windows 32 bit has all we need. CreateEllipticRgn() allows you to create an elliptic region. After we do so all we have to do is assign the region to our form. This can be done using the SetWindowRgn() function determines the area of a window that can be drawn on. The area not included in this region will not be displayed. You can use the first function a couple of times to create “weird” shapes :-) Then assign the created region using the second routine.

Add this code to your form’s OnCreate event:

var
  rgnNew: Hrgn; // The region variable
  ...
begin
  ...
  rgnNew := CreateEllipticRgn(1, 1, 100, 100); // A small circle
  { Work on the region here if you want to change it a bit more }
  SetWindowRgn(Handle, rgnNew, True);
  ...
end;