1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

C++Builder dynamic events from DLL

Discussion in 'MFC' started by michal, Jan 16, 2015.

  1. michal

    michal New Member

    Joined:
    Sep 12, 2014
    Messages:
    11
    Likes Received:
    0
    Trophy Points:
    0
    Hello to every body.

    I want to make a DLL with a dynamic Form inside, this Form must contains a dynamic TButton and this one must have an OnClick event, dynamic too.
    How I can achieve that?

    I had created a dynamic Form with all mentioned before, but It comes from a clasic OnClick event from a Button inside a clasic Form, I mean, this Form clasic is not dynamic.
    This is my code:
    Code:
    //Unit1.cpp
    //---------------------------------------------------------------------------
    
    #include <vcl.h>
    #pragma hdrstop
    #include "Unit1.h"
    //---------------------------------------------------------------------------
    #pragma package(smart_init)
    #pragma resource "*.dfm"
    TForm1 *Form1;
    TForm *MyDynamicForm;
    TButton *MyDynamicButton;
    
    //---------------------------------------------------------------------------
    __fastcall TForm1::TForm1(TComponent* Owner)
    	: TForm(Owner)
    {
    }
    //---------------------------------------------------------------------------
    void __fastcall TForm1::Button1Click(TObject *Sender)
    {
       MyDynamicForm = new  TForm(Owner);
       MyDynamicForm->Height = 240;
       MyDynamicForm->Width = 480;
       MyDynamicForm->Caption = "My Dynamic Form";
    
       MyDynamicButton = new TButton(MyDynamicForm);
       MyDynamicButton->Parent = MyDynamicForm;
       MyDynamicButton->Caption = "My Button";
       MyDynamicButton->Left= 80;
       MyDynamicButton->Top = 30;
       MyDynamicButton->OnClick = MyFunction;
    
       MyDynamicForm->ShowModal();
    }
    //---------------------------------------------------------------------------
     void __fastcall TForm1::MyFucntion(TObject *Sender)
     {
       ShowMessage("This is a Dynamic OnClick Event");
     }
    
    Code:
    //----------------------------------------------------------------------------
    //Unit1.h
    
    //---------------------------------------------------------------------------
    
    #ifndef Unit1H
    #define Unit1H
    //---------------------------------------------------------------------------
    #include <Classes.hpp>
    #include <Controls.hpp>
    #include <StdCtrls.hpp>
    #include <Forms.hpp>
    //---------------------------------------------------------------------------
    class TForm1 : public TForm
    {
    __published:	// IDE-managed Components
    	TButton *Button1;
    	void __fastcall Button1Click(TObject *Sender);
    private:	// User declarations
    public:		// User declarations
    	__fastcall TForm1(TComponent* Owner);
    	void __fastcall MyFunction(TObject *Sender);
    };
    //---------------------------------------------------------------------------
    extern PACKAGE TForm1 *Form1;
    //---------------------------------------------------------------------------
    #endif
    
    All before working perfectly. But if MyFunction not belong to a Form class,this event don't will work.Why?? Exist a way to achieve this?? Which one??
    I was thinking that the event problem is that this one must belong to a Form Class, but my dll don't have one. So, how I can to build a Form class inside my dll?
    I know that a function definition inside a Dll is like this:

    extern "C" __declspec(dllexport) __fastcall void MyFunction(TObject *Sender);

    But that don't working when I try to use as an event. I mean:

    MyDynamicButton->OnClick = MyFunction; // but MyFunction is not a Form class member.

    How I can to convert MyFunction in an event and then use it in my dynamic button??

    Thanks and sorry for my bad English, I am a Cuban.

    Míchal
     

Share This Page