1 { Copyright (C) 2005 Bas Steendijk and Peter Green
\r
2 For conditions of distribution and use, see copyright notice in zlib_license.txt
\r
3 which is included in the package
\r
4 ----------------------------------------------------------------------------- }
\r
6 unit unitwindowobject;
\r
13 windows,messages,wmessages,
\r
17 {$define windows := lmessages}
\r
23 twindowobject=class(tobject)
\r
25 onmsg:function(msg,wparam,lparam:taddrint):boolean of object;
\r
26 exitloopflag:boolean;
\r
27 function settimer(id,timeout:taddrint):integer;
\r
28 function killtimer(id:taddrint):boolean;
\r
29 procedure postmessage(msg,wparam,lparam:taddrint);
\r
30 procedure messageloop;
\r
32 procedure processmessages;
\r
33 function processmessage:boolean;
\r
36 destructor destroy; override;
\r
43 function WindowProc(ahWnd:HWND; auMsg:Integer; awParam:WPARAM; alParam:LPARAM):Integer; stdcall;
\r
47 ////swriteln('in unitwindowobject.windowproc');
\r
48 Result := 0; // This means we handled the message
\r
49 if ahwnd <> hwnd(0) then i := getwindowlongptr(ahwnd,0) else i := 0;
\r
50 if i <> 0 then begin
\r
51 if assigned(twindowobject(i).onmsg) then begin
\r
52 if not twindowobject(i).onmsg(aumsg,awparam,alparam) then i := 0;
\r
55 if i = 0 then Result := DefWindowProc(ahWnd, auMsg, awParam, alParam)
\r
59 twindowobject_Class : TWndClass = (style:0; lpfnWndProc:@WindowProc;
\r
60 cbClsExtra:0; cbWndExtra:sizeof(pointer); hInstance:thinstance(0); hIcon:hicon(0); hCursor:hcursor(0);
\r
61 hbrBackground:hbrush(0);lpszMenuName:nil; lpszClassName:'twindowobject_class');
\r
63 function twindowobject.settimer;
\r
65 result := windows.settimer(hwndmain,id,timeout,nil);
\r
68 function twindowobject.killtimer;
\r
70 result := windows.killtimer(hwndmain,id);
\r
73 constructor twindowobject.create;
\r
76 //swriteln('in twindowobject.create, about to call registerclass');
\r
77 Windows.RegisterClass(twindowobject_Class);
\r
78 //swriteln('about to call createwindowex');
\r
79 hWndMain := CreateWindowEx(WS_EX_TOOLWINDOW, twindowobject_Class.lpszClassName,
\r
80 '', WS_POPUP, 0, 0,0, 0, hwnd(0), 0, HInstance, nil);
\r
81 //swriteln('about to check result of createwindowex');
\r
82 if hWndMain = hwnd(0) then raise exception.create('CreateWindowEx failed');
\r
83 //swriteln('about to store reference to self in extra windo memory');
\r
84 setwindowlongptr(hwndmain,0,taddrint(self));
\r
85 //swriteln('finished twindowobject.create , hwndmain='+inttohex(taddrint(hwndmain),16));
\r
88 destructor twindowobject.destroy;
\r
90 if hWndMain <> hwnd(0) then DestroyWindow(hwndmain);
\r
94 procedure twindowobject.postmessage;
\r
96 windows.postmessage(hwndmain,msg,wparam,lparam);
\r
100 function twindowobject.ProcessMessage : Boolean;
\r
105 if PeekMessage(Msg, hwndmain, 0, 0, PM_REMOVE) then begin
\r
107 DispatchMessage(Msg);
\r
111 procedure twindowobject.processmessages;
\r
113 while processmessage do;
\r
117 procedure twindowobject.messageloop;
\r
121 while GetMessage(MsgRec, hwnd(0), 0, 0) do begin
\r
122 DispatchMessage(MsgRec);
\r
123 if exitloopflag then exit;
\r
124 {if not peekmessage(msgrec,0,0,0,PM_NOREMOVE) then onidle}
\r