PROGRAM   CharTb03 ;
{$IFDEF VER70}{$IFDEF Windows}   USES   WinTypes , WinProcs , Strings ;   {$ENDIF}{$ENDIF}
{$IFDEF VER80}   USES   Messages ,  WinTypes , WinProcs , SysUtils ;   {$ENDIF}      {--- Delphi1 }
{$IFDEF WIN32}   USES   Messages ,  Windows , SysUtils ;   {$WRITEABLECONST ON}   {$ENDIF}      {--- Delphi2,... }

CONST
   wm_MyAction = wm_User + 101 ;

VAR
   G_MainPaintedOnceQ : Boolean ;
   G_Buffer : ARRAY[0..4096] of Char ;

FUNCTION   MainWindow_Proc   ( I_HWnd : HWnd
                             ; I_Message : {$IFDEF WIN32}Longint{$ELSE}Word{$ENDIF}
                             ; I_WParam  : {$IFDEF WIN32}Longint{$ELSE}Word{$ENDIF}
                             ; I_LParam  : Longint ) : Longint ;  {$IFDEF WIN32}StdCall;{$ELSE}Export;{$ENDIF}
CONST
   C_MainMsg : ARRAY[0..13] of Char = 'CharTb03..., '  +  #0 ;

   C_CRLFz : ARRAY[0..2] of Char = #13#10  +  #0 ;
VAR
   L_HDC         : HDC ;
   L_PaintStruct : TPaintStruct ;
   L_Rect        : TRect ;
   L_PREV_GdiObj : THandle ;

   L_P      : PChar ;
   L_Buf25  : ARRAY[0..25] of Char ;
   L_Buf01z : ARRAY[0..1] of Char ;

   i : Longint ;
BEGIN
   CASE I_Message OF
      wm_MyAction : BEGIN
         L_P := @G_Buffer ;
         L_P^:= #0 ;

         L_Buf01z[1] := #0 ;

         {----- LOOP , On each char }
         FOR i := 0 TO 255 DO BEGIN
            Str ( i , L_Buf25 );
            L_P := StrECopy ( L_P ,  L_Buf25  );
            L_P := StrECopy ( L_P ,  '   '    );

            L_Buf01z[0] := Chr(i);
            L_P := StrECopy ( L_P ,  L_Buf01z  );

            L_P := StrECopy ( L_P ,  C_CRLFz  );
         END;{i}

         MessageBox ( I_HWnd , G_Buffer , 'Information' , MB_OK + MB_IconInformation );
      END;

      WM_PAINT : BEGIN
         L_HDC := BeginPaint ( I_HWnd , L_PaintStruct );
         GetClientRect ( I_HWnd , L_Rect );

         L_PREV_GdiObj := SelectObject ( L_HDC , GetStockObject(SYSTEM_FIXED_FONT) );

         DrawText ( L_HDC , C_MainMsg , SizeOf(C_MainMsg)-1 , L_Rect , DT_WordBreak );

         SelectObject ( L_HDC , L_PREV_GdiObj );
         EndPaint ( I_HWnd , L_PaintStruct );

         {----- Only once... }
         IF not G_MainPaintedOnceQ THEN BEGIN
            G_MainPaintedOnceQ := TRUE ;
            PostMessage ( I_HWnd , wm_MyAction , 0 , 0 );      {--- "Indirect" call... }
         END;
      END;{of}

      WM_SIZE : BEGIN      {--- When window is resized,... }
         {----- Force paint again }
         GetClientRect  ( I_HWnd , L_Rect );
         InvalidateRect ( I_HWnd , @L_Rect , False );
      END;

      WM_DESTROY : PostQuitMessage ( 0 );

      ELSE BEGIN
         MainWindow_Proc := DefWindowProc ( I_HWnd , I_Message , I_WParam , I_LParam );
         EXIT ;
      END;
   END;{case}

   {----- Default result : Msg processed!!! }
   MainWindow_Proc := 0 ;
END;

{===============}

CONST
   C_MainWindow_ClassName = 'CharTb03' + #0 ;

   C_MainWndClass : TWndClass = (
      style       : 0 ;
      lpfnWndProc : @MainWindow_Proc ;
      cbClsExtra  : 0 ;
      cbWndExtra  : 0 ;
      hInstance   : 0 ;
      hIcon         : 0 ;
      hCursor       : 0 ;
      hbrBackground : 0 ;
      lpszMenuName  : #0 ;      {--- Must match with ressource name }
      lpszClassName : C_MainWindow_ClassName
   );

VAR
   G_MainWindow : HWnd ;

   G_Message : TMsg ;

BEGIN
   {----- 1. Register  MainWindow  class }
   IF {System.}HPrevInst = 0 THEN BEGIN
      C_MainWndClass.hInstance     := {System.}HInstance ;
      C_MainWndClass.hIcon         := LoadIcon ( 0 , idi_Application );
      C_MainWndClass.hCursor       := LoadCursor ( 0 , idc_Arrow );
      C_MainWndClass.hbrBackground := GetStockObject ( WHITE_BRUSH );

      IF Boolean(RegisterClass(C_MainWndClass)) = False THEN BEGIN
         MessageBox ( GetFocus , 'Can not register Class.' , 'ERROR' , MB_OK or MB_ICONEXCLAMATION );
         HALT ( 255 );
      END;
   END;

   {----- Prepare  MainWindow  global parameters }
   G_MainPaintedOnceQ := False ;

   {----- 2. Create  MainWindow  }
   G_MainWindow := CreateWindow (
                        C_MainWindow_ClassName             {ClassName}
                      , 'CharTb03...'                      {WindowName}
                      , ws_OverlappedWindow                {Style}
                      , cw_UseDefault , cw_UseDefault      {X,Y}
                      , cw_UseDefault , cw_UseDefault      {Width,Height}
                      , 0                                  {HWndParent}
                      , 0                                  {HMenu}
                      , {System.}HInstance                 {HInstance}
                      , nil                                {Param}
                      );

   {----- 3. Show  MainWindow  }
   ShowWindow ( G_MainWindow , SW_SHOW );
   UpdateWindow ( G_MainWindow );

   {----- LOOP , Process messages }
   WHILE GetMessage ( G_Message , 0 , 0 , 0 ) DO BEGIN
      TranslateMessage ( G_Message );
      DispatchMessage ( G_Message );
   END;

   Halt ( G_Message.wParam );
END.
