TAGS :Viewed: 19 - Published at: a few seconds ago

[ How to send windows messages to non-main forms ]

How do I go about sending a Windows Message to a form in my application that is not the MainForm? Using the code below gives me an Access Violation.

procedure TMainForm.SendMessageToAnotherForm;
begin
  SendMessage(MyForm.Handle,WM_MY_MESSAGE,0,0);
end;

MyForm has already been created and is the top most window.

Edit: I have tried PostMessage also but I receive the same Access Violation and I am positive that WM_MY_MESSAGE is valid because I use it to communicate with MainForm somewhere else.

Answer 1


The most probable reason to obtain an access violation with the above code is MyForm = nil or a wild pointer. Set a breakpoint on the line of code with SendMessage call and check it. If MyForm is a valid reference, then an access violation is caused by message processing in MyForm.

Answer 2


Are you aware that SendMessage waits until the message is handled? That means that the message queue of the target window must be able to process messages.

If you call this method inside an event handler (like ButtonClick) you should better use PostMessage instead.