Archive

Posts Tagged ‘manually’

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 Make the Whole Form Behave Like the Title Bar

December 15th, 1999 m3Rlin No comments

Sometimes you want to create a fancy looking form from a bitmap, like WinAMP or K-J?fol. This part isn’t too hard. All you have to do is make a cool graphic, use the TImage component and set the form’s border style to bsNone. Well, all’s good until we want to move the form… Since there is no title bar there may be a problem with this:) Well, as you should already know Delphi can get over any problem. All we have to do is manually take care of the WM_NCHITTEST Windows message.

In this example a TImage component named imgTitle will act like the form’s title bar.

uses
  Messages;
...
  public
    procedure WMNCHitTest(var Message: TWMNCHitTest); message WM_NCHITTEST;
  end;
...
procedure TForm1.WMNCHitTest(var Message: TWMNCHitTest);
var
  P: TPoint;
begin
  inherited;
  P := ScreenToClient(SmallPointToPoint(Message.Pos));
  with imgTitle do
    if (P.X >= Left) and (P.X < Left + Width) and (P.Y >= Top) and (P.Y < Top + Height) then
      Message.Result := htCaption;
end;