您好:
下面範例讓您參考
Unit1: //--------------------------------------------------------------------------- #include
#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)
{
Form2->FormStyle=fsStayOnTop;//將Fomr2設在最頂端
Form2->Show();
}
//---------------------------------------------------------------------------
void __fastcall TForm1::FormActivate(TObject *Sender)
{
sw=false;//初始開關為關
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Button2Click(TObject *Sender)
{
sw=!sw;//反向sw中的值
}
//--------------------------------------------------------------------------- Unit1.h //--------------------------------------------------------------------------- #ifndef Unit1H
#define Unit1H
//---------------------------------------------------------------------------
#include
#include
#include
#include <Forms.hpp>
//---------------------------------------------------------------------------
class TForm1 : public TForm
{
__published: // IDE-managed Components
TButton *Button1;
TButton *Button2;
void __fastcall Button1Click(TObject *Sender);
void __fastcall FormActivate(TObject *Sender);
void __fastcall Button2Click(TObject *Sender);
private: // User declarations
public: // User declarations
bool sw;//建立溝通變數
__fastcall TForm1(TComponent* Owner);
};
//---------------------------------------------------------------------------
extern PACKAGE TForm1 *Form1;
//---------------------------------------------------------------------------
#endif Unit2.cpp //--------------------------------------------------------------------------- #include
#pragma hdrstop #include "Unit2.h"
#include "Unit1.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TForm2 *Form2;
//---------------------------------------------------------------------------
__fastcall TForm2::TForm2(TComponent* Owner)
: TForm(Owner)
{
}
//--------------------------------------------------------------------------- void __fastcall TForm2::FormActivate(TObject *Sender)
{
for (int i=0;;i )//設立一個無窮回圈直到開關按下關
{
Label1->Caption=IntToStr(i);
Sleep(300);//延遲執行,不然看不見數字在動
Application->ProcessMessages();//讓程式發出更新訊息,不然畫面會當掉
if (Form1->sw==false) break;//如果按下關時,就中斷回圈
}
}
//---------------------------------------------------------------------------
發表人 - supman 於 2005/05/10 19:44:25