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.
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.
type
TMyObject =class(TObject)
private
FHandle: THandle;
procedureWndProc(varMessage: TMessage);
public
procedureAfterConstruction;override;
procedureBeforeDestruction;override;
propertyHandle: THandlereadFHandle;
end;procedureTMyObject.AfterConstruction;begin
inherited;
FHandle := AllocateHWnd(WndProc);end;procedureTMyObject.BeforeDestruction;begin
inherited;
DeallocateHWnd(FHandle);end;procedureTMyObject.WndProc(varMessage: TMessage);begin
ifMessage.Msg = 10000then
ShowMessage('Message received')
else
Message.Result := DefWindowProc(FHandle, Message.Msg, Message.wParam, Message.lParam);end;varC: TMyObject;begin
C := TMyObject.Create;
try
SendMessage(C.Handle, 10000, 0, 0);
finally
C.Free;
end;end;
No comments:
Post a Comment