Marco Cantù 1998, Mastering Delphi 4
Project: CHILD2.DPR
Project Structure
CHILD2.DPR
program Child2;
uses
Forms,
MainF in 'MainF.pas' {MainForm},
ChildF in 'ChildF.pas' {ChildForm};
{$R *.RES}
begin
Application.CreateForm(TMainForm, MainForm);
Application.Run;
end.
MAINF.PAS
unit MainF;
interface
uses
SysUtils, Windows, Messages, Classes, Graphics, Controls,
Forms, Dialogs, Childf, Menus;
type
TMainForm = class(TForm)
MainMenu1: TMainMenu;
File1: TMenuItem;
New1: TMenuItem;
procedure New1Click(Sender: TObject);
private
{ Private declarations }
Counter: Integer;
public
ActiveForm: TChildForm;
end;
var
MainForm: TMainForm;
implementation
{$R *.DFM}
procedure TMainForm.New1Click(Sender: TObject);
begin
// disable the current active form, if any
if Assigned (ActiveForm) then
SendMessage (ActiveForm.Handle, wm_NcActivate, 0, 0);
// increase the child window counter
Inc (Counter);
// create a new form and define it as child of the current form
ActiveForm := TChildForm.Create (self);
ActiveForm.Parent := self;
// add the number to the caption, and move it slightly
ActiveForm.Caption := ActiveForm.Caption + ' ' +
IntToStr (Counter);
ActiveForm.Left := (Counter * 30) mod 250;
ActiveForm.Top := (Counter * 30) mod 150;
// show the form, activate the memo and the child form
ActiveForm.Show;
SendMessage (ActiveForm.Handle, wm_NcActivate, 1, 0);
end;
end.
CHILDF.PAS
unit ChildF;
interface
uses
SysUtils, Windows, Messages, Classes, Graphics, Controls,
Forms, Dialogs, StdCtrls;
type
TChildForm = class(TForm)
Memo1: TMemo;
procedure FormClose(Sender: TObject; var Action: TCloseAction);
private
{ Private declarations }
public
procedure MouseActiv (var Msg: TMessage);
message wm_MouseActivate;
procedure NcActiv (var Msg: TMessage);
message wm_NcActivate;
end;
var
ChildForm: TChildForm;
implementation
uses MainF;
{$R *.DFM}
{ TChildForm }
procedure TChildForm.MouseActiv (var Msg: TMessage);
begin
inherited;
// reset active form
if Assigned (MainForm.ActiveForm) then
SendMessage (MainForm.ActiveForm.Handle, wm_NcActivate, 0, 0);
SendMessage (Handle, wm_NcActivate, 1, 0);
end;
procedure TChildForm.FormClose(Sender: TObject; var Action: TCloseAction);
begin
Action := caFree;
// if destroying the active form, set it to nil
if MainForm.ActiveForm = self then
MainForm.ActiveForm := nil;
end;
procedure TChildForm.NcActiv(var Msg: TMessage);
begin
inherited;
if Msg.wParam <> 0 then
begin
MainForm.ActiveForm := self;
Memo1.SetFocus;
BringToFront;
end;
end;
end.
MAINF.DFM
object MainForm: TMainForm
Left = 213
Top = 159
Width = 597
Height = 404
Caption = 'Main Form'
Color = clBtnFace
Font.Charset = DEFAULT_CHARSET
Font.Color = clWindowText
Font.Height = -13
Font.Name = 'System'
Font.Style = []
Menu = MainMenu1
OldCreateOrder = True
PixelsPerInch = 96
TextHeight = 16
object MainMenu1: TMainMenu
Left = 32
Top = 32
object File1: TMenuItem
Caption = '&File'
object New1: TMenuItem
Caption = '&New'
ShortCut = 16462
OnClick = New1Click
end
end
end
end
CHILDF.DFM
object ChildForm: TChildForm
Left = 240
Top = 187
Width = 342
Height = 209
Caption = 'Child Form'
Color = clBtnFace
Font.Charset = DEFAULT_CHARSET
Font.Color = clWindowText
Font.Height = -11
Font.Name = 'MS Sans Serif'
Font.Style = []
OldCreateOrder = True
OnClose = FormClose
PixelsPerInch = 96
TextHeight = 13
object Memo1: TMemo
Left = 0
Top = 0
Width = 334
Height = 182
Align = alClient
TabOrder = 0
end
end
Copyright Marco Cantù 1998