How to combine the program with MFC classes in BCB 3.0 ?
1998-04-25 12:25:12


It is a grate chose to combine the VCL and MFC together. But, this way will make you program to be more complex.

In this article, I assum that you have readed another atricle" How to compiler the program with MFC classes in BCB 3.0 ?". So, I will not tell you something that I have told.

To discussing this topic, I try to use the way "doing it step by step" to tell you how to do it.

Step 1.

Use the way you know to build a project that can compiler MFC correctly in BCB 3.0.(Use the TApplication to be the main class) In this case, I assum that the project file was named as "Project1.bpr" , and the MainForm was named as "Form1". In this way, you should have these file list below :

Project1.bpr
Project1.cpp
Unit1.cpp
Unit1.h
Unit1.dfm


Step 2
.
In this example, I decide to use two classes of MFC to combine the VCL of BCB together. These two classes are "CFileDialog" and CPoint.

First, you put something on the Form1 as "Figure. 1".



Figure.1

Step 3.
Then, you can add the MFC code here. But, you must include the MFC header file first.


//---------------------------------------------------------------------------
#include <afxwin.h>
#include <Afxdlgs.h>
#include <vcl.h>


There is one thing that you should know . you must include afxwin.h before vcl.h , otherwise you will got lots of errors from compiler.


Step 4.
Ok, that do it.


First, I use the CPoint. You must key in this code below in the OnMouseMove event of Form1.


void __fastcall TForm1::FormMouseMove(TObject *Sender, TShiftState Shift,
int X, int Y)
{
    CPoint Ps;
    Ps.x =X;
    Ps.y =Y;
    Label1->Caption = "X :" + (AnsiString)Ps.x + " Y :" + (AnsiString)Ps.y;
}


Ok, you can try to compiler this project. But you will see that there are lots of warrings from compiler. Don't worry, because of there lots of marcos and virtual functions in MFC are not defined so fine, so the compiler find it out.

After compiler and run this program, you will find that the Label1 will show the mouse position when you moving the mouse. In this way, we make the MFC and VCL combine together correctly!

Step 5.

And then, that as use
CFileDialog class of MFC in BCB.
In the "OnClick" Event of Button1, add these codes :

void __fastcall TForm1::Button1Click(TObject *Sender)
{

     CFileDialog dlg( TRUE,"CPP","*.CPP",
            OFN_HIDEREADONLY|OFN_OVERWRITEPROMPT,
            _T("C++ Source File (*.cpp)"));

    if( dlg.DoModal()==IDOK )
    {
        Label2->Caption = (AnsiString)dlg.GetPathName();
    }
}

You will got lots of warrings and the CPoint example. But after running this program. click the Button1 and you will got the same Dialog as
TOpenDialog in VCL . After chosing the file, you will find out the Lable2 of Form1will show the name of the file you selected. In this way, we make the CFileDialog work together with VCL too.

Result

002_Result.gif (3790 bytes)

Download the example

Something that you should know

In this example, you can learn how to combine MFC and VCL together. But, therer are lots of compiler warring that we should solve it. In my opinion, I can bare that there are lots of  warrings generated from the compiler. How to solve it? Wise reader, use your brain to find it out!