{ Second example of program using the units
  wApplication0.pp  and  wWindow0.pp
  This example shows how to react to events,
  and how to write text in the main window
  --------------------------------------------}
program tEx002;

// To tell FreePascal that the application is a Graphical User Interface
// i.e. is running in windows, not in text mode.
{$APPTYPE GUI}

Uses
  Windows
 ,wApplication0
 ,wWindow0
 ,wDebug0;

type
  PMyWindow = ^TMyWindow;
  TMyWindow = object(TWindow)
  //"""""""""""""""""""""""""
  constructor Init(strWinTitlePrm:ShortString);
  procedure WMPaint;
  procedure WMLButtonDown;
  procedure WMRButtonDown;
  function WindowProc(hWindow:HWND; wMsg:UINT; wPar:WPARAM; lPar:LPARAM):LRESULT; virtual;
  end; // TMyWindow = object(TWindow0)

// var nNotUsed : integer; // only to avoid an error message while compliling in DevPascal 1.9

constructor TMyWindow.Init(strWinTitlePrm:ShortString);
//=====================================================
begin
inherited Init(strWinTitlePrm);
end;  // TMyWindow.Init

procedure TMyWindow.WMPaint;
//==========================
Var
  rRec        : rect;
  dc          : hdc;
  ps          : paintstruct;

begin
dc:=BeginPaint(hWin, @ps);
GetClientRect(hWin, @rRec);
DrawText( dc, 'Example 002 using wApplication and wWindow',-1,@rRec,
          DT_SINGLELINE or DT_CENTER or DT_VCENTER);
rRec.Top :=rRec.top    + 50;
DrawText( dc, 'Click with the mouse',-1,@rRec,
          DT_SINGLELINE or DT_CENTER or DT_VCENTER);
EndPaint(hWin, ps);
end;  // TMyWindow.WMPaint

procedure TMyWindow.WMLButtonDown;
//================================
begin
MessageBox(hWin, 'You have pressed the left mouse button',
    'Message Dispatched', mb_Ok);
end;  // TMyWindow.WMLButtonDown

procedure TMyWindow.WMRButtonDown;
//================================
begin
MessageBox(hWin, 'You have pressed the right mouse button',
    'Message Dispatched', mb_Ok);
end;  // TMyWindow.WMRButtonDown

function TMyWindow.WindowProc(hWindow:HWND; wMsg:UINT; wPar:WPARAM; lPar:LPARAM):LRESULT;
//=======================================================================================
// All window messages are send here.

Begin
WindowProc := 0; // Default return value when processing a message

Case wMsg Of
   WM_paint : WMPaint;
//   WM_Destroy : PostQuitMessage (0); // This is the default behaviour.
//              This should not be treated here, or at least the
//              inherited WindowProc should be called.
//              This allows to save the window position and size in a file
   WM_LButtonDown : WMLButtonDown;
   WM_RButtonDown : WMRButtonDown;

   else begin // Case wMsg Of
      // Default processing of the messages.
      WindowProc:=inherited WindowProc(hWindow, wMsg, wPar, lPar);
      end; // Case wMsg Of
   End; // Case wMsg Of

End;  // TMyWindow.WindowProc

var
  oApp        : TApplication;
  poMyMainWin : PMyWindow;

Begin // MAIN ******
// Name under which the main window is registered. Spaces are not allowed
oApp.Init('Example002');

// Creation of the main window
new(poMyMainWin, Init('Title of the Example002 (Ex002.pp) Window'));

{ You could change the default position and size of the window here,
  but it is not a good idea, since position and size will be the
  same as they were when the windo was closed
// To change the default position and size of the window when created.
// Normally it is better to do it in the TMyWindow.init part
with poMyMainWin^ do begin
   nLeft   :=5;
   nTop    :=50;
   nWidth  :=400;
   nHeight :=200;
   end;
---------------}

// The App.WindowClass can be modified here, if wanted. (Not usual)
oApp.Run;
oApp.Done;  // The main window object is disposed here.
End.

