Simple windows program

This is a simple program to show how a windows program is written from scratch in BP (with no OWL).  There is also an example of this in \delphi\demos\generic.

Note:  Click here for the 32 bit version.

{Lloyd Linklater;  2-6-95}
{c:\bp\examples\win\generic.pas has much of this in it.}
program WinClk;
{$R sumthing}  {Resource file is sumthing.res}
uses WinTypes, WinProcs;
const
  AppName = 'WinClk';

{****************************}
{The export is used to force the far call model and to generate
special entry code so that it can be called by Windows.}

function About(Dialog: HWnd; Message, WParam: Word;
               LParam: Longint): Bool; export;
begin
  About := True;
  case Message of
    wm_InitDialog: Exit;
    wm_Command: if (WParam = id_Ok) or (WParam = id_Cancel) then
    begin
      EndDialog(Dialog, 1);
      Exit;
    end;
  end;
  About := False;
end;  {About}

{****************************}

function WindowProc(Window: HWnd; Message, WParam: Word;
                    LParam: Longint): Longint; export;
var
  AboutProc: TFarProc;
begin
  WindowProc := 0;
  case Message of
{The WM_COMMAND message is sent to a window when the user selects
an item from a menu, when a control sends a notification
message to its parent window, or when an accelerator keystroke
is translated. }
    wm_Command: case WParam of
      301 : {Help | About selected from the menu.}
        begin
          AboutProc := MakeProcInstance(@About, HInstance);
          DialogBox(HInstance, 'AboutBox', Window, AboutProc);
          FreeProcInstance(AboutProc);
          Exit;
        end;
      101 : {EXIT selected from the menu.}
        begin
          PostQuitMessage(0); {Puts a wm_Quit message on the queue.}
          halt;
        end;
      end;
    wm_Destroy:
      begin
        PostQuitMessage(0);
        Exit;
      end;
  end;  {case Message}
  WindowProc := DefWindowProc(Window, Message, WParam, LParam);
end;  {WindowProc}

{****************************}
{This MUST be called WinMain.}

procedure WinMain;
var
  Window: HWnd;
  Message: TMsg;
const
  WindowClass: TWndClass = (
    style:         0;
    lpfnWndProc:   @WindowProc; {Function pointer to the message handling code.}
    cbClsExtra:    0;
    cbWndExtra:    0;
    hInstance:     0;
    hIcon:         0;
    hCursor:       0;
    hbrBackground: 0;
    lpszMenuName:  'MyFirst';
    lpszClassName: AppName);
begin
  if HPrevInst = 0 then {If there is not another copy running then...}
  begin {...a window class must be declared since it is not declared already.}
    WindowClass.hInstance := HInstance;
    WindowClass.hIcon := LoadIcon(hInstance, 'ICON_1');
    WindowClass.hCursor := LoadCursor(0, idc_Arrow);
    WindowClass.hbrBackground := GetStockObject(white_Brush);
    if not RegisterClass(WindowClass) then Halt(255);
  end;
  Window := CreateWindow(AppName,               {Class name}
                         'Windoze Clock',      {Window name}
                         ws_OverlappedWindow,  {style}
                         cw_UseDefault,        {X}
                         cw_UseDefault,        {Y}
                         cw_UseDefault,        {Width}
                         cw_UseDefault,        {Height}
                         0,                    {WndParent}
                         0,                    {Menu}
                         HInstance,            {Instance}
                         nil);                 {structure creation parameter}

  {CmdShow is used only when ShowWindow is used to display the
  app's main window.  Otherwise it uses one of the sw_ constants.}
  ShowWindow(Window, CmdShow);
  UpdateWindow(Window);

  {Messages are not sent directly to the app by windows, so we must use
  the OBLIGATORY message loop to keep getting messages and using them
  until we get the 'go away now' message.  If the message is WM_QUIT, then
  GetMessage() returns a 0.  Message is of type TMsg.
    TMsg = record
      hwnd: HWnd;
      message: Word;
      wParam: Word;
      lParam: LongInt;
      time: Longint;
      pt: TPoint;
    end;}
  while GetMessage(Message, 0, 0, 0) do
  begin
    {This translates virtual-key messages into character messages.}
    TranslateMessage(Message);
    DispatchMessage(Message); {The translated message is now 'mailed' out.}
  end;
  Halt(Message.wParam);
end;  {WinMain}

{****************************}

begin
  WinMain; {By this time it is simple <G>}
end.