Program Demo1;
uses WinTypes, WinProcs, OWindows;
{Tells the compiler which libraries are needed}
type
PTheWindow = ^TTheWindow;
{creates a pointer ready for the main window}
TTheWindow = object(TWindow)
{assigns the pointer to the window}
procedure Paint(PaintDC: HDC;
var PaintInfo: TPaintStruct); virtual;
end;
{ The procedure Paint is included because }
{Windows frequently hides one}
{ window behind another. Paint recreates }
{what has been covered over}
TTheApplication = object(TApplication)
procedure InitMainWindow; virtual;
end;
procedure TTheWindow.Paint;
begin
{later, we'll draw certain graphics }
{by adding to this procedure}
end;
procedure TTheApplication.InitMainWindow;
begin
MainWindow := New(PTheWindow, Init(nil, 'A Program'));
{Create the window}
end;
var
TheApp: TTheApplication;
begin
TheApp.Init('A Program');
TheApp.Run;
TheApp.Done;
end.
Run the program and it will display a blank window.
Click the control window in the top
right hand corner of the window (for Windows 3.0 and 3.1)
and select close.
What happened? Our graphics program created a window
OBJECT. The object was
based on ObjectWindows through the line:
TTheWindow = object(TWindow)
This works because ObjectWindows acts as the parent
and TTheWindow is the child,
inheriting the behaviour of the TWindow parent. So, when
we tell TTheWindow to do
something, like Paint:
procedure TTheWindow.Paint;
TTheWindow calls the parent, TWindow, and waits until
the parent has sorted everything
out before the child, TTheWindow, will do anything else.
TTheWindow is descended from TWindow - it cannot
function without TWindow being
able to sort out the things that TWindow is good at
sorting out. (Namely, creating and
controlling the window and user input/output.) At
the moment, TTheWindow has learnt
nothing about doing anything for itself - that will
come with later versions of the program.
Exactly the same happens with TApplication and
TheApp. In this case, the child (TheApp)
asks the parent to initialise the application
(TheApp.Init('A Program')), and Run the new
application. Finally, TheApp simply tells the
parent that the job is Done.