FMX.Advertising.TBannerAd (C++)

From Appmethod Code Examples
Jump to: navigation, search

Description

This example shows how to add an advertising banner to a mobile application.

To build and test this example:

  1. Create a blank FireMonkey mobile application.
  2. Add a TBannerAd and a TLabel to your main form. You can place the banner on top and the label right below.
  3. Configure the connection data for your advertising service.
  4. Set the TestMode property of your TBannerAd to True

.

  1. Define the following string for the label text: "Click the ad above!".
  2. In "Unit1.h", in the private section of your form class, declare:
    TDateTime ActionBeginDate;
    int WastedSeconds;
  3. In the #include section, add #include <System.DateUtils.hpp>.
  4. Define event handlers with the implementations shown in the Code section below.
    1. Select the form and define an event handler for its OnShow event.
    2. Select the banner and define event handlers for its OnActionCanBegin and OnActionDidFinish events.

FMX.Advertising.TBannerAd.png

Code

Unit1.h:

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

#ifndef Unit1H
#define Unit1H
//---------------------------------------------------------------------------
#include <System.Classes.hpp>
#include <FMX.Controls.hpp>
#include <FMX.Forms.hpp>
#include <FMX.Advertising.hpp>
#include <FMX.StdCtrls.hpp>
#include <FMX.Types.hpp>
#include <System.DateUtils.hpp>
//---------------------------------------------------------------------------
class TForm1 : public TForm
{
__published:    // IDE-managed Components
        TBannerAd *BannerAd1;
        TLabel *Label1;
        void __fastcall FormShow(TObject *Sender);
        void __fastcall BannerAd1ActionCanBegin(TObject *Sender, bool &WillLeaveApplication);
        void __fastcall BannerAd1ActionDidFinish(TObject *Sender);

private:        // User declarations
public:         // User declarations
        __fastcall TForm1(TComponent* Owner);

        TDateTime ActionBeginDate;
        __int64 WastedSeconds;
};
//---------------------------------------------------------------------------
extern PACKAGE TForm1 *Form1;
//---------------------------------------------------------------------------
#endif

Unit1.cpp:

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

#include <fmx.h>
#include <string.h>
#pragma hdrstop

#include "Unit1.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.fmx"
TForm1 *Form1;
//---------------------------------------------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner)
        : TForm(Owner)
{
}
//---------------------------------------------------------------------------
void __fastcall TForm1::FormShow(TObject *Sender)
{
    BannerAd1->LoadAd();
}
//---------------------------------------------------------------------------
void __fastcall TForm1::BannerAd1ActionCanBegin(TObject *Sender, bool &WillLeaveApplication)
{
    ActionBeginDate = Now();
}
//---------------------------------------------------------------------------
void __fastcall TForm1::BannerAd1ActionDidFinish(TObject *Sender)
{
    __int64 Seconds = SecondsBetween(ActionBeginDate, Now());
    WastedSeconds += Seconds;
    Label1->Text = WastedSeconds + " seconds wasted watching ads so far.";
}
//---------------------------------------------------------------------------


Uses

See Also