Previous: 6.4. Associating a Window Procedure with a Window Class To the Table of Contents Next: 6.6. Writing Win32 Control Panel Applications
6.4. Associating a Window Procedure with a Window Class Table of Contents 6.6. Writing Win32 Control Panel Applications

- 6.5. -
Table of Contents
6. Win32 Programming
6.5. Example of Win32 GUI Application


6.5. Example of Win32 GUI Application


program HelloW;
 
{$ifndef __WIN32__}
  {$define INVALID_TARGET}
{$endif}
{$ifndef __GUI__}
  {$define INVALID_TARGET}
{$endif}
{$ifdef INVALID_TARGET}
  This program must be compiled for Win32 GUI target only
{$endif}

uses
  Windows, MMSystem, Messages;

function MyWndProc conv arg_stdcall
                           (Window: HWND;
                            Mess: UINT;
                            Wp: WParam;
                            Lp: LParam): LRESULT;
begin
  case Mess of   
 WM_PAINT:       begin

                   declare

                     var
 
                       DC: hDC;
                       ps: TPaintStruct;
                     begin
 
                       DC := BeginPaint(Window, ps);
                       TextOut(DC, 0, 0, 'Hello World!', 12); 
                       EndPaint(Window, ps);
                       Result := 0;
                     end;  
                 end; 
     WM_DESTROY: begin

                   PostQuitMessage(0);
                   Result := 0;
                 end;

 WM_LBUTTONDOWN: begin

                   MessageBox(Window, 'This is my message!',
                              'My message box', MB_OK);
                   Result := 0;
                 end;
           else   
             Result := DefWindowProc(Window, Mess, Wp, Lp);
  end;
end; 

var
  wc : TWndClass;
  wnd: HWnd;
  Msg: TMsg;
begin
  FillChar(wc, SizeOf(wc), 0);
  with wc do begin

    style:=CS_HREDRAW + CS_VREDRAW;
    lpfnWndProc := @MyWndProc;
    cbClsExtra := 0;
    cbWndExtra := 0;
    hInstance := System.hInstance;
    hIcon := LoadIcon(THandle(NIL), IDI_APPLICATION);
    hCursor := LoadCursor(THandle(NIL), IDC_ARROW);
    hbrBackGround := COLOR_WINDOW+1;
    lpszMenuName := nil;
    lpszClassName := 'HelloWorld';
  end;

  if RegisterClass(wc) = 0 then begin
    Exit;
  end;

  wnd := CreateWindow(wc.lpszClassName,
                      'GUI Application Demo',
                      WS_OVERLAPPEDWINDOW,
                      CW_USEDEFAULT,
                      0,
                      CW_USEDEFAULT,
                      0, 0, 0,
                      HInstance,
                      NIL);
 
  ShowWindow(wnd, SW_RESTORE);
  UpdateWindow(wnd);
 
  while GetMessage(Msg,0,0,0) do begin

    TranslateMessage(Msg);
    DispatchMessage(Msg);
  end;
 
end.





Previous: 6.4. Associating a Window Procedure with a Window Class To the Table of Contents Next: 6.6. Writing Win32 Control Panel Applications
6.4. Associating a Window Procedure with a Window Class Table of Contents 6.6. Writing Win32 Control Panel Applications

- 6.5. -