Proyecto Multiplataforma Toast Para Android, iOS, Windows y OSX usando C++

Código completo C++ Builder:

Ésta página es el detalle de archivos del proyecto "Cómo simular mensajes Toast en Android, iOS, Windows y OSX usando C++".

Unit2.h
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
//---------------------------------------------------------------------------

#ifndef Unit2H
#define Unit2H
//---------------------------------------------------------------------------
#include <System.Classes.hpp>
#include <FMX.Controls.hpp>
#include <FMX.Forms.hpp>
#include <FMX.Ani.hpp>
#include <FMX.Controls.Presentation.hpp>
#include <FMX.Objects.hpp>
#include <FMX.StdCtrls.hpp>
#include <FMX.Types.hpp>
//---------------------------------------------------------------------------
class TfToastMsg : public TFrame
{
__published: // IDE-managed Components
 TFloatAnimation *FloatAnimation1;
 TRoundRect *RoundRect1;
 TLabel *lblWaitMsg;
 void __fastcall FloatAnimation1Finish(TObject *Sender);
private: // User declarations
public:  // User declarations
 __fastcall TfToastMsg(TComponent* Owner);
 void __fastcall ShowToast(String pMsg);
};
//---------------------------------------------------------------------------
extern PACKAGE TfToastMsg *fToastMsg;
//---------------------------------------------------------------------------
#endif

Design (Alt+F12): Unit2
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
object fToastMsg: TfToastMsg
  Size.Width = 232.000000000000000000
  Size.Height = 70.000000000000000000
  Size.PlatformDefault = False
  Visible = False
  object FloatAnimation1: TFloatAnimation
    Duration = 0.200000002980232200
    OnFinish = FloatAnimation1Finish
    PropertyName = 'Position.Y'
    StartValue = 0.000000000000000000
    StopValue = 0.000000000000000000
    Trigger = 'IsVisible=true'
  end
  object RoundRect1: TRoundRect
    Align = Client
    Size.Width = 232.000000000000000000
    Size.Height = 70.000000000000000000
    Size.PlatformDefault = False
    object lblWaitMsg: TLabel
      Align = Center
      AutoSize = True
      Size.Width = 60.000000000000000000
      Size.Height = 16.000000000000000000
      Size.PlatformDefault = False
      TextSettings.HorzAlign = Center
      TextSettings.WordWrap = False
      Text = 'lblWaitMsg'
      TabOrder = 0
    end
  end
end

Unit2.cpp
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
//---------------------------------------------------------------------------

#include <fmx.h>
#pragma hdrstop

#include "Unit2.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.fmx"
TfToastMsg *fToastMsg;
//---------------------------------------------------------------------------
__fastcall TfToastMsg::TfToastMsg(TComponent* Owner)
 : TFrame(Owner)
{
 this->Parent = ((TForm*)Owner);
 Name = "FMA"+Now().FormatString("ddhnszzz");
}
//---------------------------------------------------------------------------
void __fastcall TfToastMsg::ShowToast(String pMsg)
{
 auto *xForm = ((TForm*)this->Owner);
 lblWaitMsg->Text = pMsg;
 if ( xForm->Width < lblWaitMsg->Width ) {
   lblWaitMsg->WordWrap = true;
   lblWaitMsg->Width = xForm->Width*0.8;
   this->Width = lblWaitMsg->Width+15;
   this->Height = lblWaitMsg->Height+8;
 } else {
  this->Width = lblWaitMsg->Width+15;
  this->Height = lblWaitMsg->Height+8;
 }

 this->Position->X = (xForm->Width/2) - (this->Width/2);

 int xDistFromBottom= 120;
 if (false) {
  FloatAnimation1->StartValue = 0;
  FloatAnimation1->StopValue = xDistFromBottom;
 } else {
  FloatAnimation1->StartValue = xForm->Height;
  FloatAnimation1->StopValue = xForm->Height-xDistFromBottom;
 }
 this->Visible=true;            //Starts animation.
 Application->ProcessMessages();         //Pass to other processes
}
void __fastcall TfToastMsg::FloatAnimation1Finish(TObject *Sender)
{
 if(this->FloatAnimation1->Inverse) {
  this->FloatAnimation1->Inverse=false;
  this->Visible=false;
 } else {
  this->FloatAnimation1->Inverse=true;
  TThread::CreateAnonymousThread([this]() -> void {
   Sleep(1000); //Segundos que se muestra el mensaje al usuario
   TThread::Synchronize(TThread::CurrentThread, [this]() -> void {
    this->FloatAnimation1->Start();
   });
  })->Start();
 }
}
//---------------------------------------------------------------------------

En Unit1 me parece que no es necesario poner todos sus archivos relacionados porque sólo tenemos la referencia "#include "Unit2.h" y el botón con el evento clic llamando al objeto ToastMsg.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
//---------------------------------------------------------------------------

#include <fmx.h>
#pragma hdrstop

#include "Unit1.h"
#include "Unit2.h"

//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.fmx"
TForm1 *Form1;
//---------------------------------------------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner)
 : TForm(Owner)
{
}
//---------------------------------------------------------------------------
void __fastcall TForm1::SpeedButton1Click(TObject *Sender)
{
 (new TfToastMsg(this))->ShowToast("Hola Mundo!");
}
//---------------------------------------------------------------------------

Feliz C++ code!

No hay comentarios.:

Publicar un comentario

Por favor ser gentil.