Declaring Form in Embarcadero C++ builder:

731 Views Asked by At

Getting started with Embarcadero XE-5, the object model has me confused. My project involves the Canvas right from the start, so my hello world is to draw a line or two. Set up an SDI project, and added a fastcall directly out of the C++ builder help, but can't get it to compile. Form1 is used in all the examples, but my efforts to instantiate it aren't working. I've tried to declare Form1 in various ways, no success.

Can anyone point out my error, please?

// ----------------------------------------------------
#include <vcl.h>
#pragma hdrstop>  
#include <tchar.h>
//-----------------------------------------------------
USEFORM("SDIMAIN.CPP", SDIAppForm);
USEFORM("ABOUT.CPP", AboutBox);
//-----------------------------------------------------
int WINAPI _tWinMain(HINSTANCE, HINSTANCE, LPTSTR, int)
    {
    Application->Initialize();
    Application->CreateForm(__classid(TSDIAppForm), &SDIAppForm);

 // ** Following line gives error: Form1 undefined. ** 
     Application->CreateForm(__classid(TCanvas), &Form1);   
     Application->CreateForm(__classid(TAboutBox), &AboutBox);
     Application->Run();

     return 0;
     }
 //------------------------------------------------------

/*  SDIMAIN - copied from the help screens  */
void __fastcall TForm1::FormPaint(TObject *Sender) 
{
Canvas->MoveTo(0,0);
Canvas->LineTo(ClientWidth, ClientHeight);
Canvas->MoveTo(0, ClientHeight);
Canvas->LineTo(ClientWidth, 0);
}
1

There are 1 best solutions below

0
Remy Lebeau On

You don't use TApplication::CreateForm() to create TCanvas objects. Change __classid(TCanvas) to __classid(TForm1) instead:

// ----------------------------------------------------
#include <vcl.h>
#pragma hdrstop>  
#include <tchar.h>
//-----------------------------------------------------
USEFORM("SDIMAIN.CPP", SDIAppForm);
USEFORM("Unit1.cpp", Form1);
USEFORM("ABOUT.CPP", AboutBox);
//-----------------------------------------------------
int WINAPI _tWinMain(HINSTANCE, HINSTANCE, LPTSTR, int)
{
    Application->Initialize();
    Application->CreateForm(__classid(TSDIAppForm), &SDIAppForm);
    Application->CreateForm(__classid(TForm1), &Form1);   
    Application->CreateForm(__classid(TAboutBox), &AboutBox);
    Application->Run();
    return 0;
}
//------------------------------------------------------

Of course, this requires you to have a TForm1 class to begin with:

File > New > VCL Form