C++ Builder XE4, 10.2 Tokyo > Form2에서 Form1 버튼 누르기 > Form2는 Unit1.h를 포함하지 않는다 > TButton 포인터 전달

운영 환경
C++ Builder XE4
RAD Studio 10.2 Tokyo Update 2 (追記: 2018/01/05)

개요


  • Form1과 Form2가 있다
  • Form2에서 어느 처리를 하면, Form1에 반영되도록 하고 싶다
  • Form2에서 Unit1.h를 include하지 않는다
  • include 하면 Unit1에 의존해, Unit2의 재이용성이 저감한다
  • 예 : Unit3에서 Unit2 사용



  • code



    TButton의 포인터를 전달해 보았습니다.

    Unit1.h
    //---------------------------------------------------------------------------
    
    #ifndef Unit1H
    #define Unit1H
    //---------------------------------------------------------------------------
    #include <System.Classes.hpp>
    #include <Vcl.Controls.hpp>
    #include <Vcl.StdCtrls.hpp>
    #include <Vcl.Forms.hpp>
    //---------------------------------------------------------------------------
    class TForm1 : public TForm
    {
    __published:    // IDE で管理されるコンポーネント
        TButton *Button1;
        TMemo *Memo1;
        void __fastcall Button1Click(TObject *Sender);
        void __fastcall FormShow(TObject *Sender);
    private:    // ユーザー宣言
    public:     // ユーザー宣言
        __fastcall TForm1(TComponent* Owner);
    };
    //---------------------------------------------------------------------------
    extern PACKAGE TForm1 *Form1;
    //---------------------------------------------------------------------------
    #endif
    

    Unit1.cpp
    //---------------------------------------------------------------------------
    
    #include <vcl.h>
    #pragma hdrstop
    
    #include "Unit1.h"
    #include "Unit2.h"
    //---------------------------------------------------------------------------
    #pragma package(smart_init)
    #pragma resource "*.dfm"
    TForm1 *Form1;
    //---------------------------------------------------------------------------
    __fastcall TForm1::TForm1(TComponent* Owner)
        : TForm(Owner)
    {
    }
    //---------------------------------------------------------------------------
    void __fastcall TForm1::Button1Click(TObject *Sender)
    {
        Memo1->Lines->Add(L"TEST");
    }
    //---------------------------------------------------------------------------
    void __fastcall TForm1::FormShow(TObject *Sender)
    {
        Form2->SetButtonPointer(Button1);
    
        Form2->Show();
    }
    //---------------------------------------------------------------------------
    

    Unit2.h
    //---------------------------------------------------------------------------
    
    #ifndef Unit2H
    #define Unit2H
    //---------------------------------------------------------------------------
    #include <System.Classes.hpp>
    #include <Vcl.Controls.hpp>
    #include <Vcl.StdCtrls.hpp>
    #include <Vcl.Forms.hpp>
    //---------------------------------------------------------------------------
    class TForm2 : public TForm
    {
    __published:    // IDE で管理されるコンポーネント
        TButton *Button1;
        void __fastcall Button1Click(TObject *Sender);
    private:    // ユーザー宣言
        TButton *m_buttonPtr;
    public:     // ユーザー宣言
        void __fastcall SetButtonPointer(TButton *btnPtr);
    
        __fastcall TForm2(TComponent* Owner);
    };
    //---------------------------------------------------------------------------
    extern PACKAGE TForm2 *Form2;
    //---------------------------------------------------------------------------
    #endif
    

    Unit2.cpp
    //---------------------------------------------------------------------------
    
    #include <vcl.h>
    #pragma hdrstop
    
    #include "Unit2.h"
    //---------------------------------------------------------------------------
    #pragma package(smart_init)
    #pragma resource "*.dfm"
    TForm2 *Form2;
    //---------------------------------------------------------------------------
    __fastcall TForm2::TForm2(TComponent* Owner)
        : TForm(Owner)
    {
        m_buttonPtr = NULL;
    }
    
    void __fastcall TForm2::SetButtonPointer(TButton *btnPtr)
    {
        m_buttonPtr = btnPtr;
    }
    
    void __fastcall TForm2::Button1Click(TObject *Sender)
    {
        if (m_buttonPtr == NULL) {
            return;
        }
    
        m_buttonPtr->Click();
    }
    //---------------------------------------------------------------------------
    

    실행





    __closure



    Button의 포인터가 아니라, 함수 그 자체를 건네주는 방법이 본래의 방법일지도 모른다.

    좋은 웹페이지 즐겨찾기