Re: [問題] VC++ WinForm開關按鈕

看板C_and_CPP (C/C++)作者 (好人超)時間16年前 (2010/01/06 13:13), 編輯推噓1(100)
留言1則, 1人參與, 最新討論串2/2 (看更多)
給一些小小的建議: 1. 非不得已,請不要使用全域變數 2. 在寫視窗程式時,重覆性的動作不宜使用迴圈 + sleep 容易使得視窗卡死無反應,而應該改用 timer 來周期性的執行 3. 要取得鍵盤的按鍵,也該使用視窗事件,而非 getche() 函式 因此程式碼可以這麼改: Void button1_Click(Object^ sender, EventArgs^ e) { if(!isStart) { isStart=true; this->button1->Text = L"Stop"; this->timer1->Enabled = true; } else { isStart=false; this->button1->Text = L"Start"; this->timer1->Enabled = false; } } /* 在 timer1->Enabled 被設為 true 後,這個函式會被周期性的呼叫 */ Void timer1_Tick(Object^ sender, EventArgs^ e) { Point p = this->pictureBox1->Location; p.X--; this->pictureBox1->Location = p; } 至於怎麼加入一個 timer,怎麼控制它在 enable 之後的發生頻率 就交給你自己去研究一下囉 (畢竟很簡單) 總之,請記得,視窗程式必須寫成「事件驅動」(event driven) 也就是「發生某件事→做某些事」的模式 -- ※ 發信站: 批踢踢實業坊(ptt.cc) ◆ From: 140.117.171.46 ※ 編輯: james732 來自: 140.117.171.46 (01/06 13:14)

01/06 13:57, , 1F
你真是好人qq 終於看到回應了
01/06 13:57, 1F
文章代碼(AID): #1BH1lipy (C_and_CPP)
文章代碼(AID): #1BH1lipy (C_and_CPP)