System.Messaging (C++)

From RAD Studio Code Examples
Jump to: navigation, search

Description

This example shows how to send and receive messages using the RTL.

It defines a form that contains a text input field and a button. The application behaves as follows:

  • The form defines a method that is called when the default message manager receives a message of UnicodeString type, and this method shows a dialog box with the string from the message.
  • A click on the button sends the string in the input field as a message of UnicodeString type to the default message manager.
Note: This code example is implemented with messages to show how to use them, not because they are the best choice in this case. See When to Use Messages.

To build and test this example:

  1. Create a Multi-Device Application.
  2. Add a TEdit and a TButton to your main form.
  3. Change the Text of the button to "Send Message".
  4. Declare and define a method of your form, ShowReceivedMessage, as shown in the Code section below.
  5. Define event handlers with the implementations shown in the Code section below.
    1. Select the form and define an event handler for its OnCreate event.
    2. Select the button and define an event handler for its OnClick event.

System.Messaging.png

Code

Unit1.h:

//---------------------------------------------------------------------------

#ifndef Unit1H
#define Unit1H
//---------------------------------------------------------------------------
#include <System.Classes.hpp>
#include <FMX.Controls.hpp>
#include <FMX.Forms.hpp>
#include <FMX.Edit.hpp>
#include <FMX.StdCtrls.hpp>
#include <FMX.Types.hpp>
//---------------------------------------------------------------------------
class TForm1 : public TForm
{
__published:    // IDE-managed Components
    TButton *Button1;
    TEdit *Edit1;
    void __fastcall FormCreate(TObject *Sender);
    void __fastcall Button1Click(TObject *Sender);
private:        // User declarations
public:         // User declarations
    __fastcall TForm1(TComponent* Owner);
    void __fastcall ShowReceivedMessage(System::TObject* const Sender, System::Messaging::TMessageBase* const M);
};
//---------------------------------------------------------------------------
extern PACKAGE TForm1 *Form1;
//---------------------------------------------------------------------------
#endif

Unit1.cpp:

//---------------------------------------------------------------------------

#include <fmx.h>
#pragma hdrstop

#include "Unit1.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.fmx"
TForm1 *Form1;
//---------------------------------------------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner)
: TForm(Owner)
{
}
//---------------------------------------------------------------------------
// Captures messages of type TMessage<UnicodeString> and shows a dialog box with
// the string from the message.
void __fastcall TForm1::ShowReceivedMessage(System::TObject* const Sender, System::Messaging::TMessageBase* const M)
{
    TMessage__1<UnicodeString>* Message = dynamic_cast<TMessage__1<UnicodeString>*>(M);
    if (Message)
        ShowMessage(Message->Value);
}
//---------------------------------------------------------------------------
// Subscribes the ShowReceivedMessage method to TMessage<UnicodeString>
// messages.
void __fastcall TForm1::FormCreate(TObject *Sender)
{
    TMessageManager* MessageManager = TMessageManager::DefaultManager;
    TMetaClass* MessageClass = __classid(TMessage__1<UnicodeString>);
    TMessageListenerMethod ShowReceivedMessagePointer = &(this->ShowReceivedMessage);
    MessageManager->SubscribeToMessage(MessageClass, ShowReceivedMessagePointer);
}
//---------------------------------------------------------------------------
// Sends a TMessage<UnicodeString> message containing the text from the TEdit.
void __fastcall TForm1::Button1Click(TObject *Sender)
{
    TMessageManager* MessageManager = TMessageManager::DefaultManager;
    TMessage__1<UnicodeString>* Message = new TMessage__1<UnicodeString>(this->Edit1->Text);
    MessageManager->SendMessage(Sender, Message, true);
}
//---------------------------------------------------------------------------

Uses

See Also