texttransformer.jpg

With the message manager TMessageManager in the "System.Messaging" unit, Delphi RTL offers a cross-platform option for processing messages.

The messages to be processed must all be derived from the base class TMessage, which is defined in the unit "System.Messaging" *). Custom messages are defined using a generic TMessage type:

TMessage = class (TMessage)

The further steps, obtaining the instance of a message manager, subscribing to message types and finally sending the messages are described in the Embarcadero documentation. (The links are not here because they change frequently. But they are easy to find.)

The entire Delphi code of the example is copied here:


unit Unit1;

interface

uses
  System.SysUtils, System.Types, System.UITypes, System.Classes, System.Variants,
  FMX.Types, FMX.Graphics, FMX.Controls, FMX.Forms, FMX.Dialogs, FMX.StdCtrls,
  System.Messaging, FMX.Edit;

type
  TForm1 = class(TForm)
    Edit1: TEdit;
    Button1: TButton;
    procedure Button1Click(Sender: TObject);
    procedure FormCreate(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form2: TForm1;

implementation

{$R *.fmx}

procedure TForm1.Button1Click(Sender: TObject);
var
  MessageManager: TMessageManager;
  Message: TMessage;
begin
  MessageManager := TMessageManager.DefaultManager;
  Message := TMessage.Create(Edit1.Text);
  MessageManager.SendMessage(Sender, Message, True);
end;

procedure TForm1.FormCreate(Sender: TObject);
var
  SubscriptionId: Integer;
  MessageManager: TMessageManager;
begin
  MessageManager := TMessageManager.DefaultManager;
  SubscriptionId := MessageManager.SubscribeToMessage(TMessage, procedure(const Sender: TObject; const M: TMessage)
  begin
    ShowMessage((M as TMessage).Value);
  end);
end;

end.


This code can be translated directly into the corresponding C++ code using Delphi2Cpp:



#ifndef Unit1H
#define Unit1H

#include "System.h"
#include "d2c_system.h"

#include "Winapi.Windows.h"
#include "Winapi.Messages.h"
#include "System.SysUtils.h"
#include "System.Variants.h"
#include "System.Classes.h"
#include "Vcl.Graphics.h"
#include "Vcl.Controls.h"
#include "Vcl.Forms.h"
#include "Vcl.Dialogs.h"
#include "Vcl.StdCtrls.h"



class TForm1 : public Vcl::Forms::TForm
{
public:
  typedef Vcl::Forms::TForm inherited;
  typedef System::ClassRef ClassRefType;  
  ClassRefType* ClassType() const {return  System::class_id();}
  TForm1* Create() const {return new TForm1();}
  static TForm1* SCreate() {return new TForm1();}
  System::String ClassName() const {return L"TForm1";}
  static System::String SClassName() {return L"TForm1";}
public:
  Vcl::Stdctrls::TButton* Button1;
  Vcl::Stdctrls::TEdit* Edit1;
  void Button1Click(TObject* Sender);
  void FormCreate(TObject* Sender);
private:
    /* Private-Deklarationen */
public:
    /* Public-Deklarationen */
  void InitMembers(){Button1 = nullptr; Edit1 = nullptr;}
  TForm1(System::Classes::TComponent* AOwner);
  TForm1(HWND parentWindow);
  TForm1();
};
extern TForm1* Form1;
#endif // Unit1H

#include "pch.h"

#include "Unit1.h"
#include "System.Messaging.h"

using namespace std;
using namespace System;
using namespace System::Classes;
using namespace System::Messaging;
using namespace Vcl::Dialogs;
using namespace Vcl::Forms;
using namespace Vcl::Stdctrls;

TForm1::TForm1(System::Classes::TComponent* AOwner) : inherited(AOwner) {InitMembers();}
TForm1::TForm1(HWND parentWindow) : inherited(parentWindow) {InitMembers();}
TForm1::TForm1() {InitMembers();}

TForm1* Form1 = nullptr;
//#pragma resource /* "*.dfm" */ 

// https://docwiki.embarcadero.com/CodeExamples/Alexandria/en/System.Messaging_(Delphi)
// https://docwiki.embarcadero.com/CodeExamples/Alexandria/en/System.Messaging_(C%2B%2B)

void TForm1::Button1Click(TObject* Sender)
{
  TMessageManager* MessageManager = nullptr;
  System::Messaging::TMessage* Message = nullptr;
  MessageManager = TMessageManager::ReadPropertyDefaultManager();
  Message = new TMessage__1(Edit1->ReadPropertyText());
  MessageManager->SendMessage(Sender, Message, true);
}

void TForm1::FormCreate(TObject* Sender)
{
  int SubscriptionId = 0;
  TMessageManager* MessageManager = nullptr;
  MessageManager = TMessageManager::ReadPropertyDefaultManager();
  SubscriptionId = (int) MessageManager->SubscribeToMessage(class_id>(), [&](TObject* const Sender,    System::Messaging::TMessage* const m) -> void 
  {
    ShowMessage(((TMessage__1*) m)->ReadPropertyValue());
  });
}

C++Builder

The translation for the C++Builder differs from this code in that the TMessageBase type must be used instead of the base type TMessage. This can be achieved by defining a refactoring to TMessageBase for the non-generic TMessage type.

The Embarcadero documentation also contains the example code for the C++Builder. Instead of the anonymous method, a method is defined in TForm1. With the modern C++ compiler in RAD Studio Alexandrira, this is no longer necessary. The translated code for the C++Builder works without any further post-processing.

For the C++Builder we use the __classid of the message type as the key for the subscribed methods. This special keyword does not exist for other compilers. Instead, simulation using the class_id function is used here.

*) One problem is that there is another TMessage class defined in the unit "Winapi.Messages". For the C++Builder, message classes must therefore not be derived from TMessage but from TMessageBase


   deutsch Deutsch

 

 
Latest News
08/26/24
Delphi2Cpp 2.4: Updated to RAD Studio 12.1 Athens [more...]

01/29/24
Aurora2Cpp: Delphi 7 translator [more...]



"Thanks for your great work, really appreciate the work you have done on zlib and compiling ... test case."


Mattewada, Udayabhaskar
Nokia India 02/01/2021




[from case study...]

"A masterpiece -- Delphi2Cpp has exceeded all my expectations by far."


Tony Hürlimann
virtual-optima 08/20/2011



"First off, I have to say WOW! Delphi2Cpp is doing a *fantastic* job!"


Daniel Flower
linkrealms 01/15/2011


 
This website is generated from plain text with [Minimal Website ]

Minimal Website
Minimal Website is made with TextTransformer

TextTransformer
TextTransformer is made with Borland CBuilder

  borland