{ Example and Test of the  wDebug0.pp  unit
  Feel free to use this example as you want.
  First developped by Bernard Gisin
  --------------------------------------------}
program tDebug;

{$APPTYPE GUI}

Uses wDebug0, Windows;

Const
  AppName = 'tDebug';

Var
  glAMessage              : Msg;
  glhWindow               : HWnd;

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

Function WindowProc (hWindow:HWND;AMessage:UINT;WPar:WPARAM;LPar:LPARAM):LRESULT; stdcall; export;
//===============================================================================================
Var
  R           : rect;
  NrMenu      : Longint;
  NotiCode    : LongInt;
  dc          : hdc;
  ps          : paintstruct;
  lMask       : longint;

Begin
WindowProc := 0;

// to write all incomming messages
lMask:=1024;
if (AMessage =  32) or (AMessage = 132) or (AMessage = 512) or
   (AMessage = 160) then lMask:=lMask + 2048;
DBW0(lMask, 'msg= ' + DBMsgToStr(AMessage) + ' = ' + ITS(AMessage));

Case AMessage Of
   WM_paint : begin
      dc:=BeginPaint(hWindow,@ps);
      GetClientRect(hWindow,@r);
      DrawText( dc,'Test of debugging window',-1,@r,
                DT_SINGLELINE or DT_CENTER or DT_VCENTER);
      EndPaint(hWindow,ps);
      Exit;
      end;
//   WM_Close : Begin End;
   WM_Destroy : Begin
      PostQuitMessage (0);
      Exit;
      End;
   WM_Command : Begin
      NotiCode := HiWord(WPar);
      Case NotiCode of
         en_Change : Begin end; // do nothing
      Else // Case NotiCode of
      Begin //Menu item
         NrMenu := LoWord(WPar);
         Case NrMenu Of
                // these calls to DBW0 can be done from anywhere
                100 : DBW0(0, '');  // just open the debugging window
                101 : DBW0(   1,'1 -> a debugging message');
                102 : DBW0(   2,'2 -> another debugging message');
                103 : DBW0(   4,'3 -> A third debugging message');
                104 : DBW0(   8,'4 -> A fourth debugging message');
                105 : DBW0(  16,'5 -> A fifth debugging message');
                106 : DBW0(  32,'6 -> A sixth debugging message');
                107 : DBW0(  64,'7 -> A seventh debugging message');
                108 : DBW0( 128,'8 -> An eigth debugging message');

                200 : PostMessage(hWindow,WM_Close,0,0);
                401 : MessageBox(hWindow,'Help','Not implemented',
                                 MB_OK Or MB_IconInformation);
            End;  // Case NrMenu Of
         End; // Begin //Menu item
         End; // Case NotiCode of
      End; // WM_Command
   End; // Case AMessage Of

WindowProc := DefWindowProc(hWindow,AMessage,WPar,LPar);
End;  // WindowProc

Function WinRegister: Boolean;
//============================
Var
  WindowClass : WndClass;
Begin
DBW0(4096, 'Begin WinRegister');
  With WindowClass Do Begin
      Style         := cs_hRedraw Or cs_vRedraw;
      lpfnWndProc   := WndProc(@WindowProc);
      cbClsExtra    := 0;
      cbWndExtra    := 0;
      hInstance     := system.MainInstance;
      hIcon         := LoadIcon (0,idi_Application);
      hCursor       := LoadCursor (0,idc_Arrow);
      hbrBackground := GetStockObject(WHITE_BRUSH);
//  hbrBackground :=COLOR_BACKGROUND+1; // to have the desktop color
      lpszMenuName  := Nil;
      lpszClassName := AppName;
    End;
  WinRegister := RegisterClass (WindowClass)<>0;
DBW0(4096, 'End WinRegister');
End;  // WinRegister

Function WinCreate: HWnd;
//=======================
Var hWindow : HWnd;
    Menu    : hMenu;
    SubMenu : hMenu;
Begin
// here, no calls to WindowProc have been made.
DBW0(4096, 'Begin WinCreate, before Create Window');

hWindow := CreateWindow ( AppName,'Test Debuging window',ws_OverlappedWindow,
//                          cw_UseDefault,cw_UseDefault,cw_UseDefault,cw_UseDefault,
                          10,20, 300, 300,
                          0,0,MainInstance,Nil);

// here, some calls to WindowProc have been made.
DBW0(4096, 'In WinCreate, just after Create Window');
DBW0(4096, 'hWindow = ' + ITS(hWindow));

  If hWindow<>0 Then
    Begin
      Menu := CreateMenu;
      SubMenu := CreateMenu;
      AppendMenu(Submenu,MF_STRING,100,'&Open debug window');
      AppendMenu(Submenu,MF_STRING,101,'&1 A debug msg');
      AppendMenu(Submenu,MF_STRING,102,'&2 A 2nd debug msg');
      AppendMenu(Submenu,MF_STRING,103,'&3 A 3rd debug msg');
      AppendMenu(Submenu,MF_STRING,104,'&4 A 4th debug msg');
      AppendMenu(Submenu,MF_STRING,105,'&5 A 5th debug msg');
      AppendMenu(Submenu,MF_STRING,106,'&6 A 6th debug msg');
      AppendMenu(Submenu,MF_STRING,107,'&7 A 7th debug msg');
      AppendMenu(Submenu,MF_STRING,108,'&8 A 8th debug msg');
      AppendMenu(Submenu,MF_SEPARATOR,0,Nil);
      AppendMenu(SubMenu,MF_String,200,'E&xit');
      AppendMenu(Menu,MF_POPUP,SubMenu,'&File');
      SubMenu := CreateMenu;
      AppendMenu(Menu,MF_STRING,401,'&Help');
      SetMenu(hWindow,menu);
      ShowWindow(hWindow,SW_Show);
    ShowWindow(hWindow, CmdShow);

      UpdateWindow(hWindow);
    End;
  WinCreate := hWindow;

DBW0(4096, 'End of WinCreate');
End;  // WinCreate

Begin // MAIN **************************************
If Not WinRegister Then Begin
   MessageBox (0,'Register failed',Nil, mb_Ok);
   End
Else Begin
   glhWindow := WinCreate;

   If longint(glhWindow)=0 Then Begin
      MessageBox (0,'WinCreate failed',Nil,MB_OK);
      End
   Else Begin
      While GetMessage(@glAMessage,0,0,0) Do Begin
         TranslateMessage(glAMessage);
         DispatchMessage(glAMessage);
         End;
      Halt(glAMessage.wParam);
      End;
   End;
End.

