unit hello;
{$mode objfpc}{$H+}
interface
uses
Windows, Classes, SysUtils;
type
{ THello }
THello = class(TObject)
private
someText: string;
function WindowProc(hWnd: HWND; Msg: UINT; WPARAM: WPARAM;
LPARAM: LPARAM): LRESULT; stdcall;
public
constructor Create;
destructor Destroy; override;
procedure Run;
end;
implementation
function dummy(hWnd: HWND; Msg: UINT; WPARAM: WPARAM; LPARAM: LPARAM): LRESULT; stdcall;
var
vData: Pointer;
begin
vData := Pointer(GetWindowLongPtr(hWnd, GWLP_USERDATA));
Result := THello(VData).WindowProc(hWnd, Msg, WPARAM, LPARAM);
end;
{ THello }
function THello.WindowProc(hWnd: HWND; Msg: UINT; WPARAM: WPARAM;
LPARAM: LPARAM): LRESULT; stdcall;
begin
case (Msg) of
WM_CREATE:
begin
MessageBox(0, 'Hello world!', PChar(someText), MB_OK or MB_USERICON);
end;
WM_CLOSE:
begin
PostQuitMessage(0);
Result := 0;
end;
else
Result := DefWindowProc(hWnd, Msg, WParam, LParam);
end;
end;
constructor THello.Create;
var
WindowClass: WndClass;
hWindow: HWnd;
begin
someText := 'Hello';
WindowClass.Style := cs_hRedraw or cs_vRedraw;
WindowClass.lpfnWndProc := @dummy;
WindowClass.cbClsExtra := 0;
WindowClass.cbWndExtra := 0;
WindowClass.hInstance := system.MainInstance;
WindowClass.hIcon := LoadIcon(0, idi_Application);
WindowClass.hCursor := LoadCursor(0, idc_Arrow);
WindowClass.hbrBackground := GetStockObject(WHITE_BRUSH);
WindowClass.lpszMenuName := nil;
WindowClass.lpszClassName := PChar(someText);
if Windows.RegisterClass(WindowClass) = 0 then
begin
halt(1);
end;
hWindow := CreateWindow(PChar(someText), 'Hello world program',
ws_OverlappedWindow, cw_UseDefault, cw_UseDefault, cw_UseDefault,
cw_UseDefault, 0, 0, system.MainInstance, nil);
if hWindow = 0 then
begin
halt(1);
end;
SetWindowlongPtr(hWindow, GWLP_USERDATA, LONG_PTR(self));
ShowWindow(hWindow, CmdShow);
ShowWindow(hWindow, SW_SHOW);
UpdateWindow(hWindow);
end;
destructor THello.Destroy;
begin
inherited Destroy;
end;
procedure THello.Run;
var
AMessage: Msg;
begin
while GetMessage(@AMessage, 0, 0, 0) do
begin
TranslateMessage(AMessage);
DispatchMessage(AMessage);
end;
Halt(AMessage.wParam);
end;
end.