1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
| type
TController = class (TObject)
Hwnd: THandle;
HwndParent: THandle;
constructor Create(X:integer; Y:integer; Width:integer; Height:integer; Brush: HBrush; Caption:string; Style:integer; StyleEx:integer; HwndParent:THandle);
destructor Destroy; override;
procedure Close;
function OnClose: Boolean; virtual;
function OnCreate: Boolean; virtual;
function OnDestroy: Boolean; virtual;
end;
type
TWin = class (TController)
end;
var
Win: TWin;
function WinProc (Hwnd:THandle; message:integer; wParam:WPARAM; lParam:LPARAM) : LRESULT; stdcall;
var Ctrl: TController;
P: Pointer;
begin
Ctrl := TController ( GetWindowLong (Hwnd, GWL_USERDATA) );
case message of
WM_NCCREATE: begin
with PCreateStruct (lParam)^ do Ctrl := TController(lpCreateParams);
SetWindowLong (Hwnd, GWL_USERDATA, integer(Ctrl));
Result := DefWindowProc(Hwnd, message, wParam, lParam);
end;
WM_CLOSE: begin
if Ctrl.OnClose then Result := 0 else Result := DefWindowProc(Hwnd, message, wParam, lParam);
end;
WM_CREATE: begin
Ctrl.Hwnd := Hwnd;
if Ctrl.OnCreate then Result := 0 else Result := DefWindowProc(Hwnd, message, wParam, lParam);
end;
WM_DESTROY: begin
if Ctrl.OnDestroy then Result := 0 else Result := DefWindowProc(Hwnd, message, wParam, lParam);
Ctrl.Free;
end;
else Result := DefWindowProc(Hwnd, message, wParam, lParam);
end;
end;
constructor TController.Create(X:integer; Y:integer; Width:integer; Height:integer; Brush: HBrush; Caption:string; Style:integer; StyleEx:integer; HwndParent:THandle);
var WndClass: TWndClassEx;
s: string;
begin
inherited Create;
Self.HwndParent := HwndParent;
s := ClassName;
if GetClassInfoEx(hInstance, PChar(s), WndClass) = False then
begin
WndClass.cbSize := SizeOf (TWndClassEx);
WndClass.style := CS_DBLCLKS or CS_HREDRAW or CS_OWNDC or CS_VREDRAW;
WndClass.lpfnWndProc := @WinProc;
WndClass.cbClsExtra := 0;
WndClass.cbWndExtra := 0;
WndClass.hInstance := hInstance;
WndClass.hIcon := LoadIcon (hInstance, 'MAINICON');
WndClass.hCursor := LoadCursor (0, IDC_ARROW);
WndClass.hbrBackground := Brush;
WndClass.lpszMenuName := nil;
WndClass.lpszClassName := PChar(s);
WndClass.hIconSm := LoadIcon (hInstance, 'SMALLICON');
if RegisterClassEx (WndClass) = 0 then DisplayError ('Register Window Class Failed.');
end;
Hwnd := CreateWindowEx (StyleEx, PChar(s), PChar(Caption), Style, X, Y, Width, Height, HwndParent, 0, hInstance, Self);
if Hwnd = 0 then DisplayError ('Create Window Failed.');
end;
destructor TController.Destroy;
begin
inherited Destroy;
end;
procedure TController.Close;
begin
SendMessage (Hwnd, WM_CLOSE, 0, 0);
end;
function TController.OnClose: Boolean;
begin
Result := False;
end;
function TController.OnCreate: Boolean;
begin
Result := False;
end;
function TController.OnDestroy: Boolean;
begin
Result := False;
end;
procedure InitializeAppl;
begin
Win := TWin.Create (10, 10, 200, 100, GetStockObject (WHITE_BRUSH), 'Win', WS_SYSMENU, WS_EX_TOOLWINDOW, 0);
end;
procedure MessageLoop;
var Msg: TMsg;
begin
while GetMessage (Msg, 0, 0, 0) do
begin
DispatchMessage (Msg);
end;
end;
procedure FinalizeAppl;
begin
Win.Free; //Hier weet ik dus niet precies hoe ik kan
// controleren ofdat Win wel nog naar een geldig object
// verwijst... Win.Free resulteert in een foutmelding...
end;
begin
InitializeAppl;
MessageLoop;
FinalizeAppl;
end. |