views:

189

answers:

1

Hi

I need to send a windows message to a TDataModule in my Delphi 2010 app.

I would like to use

PostMessage(???.Handle, UM_LOG_ON_OFF, 0,0);

Question:

The TDataModule does not have a Handle. How I send a windows message to it?

+6  A: 

You can give it a handle easily enough. Take a look at AllocateHWND in the Classes unit. Call this to create a handle for your data module, and define a simple message handler that will process UM_LOG_ON_OFF.

Mason Wheeler
Does AllocateHWND just create a hidden window?
gbrandt
@gbrandt: It creates a *window handle,* which is not quite the same thing. All visual controls must have a window handle in order to receive messages and handle their own drawing, (and visual controls without a handle can't draw themselves or receive messages,) but not every handle neds to be bound to a visual element.
Mason Wheeler
@gbrandt: Have a look at this link http://www.delphidabbler.com/articles?article=1on "How a non-windowed component can receive messages from Windows"
Charles Faiga
Note that the AllocateHWND of the Forms unit is deprecated. Use the one available in Classes instead. And yes, AllocateHWND creates an hidden window, but in the sense of the Windows API, not in the sense of a Delphi TForm: this window is an API handle, which is used to receive GDI messages.
A.Bouchez