program APICommonControl;

uses
  Windows,
  CommCtrl,
  Messages;

const AppName = 'API Window';

var AMessage  : TMsg;
    hWindow   : HWnd;
    hBtnExit  : HWnd;
    hBtnAbout : HWnd;
    hListView : HWnd;
    hDate     : HWnd;
    hStatusBar: HWnd;

function WindowProc(Window:HWnd; AMessage, WParam, LParam:longint):longint; stdcall; export;
begin
 WindowProc := 0;
 case AMessage of
  WM_DESTROY: begin PostQuitMessage(0); Exit; end;
  WM_COMMAND: if lParam = hBtnExit then begin PostQuitMessage(0); Exit; end
               else if LParam = hBtnAbout then MessageBox(Window,
                'Вікно, створене функціями API, без використання VCL Delphi.' + #13#13 +
                'Copyright© Microsoft... і т.д.', 'API Window', MB_OK or MB_ICONINFORMATION);
  WM_KEYDOWN: if (wParam = VK_RETURN) or (wParam = VK_ESCAPE) then SendMessage(hBtnExit, BM_CLICK, 0, 0)
               else if wParam = VK_F1 then SendMessage(hBtnAbout, BM_CLICK, 0, 0);
 end;
 WindowProc := DefWindowProc(Window, AMessage, WParam, LParam);
end;

function WinRegister: boolean;
var WindowClass: TWndClass;
begin
 with WindowClass do begin
  Style := CS_HREDRAW or CS_VREDRAW;
  lpfnWndProc := @WindowProc;
  cbClsExtra := 0;
  cbWndExtra := 0;
  hInstance := SysInit.HInstance;
  hIcon := LoadIcon(0, IDI_APPLICATION);
  hCursor := LoadCursor(0, IDC_ARROW);
  hbrBackGround := COLOR_BTNSHADOW;
  lpszMenuName := nil;
  lpszClassName := AppName;
 end;
 Result := RegisterClass(WindowClass) <> 0;
end;

function WinCreate: HWnd;
var hWindow : HWnd;
begin
 hWindow := CreateWindow(AppName, 'Вікно, створене використанням API', WS_OVERLAPPEDWINDOW,
  100, 100, 400, 200, 0, 0, HInstance, nil);

 if hWindow <> 0 then
 begin
  ShowWindow(hWindow, SW_SHOWNORMAL);
  UpdateWindow(hWindow);
  hBtnExit := CreateWindow('BUTTON', 'Вихід', WS_CHILD or BS_DEFPUSHBUTTON or WS_TABSTOP,
    300, 10, 90, 30, hWindow, 0, HInstance, nil);
  if hBtnExit <> 0 then ShowWindow(hBtnExit, SW_SHOWNORMAL);
  hBtnAbout := CreateWindow('BUTTON', 'Про вікно...', WS_CHILD or WS_TABSTOP,
    300, 75, 90, 30, hWindow, 0, HInstance, nil);
  if hBtnAbout <> 0 then ShowWindow(hBtnAbout, SW_SHOWNORMAL);

  hListView := CreateWindow(TRACKBAR_CLASS, '', WS_VISIBLE or WS_CHILD or TBS_HORZ,
    10, 10, 250, 40, hWindow, 0, HInstance, 0);
  hDate := CreateWindow(DATETIMEPICK_CLASS, '', WS_VISIBLE or WS_CHILD,
    10, 70, 100, 20, hWindow, 0, HInstance, 0);
  hStatusBar := CreateWindow(STATUSCLASSNAME, '', WS_VISIBLE or WS_CHILD,
    0, 0, 0, 0, hWindow, 0, HInstance, 0);
 end;
 Result := hWindow;
end;

begin
 if not WinRegister then begin
  MessageBox(0,'Клас вікна не зареєстровано', AppName, MB_OK);
  Exit;
 end;
 hWindow := WinCreate;
 if hWindow = 0 then begin
  MessageBox(0, 'Не вийшло створити вікно.', AppName, MB_OK);
  Exit;
 end;
 while GetMessage(AMessage, 0, 0, 0) do begin
  TranslateMessage(AMessage);
  DispatchMessage(AMessage);
 end;
 Halt(AMessage.wParam);
end.
