As Michael Van Canneyt says, you cannot use a method as a window message handler. This assumed implicit Self variable will most likely break the return pointer on the stack. <br><br>Here is my code for a user defined window class. Note, window messages are processed through TObject.Dispatch<br>
<br><br>threadvar<br> CreationWindow: TBaseWindow;<br><br>function BaseWindowProc(Wnd: HWND; uMsg: Cardinal; wParam: LongInt; lParam: LongInt): Integer; stdcall;<br>var<br> BaseWindow: TBaseWindow;<br> Msg: TMessage;<br>
begin<br> if CreationWindow <> nil then<br> begin<br> BaseWindow := CreationWindow;<br> BaseWindow.FHandle := Wnd;<br> SetWindowLong(Wnd, GWL_USERDATA, Integer(BaseWindow));<br> CreationWindow := nil;<br>
end<br> else<br> BaseWindow := TBaseWindow(GetWindowLong(Wnd, GWL_USERDATA));<br> Result := -1;<br> if BaseWindow <> nil then<br> try<br> Msg.Msg := uMsg;<br> Msg.wParam := wParam;<br> Msg.lParam := lParam;<br>
Msg.Result := -1;<br> BaseWindow.Dispatch(Msg);<br> Result := Msg.Result;<br> if Msg.Msg = WM_DESTROY then<br> BaseWindow.FHandle := 0;<br> except<br> on E: Exception do<br> MessageBox(0, PChar(E.ClassName + ': ' + E.Message), 'Error',<br>
MB_ICONERROR or MB_OK or MB_TASKMODAL);<br> end<br> else<br> Result := DefWindowProc(Wnd, uMsg, wParam, lParam);<br>end;<br><br>constructor TBaseWindow.Create(Parent: HWND = 0; Style: Cardinal = 0; ExStyle: Cardinal = 0);<br>
var<br> WindowClass: string;<br> WindowName: string;<br> Info: TCreateInfo;<br>begin<br> inherited Create;<br> WindowClass := ClassName + IntToStr(HInstance);<br> FillChar(Info, SizeOf(Info), #0);<br> Info.Parent := Parent;<br>
Info.Style := Style;<br> Info.ExStyle := ExStyle;<br> CreateInfo(Info);<br> if not GetClassInfo(SysInit.HInstance, PChar(WindowClass), Info.WndClass) then<br> begin<br> with Info.WndClass do<br> begin<br> lpfnWndProc := @BaseWindowProc;<br>
lpszClassName := PChar(WindowClass);<br> hInstance := SysInit.HInstance;<br> Windows.RegisterClass(Info.WndClass);<br> end;<br> end;<br> CreationWindow := Self;<br> try<br> with Info do<br> CreateWindowEx(ExStyle, PChar(WindowClass), PChar(WindowName), Style,<br>
X, Y, W, H, Parent, 0, 0, nil);<br> except<br> CreationWindow := nil;<br> end;<br> if FHandle = 0 then<br> RaiseLastWin32Error;<br>end;<br><br>