Board index » cppbuilder » User-defined windows messages in a TDataModule

User-defined windows messages in a TDataModule


2004-01-07 08:24:13 PM
cppbuilder73
Hi.
How does one catch user-defined windows messages in a TDataModule?
i.e. messages created by the "registerwindowsmessage" api function.
Regards,
-- Rod
 
 

Re:User-defined windows messages in a TDataModule

"Rod" < XXXX@XXXXX.COM >wrote in message
Quote
i.e. messages created by the "registerwindowsmessage" api function.
TDataModule is not a windowed control to begin with, thus it cannot receive
any window messages at all by default. Have a look at using the
AllocateHWnd() function for giving the DataModule a hidden window that you
can then send your messages to. For example:
class TMyDataModule : public TDataModule
{
private:
HWND FHwnd;
UINT FMyMessage;
void __fastcall MyWndProc(TMessage &Message);
public:
__fastcall TMyDataModule(TComponent *Owner);
__fastcall ~TMyDataModule();
__property HWND WindowHandle = {read=FHwnd};
};
__fastcall TMyDataModule::TMyDataModule(TComponent *Owner)
{
FMyMessage = RegisterWindowMessage("MyMessage");
FHwnd = AllocateHWnd(MyWndProc);
}
__fastcall TMyDataModule::~TMyDataModule()
{
DeallocateHWnd(FHwnd);
}
void __fastcall TMyDataModule::MyWndProc(TMessage &Message)
{
if( (Message.Msg == FMyMessage) && (FMyMessage != 0) )
{
// do something ...
Message.Result = TRUE;
}
else
Message.Result = DefWindowProc(FHwnd, Message.Msg,
Message.WParam, Message.LParam);
}
Then you can do the following in your application code:
UINT uMsg = RegisterWindowMessage("MyMessage");
//...
SendMessage(MyDataModule->WindowHandle, uMsg, ...);
// or
PostMessage(MyDataModule->WindowHandle, uMsg, ...);
Gambit
 

Re:User-defined windows messages in a TDataModule

"Remy Lebeau \(TeamB\)" < XXXX@XXXXX.COM >wrote:
Thanks Remy.
I had already tried something quite similiar, some months ago, but there
were some strange results. (I'll have to check the code again to remind
myself).
However, the code you gave definately has differences to the code I tried
so I'll give it a shot.
Thanks again.
Regards,
[snip]
-- Rod
 

{smallsort}