The WinAPI window object has an extra memory space that you can use for your own purpose (i.e. store the reference of the Object Pascal's window object). I did it on C++ with the good old Windows 3.1 a lot of years ago, when Delphi and Visual were wet dreams. I was looking on the Internet and I've found the SetWindowLong/GetWindowLong functions, which are the functions I used (IIRC ).
The idea is to create the window object and use its handler to assign your object reference. Then the "WndProc" will extract the object reference and call the method. Be careful. The WndProc should check if the reference was assigned before to call the method and provide default behaviour because "in theory" each window object has it's own threads (on Windows 3.1 that wasn't completely true but I think it is true in modern Windows systems). IIRC there are some messages it must process before the extra memory is assigned.
Pseudo code:
Code:
(*** Remember this is PSEUDO code. ***)
CONSTRUCTOR TMyWindowClass.Create;
BEGIN
INHERITED;
SELF.WinClass := RegisterWindowClass (...);
SELF.WinHandle := CreateWindow (...);
IF SetWindowLong (SELF.WinHandle, GWL_USERDATA, LONGINT (@SELF)) <> OK THEN
RAISE Error;
END;
...
FUNCTION WindowProc (hwnd: HWND; msg: UINT; wParam: WPARAM; lParam: LPARAM): LRESULT;
VAR
Window: TMyWindowClass;
BEGIN
Window := GetWindowLong (hwnd, GWD_USERDATA);
IF Window <> OK THEN
DefaultMessageProc (...)
ELSE
Window.ProcessMessage (...);
END;
I hope you get the idea. I did it a lot of time ago, when Windows 3.1 was The Future.
Investigate about the SetWindowLong, GetWindowLong and related functions.
Bookmarks