program API;
uses
Windows, Messages, SHFolder, inifiles, SysUtils;
type
TSForm =
class
private
run: Boolean;
ClassNamen: PAnsiChar;
windowClass: TWndClass;
Winhandle: HWND;
AppName: PAnsiChar;
w, h: Integer;
msg: TMsg;
procedure finale;
procedure init;
procedure ShowError(ErrorText: PChar);
function getAppData() :
String;
procedure keypress(c : Char);
protected
//
public
constructor Create();
procedure Show();
destructor Close();
end;
var
win : TSForm;
procedure TSForm.finale;
begin
//
end;
function WindowProcedure(Winhandle: HWND; uMsg: UINT; wParam: wParam; lParam: LParam):
lresult;
stdcall;
var
x, y : integer;
begin
Result := 0;
case uMsg
of
WM_CREATE:
begin
x := GetSystemMetrics(SM_CXSCREEN);
y := GetSystemMetrics(SM_CYSCREEN);
MoveWindow(Winhandle, (x
div 2) - (win.w
div 2),
(y
div 2) - (win.h
div 2),
win.w, win.h, true);
end;
WM_DESTROY:
begin
win.run := false;
win.finale();
PostQuitMessage(0);
end;
WM_KEYDOWN:
begin
win.KeyPress(Char(Word(wParam)));
end;
else
Result := DefWindowProc(Winhandle, uMsg, wParam, lParam);
end;
end;
destructor TSForm.Close;
begin
UnregisterClass(windowClass.lpszClassName, hInstance);
end;
constructor TSForm.Create;
begin
w := 640;
h := 400;
end;
function TSForm.getAppData:
String;
var
path :
array[0..MAX_PATH]
of char;
begin
if SUCCEEDED(SHGetFolderPath(0, CSIDL_LOCAL_APPDATA, 0, 0, PChar(path[0])))
then
result := path
else
result := '
';
end;
procedure TSForm.init;
begin
run := true;
end;
procedure TSForm.keypress(c: Char);
begin
//
end;
procedure TSForm.Show;
begin
//
end;
procedure TSForm.ShowError(ErrorText: PChar);
begin
MessageBox(Winhandle, ErrorText, '
Error', MB_ICONERROR);
Halt;
end;
begin
win := TSForm.Create();
win.appName := '
WinAPITestApp';
win.ClassNamen := '
WinAPITestWindow';
win.windowClass.hInstance := hInstance;
with win.windowClass
do
begin
Style := CS_HREDRAW
or CS_VREDRAW;
lpfnWndProc := @WindowProcedure;
cbClsExtra := 0;
cbWndExtra := 0;
hbrBackground := COLOR_APPWORKSPACE;
lpszMenuName :=
nil;
lpszClassName := win.ClassNamen;
hIcon := LoadIcon(0, IDI_WINLOGO);
hCursor := LoadCursor(0, IDC_ARROW);
end;
if RegisterClass(win.windowClass) = 0
then
win.ShowError(PChar('
#' + IntToStr(GetLastError) + '
: ' + SysErrorMessage(GetLastError)));
win.WinHandle := CreateWindow(win.windowClass.lpszClassName, win.AppName, WS_CAPTION
or WS_VISIBLE
or WS_SYSMENU
or WS_MINIMIZEBOX
or WS_MAXIMIZEBOX
or WS_SIZEBOX, 400,
400, win.w, win.h, 0, 0, hInstance,
nil);
if win.WinHandle <> 0
then
begin
win.init();
while win.run
do
begin
if PeekMessage(win.msg, win.WinHandle, 0, 0, PM_REMOVE)
then
begin
TranslateMessage(win.msg);
DispatchMessage(win.msg);
end;
end;
end else
win.ShowError(PChar('
#' + IntToStr(GetLastError) + '
: ' + SysErrorMessage(GetLastError)));
ExitCode := win.msg.wParam;
win.Show();
win.Close();
end.