winhello.pp 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. {
  2. Copyright (c) 1996 by Charlie Calvert
  3. Modifications by Florian Klaempfl
  4. Standard Windows API application written in Object Pascal.
  5. No VCL code included. This is all done on the Windows API
  6. level.
  7. }
  8. {$APPTYPE GUI}
  9. {$MODE DELPHI}
  10. program WinHello;
  11. uses
  12. Strings, Windows;
  13. const
  14. AppName = 'WinHello';
  15. function WindowProc(Window: HWnd; AMessage: UINT; WParam : WPARAM;
  16. LParam: LPARAM): LRESULT; stdcall; export;
  17. var
  18. dc : hdc;
  19. ps : paintstruct;
  20. r : rect;
  21. begin
  22. WindowProc := 0;
  23. case AMessage of
  24. wm_paint:
  25. begin
  26. dc:=BeginPaint(Window,@ps);
  27. GetClientRect(Window,@r);
  28. DrawText(dc,'Hello world by Free Pascal',-1,@r,
  29. DT_SINGLELINE or DT_CENTER or DT_VCENTER);
  30. EndPaint(Window,ps);
  31. Exit;
  32. end;
  33. wm_Destroy:
  34. begin
  35. PostQuitMessage(0);
  36. Exit;
  37. end;
  38. end;
  39. WindowProc := DefWindowProc(Window, AMessage, WParam, LParam);
  40. end;
  41. { Register the Window Class }
  42. function WinRegister: Boolean;
  43. var
  44. WindowClass: WndClass;
  45. begin
  46. WindowClass.Style := cs_hRedraw or cs_vRedraw;
  47. WindowClass.lpfnWndProc := WndProc(@WindowProc);
  48. WindowClass.cbClsExtra := 0;
  49. WindowClass.cbWndExtra := 0;
  50. WindowClass.hInstance := system.MainInstance;
  51. WindowClass.hIcon := LoadIcon(0, idi_Application);
  52. WindowClass.hCursor := LoadCursor(0, idc_Arrow);
  53. WindowClass.hbrBackground := GetStockObject(WHITE_BRUSH);
  54. WindowClass.lpszMenuName := nil;
  55. WindowClass.lpszClassName := AppName;
  56. Result := RegisterClass(WindowClass) <> 0;
  57. end;
  58. { Create the Window Class }
  59. function WinCreate: HWnd;
  60. var
  61. hWindow: HWnd;
  62. begin
  63. hWindow := CreateWindow(AppName, 'Hello world program',
  64. ws_OverlappedWindow, cw_UseDefault, cw_UseDefault,
  65. cw_UseDefault, cw_UseDefault, 0, 0, system.MainInstance, nil);
  66. if hWindow <> 0 then begin
  67. ShowWindow(hWindow, CmdShow);
  68. ShowWindow(hWindow, SW_SHOW);
  69. UpdateWindow(hWindow);
  70. end;
  71. Result := hWindow;
  72. end;
  73. var
  74. AMessage: Msg;
  75. hWindow: HWnd;
  76. begin
  77. if not WinRegister then begin
  78. MessageBox(0, 'Register failed', nil, mb_Ok);
  79. Exit;
  80. end;
  81. hWindow := WinCreate;
  82. if longint(hWindow) = 0 then begin
  83. MessageBox(0, 'WinCreate failed', nil, mb_Ok);
  84. Exit;
  85. end;
  86. while GetMessage(@AMessage, 0, 0, 0) do begin
  87. TranslateMessage(AMessage);
  88. DispatchMessage(AMessage);
  89. end;
  90. Halt(AMessage.wParam);
  91. end.