你可以另外寫一個h檔,把你要的全域變數放在裡面,
就可以讓這兩個form來使用。
如:
File1.h
#ifndef File1H
#define File1H int sInt; #endif
Unit1.cpp
#include
#pragma hdrstop #include "Unit1.h"
#include "Unit2.h"
#include "File1.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TForm1 *Form1;
//---------------------------------------------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner)
: TForm(Owner)
{
sInt = 1;
}
//--------------------------------------------------------------------------- void __fastcall TForm1::Button1Click(TObject *Sender)
{
Form2->Show();
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Button2Click(TObject *Sender)
{
ShowMessage(IntToStr(sInt));
sInt = 3;
}
Unit2.cpp
#include
#pragma hdrstop #include "Unit2.h"
#include "Unit1.h"
extern sInt;
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TForm2 *Form2;
//---------------------------------------------------------------------------
__fastcall TForm2::TForm2(TComponent* Owner)
: TForm(Owner)
{
}
//--------------------------------------------------------------------------- void __fastcall TForm2::Button1Click(TObject *Sender)
{
ShowMessage(IntToStr(sInt));
sInt = 2; }
還有另一個方法,兩個form直接互相存取彼此的變數、結構和vcl~
參考以下文章: