typeTMyObject =class(TObject)privateFHandle: THandle;procedureWndProc(varMessage: TMessage);publicprocedureAfterConstruction;override;procedureBeforeDestruction;override;propertyHandle: THandlereadFHandle;end;procedureTMyObject.AfterConstruction;begininherited; FHandle := AllocateHWnd(WndProc);end;procedureTMyObject.BeforeDestruction;begininherited; DeallocateHWnd(FHandle);end;procedureTMyObject.WndProc(varMessage: TMessage);beginifMessage.Msg = 10000thenShowMessage('Message received')elseMessage.Result := DefWindowProc(FHandle, Message.Msg, Message.wParam, Message.lParam);end;varC: TMyObject;beginC := TMyObject.Create;trySendMessage(C.Handle, 10000, 0, 0);finallyC.Free;end;end;
Thursday, October 25, 2007
Make non TWinControl descendant response to windows messages
Although using SendMessage and PostMessage isn't a good practice in OO design. However, I do need it in some situation.
All the while I wrote TWinControl descendant to handle the custom messages:
W := TWinControl_Descendant.Create(nil);
W.Parent := Application.MainForm;
PostMessage(W.Handle, 10000, 0, 0);
I got to set the Parent for the instance else the handle won't be allocated. It is troublesome if the application I wrote isn't a window form application.
I raise a post in codegear newsgroup "borland.public.delphi.vcl.components.using.win32" of title "Is that possible to allocate windows handle to TObject direct descendant" and get some great replies. The TTimer component in Delphi VCL already is a good example of how to make a non TWinControl descendant response to windows messages. I then write a prototype to try out and it work as expected.
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment