Главная страница | назад





Article #17457: Exporting a Form as A class

 Question and Answer Database
FAQ2457C.txt Exporting a Form as A class
Category :VCL
Platform :All
Product :C++Builder 3.x
Question:
I've an application which have a lot of Forms (about 20), and I would like
to convert all the files to DLL but how can I include all the Forms which I
already created at design time?
Answer:
OK. Here's a simple example..... Everybody got their IDEs ready?
1. Create a new DLL project (File | New | DLL).
2. Add a new form to the project (click on the new form speed button).
3. Add a TLabel to the form.
4. Add the following to the Unit1.h file, just after all the #include
statements.
#ifdef _GENERATING_DLL_
#define EXPORT_CLASS __declspec(dllexport)
#else
#define EXPORT_CLASS __declspec(dllimport)
#endif
5. Now, add EXPORT_CLASS just in front of your form class declaration and
add a Setlabel method so that your declaration looks like this:
EXPORT_CLASS class TForm1 : public TForm
{
__published: // IDE-managed Components
TLabel *Label1;
private: // User declarations
public: // User declarations
void SetLabel(char const *);
__fastcall TForm1(TComponent* Owner);
};
6. Edit Unit1.cpp, and add the following, right before you include Unit1.h:
#define _GENERATING_DLL_
7. Add the SetLabel implementation:
void TForm1::SetLabel(char const *label)
{
Label1->Caption = label;
}
8. Add the form header to the .dll source file and add the following
to the DllEntryPoint() fnc:
switch(reason)
{
case DLL_PROCESS_ATTACH : Form2 = new TForm2(NULL);
break;
case DLL_PROCESS_DETACH : delete Form2;
break;
}
return 1;
8. Build the DLL (Project | Make)
9. The ide will genererate a .lib file that you can include in
another application to use the form.
An example of a function in a test app might look like this:
//-----------------------------------------------------------
void __fastcall TMyTestAppForm::Button1Click(TObject *Sender)
{
Form2->Show();
Form2->SetLabel("Some bit of text.");
}
//-----------------------------------------------------------
7/2/98 10:32:32 AM

Last Modified: 01-SEP-99