回 帖 发 新 帖 刷新版面

主题:请教:如何拆分cpp

一个Form里包含:一个Label1、一个Memo1、一个Edit、两个SpeedButton;代码写在A.cpp 
以下是A.cpp内容: 
//--------------------------------------------------------------------------- 

#include <vcl.h> 
#pragma hdrstop 

#include "A.h" 
//--------------------------------------------------------------------------- 
#pragma package(smart_init) 
#pragma resource "*.dfm" 
TForm1 *Form1; 
AnsiString A,B,C; 
//--------------------------------------------------------------------------- 
__fastcall TForm1::TForm1(TComponent* Owner) 
        : TForm(Owner) 


//--------------------------------------------------------------------------- 
//------------------按钮一---------------------------------------------------- 
//--------------------------------------------------------------------------- 
void __fastcall TForm1::SpeedButton1Click(TObject *Sender) 

      int sn=0; 

          do 
            { 
              EditDZX->Text=Memo1->Lines->Strings[sn]; 
              EditDZX->SelStart=0; 
              EditDZX->SelLength=1; 
                A=EditDZX->SelText; 
              EditDZX->SelStart=1; 
              EditDZX->SelLength=1; 
                B=EditDZX->SelText; 
              EditDZX->SelStart=2; 
              EditDZX->SelLength=1; 
                C=EditDZX->SelText; 
              if(A==B||B==C||A==C) 
                {Memo1->Lines->Delete(sn);sn--;} 
              sn++; 
            }while(sn <Memo1->Lines->Count); 


      Label1->Caption="按钮1执行完毕!共 "+IntToStr(Memo1->Lines->Count)+" 行"; 

//--------------------------------------------------------------------------- 
//-------------按钮二--------------------------------------------------------- 
//--------------------------------------------------------------------------- 
void __fastcall TForm1::SpeedButton2Click(TObject *Sender) 

      int sn=0; 

          do 
            { 
              EditDZX->Text=Memo1->Lines->Strings[sn]; 
              EditDZX->SelStart=0; 
              EditDZX->SelLength=1; 
                A=EditDZX->SelText; 
              EditDZX->SelStart=1; 
              EditDZX->SelLength=1; 
                B=EditDZX->SelText; 
              EditDZX->SelStart=2; 
              EditDZX->SelLength=1; 
                C=EditDZX->SelText; 
              if(A!=B&&B!=C&&A!=C) 
                {Memo1->Lines->Delete(sn);sn--;} 
              sn++; 
            }while(sn <Memo1->Lines->Count); 

      Label1->Caption="按钮2执行完毕!共 "+IntToStr(Memo1->Lines->Count)+" 行"; 

//--------------------------------------------------------------------------- 
//--------------------------------------------------------------------------- 

以下是A.h内容: 
//--------------------------------------------------------------------------- 

#ifndef AH 
#define AH 
//--------------------------------------------------------------------------- 
#include <Classes.hpp> 
#include <Controls.hpp> 
#include <StdCtrls.hpp> 
#include <Forms.hpp> 
#include <Buttons.hpp> 
//--------------------------------------------------------------------------- 
class TForm1 : public TForm 

__published: // IDE-managed Components 
        TEdit *EditDZX; 
        TSpeedButton *SpeedButton1; 
        TSpeedButton *SpeedButton2; 
        TMemo *Memo1; 
        TLabel *Label1; 
        void __fastcall SpeedButton1Click(TObject *Sender); 
        void __fastcall SpeedButton2Click(TObject *Sender); 
private: // User declarations 
public: // User declarations 
        __fastcall TForm1(TComponent* Owner); 
}; 
//--------------------------------------------------------------------------- 
extern PACKAGE TForm1 *Form1; 
//--------------------------------------------------------------------------- 
#endif 

现在我要实现:把按钮一的代码放在A.cpp中,把按钮二的代码放在B.cpp中,该如何操作?

回复列表 (共1个回复)

沙发

创建B.cpp,内容为:
#include <vcl.h> 
#pragma hdrstop 

#include "A.h" 
//--------------------------------------------------------------------------- 
//-------------按钮二--------------------------------------------------------- 
//--------------------------------------------------------------------------- 
void __fastcall TForm1::SpeedButton2Click(TObject *Sender) 

      int sn=0; 

          do 
            { 
              EditDZX->Text=Memo1->Lines->Strings[sn]; 
              EditDZX->SelStart=0; 
              EditDZX->SelLength=1; 
                A=EditDZX->SelText; 
              EditDZX->SelStart=1; 
              EditDZX->SelLength=1; 
                B=EditDZX->SelText; 
              EditDZX->SelStart=2; 
              EditDZX->SelLength=1; 
                C=EditDZX->SelText; 
              if(A!=B&&B!=C&&A!=C) 
                {Memo1->Lines->Delete(sn);sn--;} 
              sn++; 
            }while(sn <Memo1->Lines->Count); 

      Label1->Caption="按钮2执行完毕!共 "+IntToStr(Memo1->Lines->Count)+" 行"; 

//--------------------------------------------------------------------------- 
//--------------------------------------------------------------------------- 





同时把A.cpp的SpeedButton2Click函数定义给删了就可以了:)
只是以后你双击SpeedButton2的OnClick事件时,CB会告诉你找不到“SpeedButton2Click”函数:)

我来回复

您尚未登录,请登录后再回复。点此登录或注册