FMX.Advertising.TBannerAd (C++)
Contents
Description
This example shows how to add an advertising banner to a mobile application.
To build and test this example:
- Create a blank Multi-Device Application.
- Add a TBannerAd and a TLabel to your main form. You can place the banner on top and the label right below.
- For Android, configure the connection data for your advertising service.
- Set the TestMode property of your TBannerAd to
True
. - Define the following string for the label text: "Click the ad above!".
- In "Unit1.h", in the
private
section of your form class, declare:TDateTime ActionBeginDate;
int WastedSeconds;
- In the
#include
section, add#include <System.DateUtils.hpp>
. - Define event handlers with the implementations shown in the Code section below.
- Select the form and define an event handler for its OnShow event.
- Select the banner and define event handlers for its OnActionCanBegin and OnActionDidFinish events.
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 = IntToStr(WastedSeconds) + " seconds wasted watching ads so far.";
}
//---------------------------------------------------------------------------
Uses
- FMX.Advertising.TBannerAd ( fr | de | ja )
- System.DateUtils.SecondsBetween ( fr | de | ja )