全國最多中醫師線上諮詢網站-台灣中醫網
發文 回覆 瀏覽次數:870
推到 Plurk!
推到 Facebook!

如何用一個Button當開關

尚未結案
lunen
一般會員


發表:4
回覆:3
積分:1
註冊:2005-05-09

發送簡訊給我
#1 引用回覆 回覆 發表時間:2005-05-10 17:35:32 IP:61.221.xxx.xxx 未訂閱
請問一下我們想用Form2裡的一個Button連結另一個Form1裡的PintBox裡的球讓球亂數跑! 請問如何用一個Form2裡的Button當開關,用if判斷式來編寫,當等於1即開啟(球就會跑);等於0不執行(球不會跑)??
supman
尊榮會員


發表:29
回覆:770
積分:924
註冊:2002-04-22

發送簡訊給我
#2 引用回覆 回覆 發表時間:2005-05-10 19:43:24 IP:203.204.xxx.xxx 未訂閱
您好: 下面範例讓您參考
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
lunen
一般會員


發表:4
回覆:3
積分:1
註冊:2005-05-09

發送簡訊給我
#3 引用回覆 回覆 發表時間:2005-05-14 03:03:59 IP:59.113.xxx.xxx 未訂閱
引言: 您好: 下面範例讓您參考
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
謝謝喔~~我會再試試的~~有問題再上來跟你討教~~ 感激不盡
系統時間:2024-11-22 7:39:56
聯絡我們 | Delphi K.Top討論版
本站聲明
1. 本論壇為無營利行為之開放平台,所有文章都是由網友自行張貼,如牽涉到法律糾紛一切與本站無關。
2. 假如網友發表之內容涉及侵權,而損及您的利益,請立即通知版主刪除。
3. 請勿批評中華民國元首及政府或批評各政黨,是藍是綠本站無權干涉,但這裡不是政治性論壇!