---- 為何在Modal Dialog 下就無法收到OnClose ---- |
答題得分者是:ccchen
|
davidchang
一般會員 發表:4 回覆:10 積分:2 註冊:2002-08-08 發送簡訊給我 |
在Main Form開啟Form2, 然後按下Form2上的Button, 該Button會執行一個很久迴圈
若在此時我按Form2右上角的關閉系窗會無回應, 必須等到那個迴圈做完才能關閉Form2
我已加上Application->ProcessMessage(); 但還是沒用
很奇怪的是最大及最小化都可以馬上看出結果, 但唯獨close不能馬上作用
下面附上簡單的測試程式, 請各位大大幫忙看看, 謝謝 PS:若是用Form2->Show(); 則沒有上述問題
//----以下為Unit1.cpp-------------------------------------------------- #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->ShowModal(); //若是用Form2->Show(); 則沒有問題 } //-----以下為Unit2.cpp------------------------------------------------ #include "Unit2.h" //--------------------------------------------------------------------------- #pragma package(smart_init) #pragma resource "*.dfm" TForm2 *Form2; //--------------------------------------------------------------------------- __fastcall TForm2::TForm2(TComponent* Owner) : TForm(Owner) { } //--------------------------------------------------------------------------- void __fastcall TForm2::Button1Click(TObject *Sender) { for(int i=0;i<100;i ) { Sleep(100); Application->ProcessMessages(); Label1->Caption=i; //請在此間按close試試 } }這個問題我已在深度論壇問過了 有人回的答案是Modal 與 Modaless 的差別 但我還是找不到解法 我要的是如果一定要用ShowModal的方式 如何才能立刻收到OnClose()的Event |
RaynorPao
版主 發表:139 回覆:3622 積分:7025 註冊:2002-08-12 發送簡訊給我 |
davidchang 你好:
依照你目前的解決方法,再加上以下修改的 code
希望能夠更符合你的需求~~參考看看吧
< class="code">
//----以下為Unit1.cpp--------------------------------------------------------
#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->Show();
} //-----以下為Unit2.cpp-------------------------------------------------------
#include "Unit2.h"
bool bf2;
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TForm2 *Form2;
//---------------------------------------------------------------------------
__fastcall TForm2::TForm2(TComponent* Owner)
: TForm(Owner)
{
}
//---------------------------------------------------------------------------
void __fastcall TForm2::Button1Click(TObject *Sender)
{
for(int i=0; i<100; i )
{
if(!bf2) break;
Sleep(100);
Application->ProcessMessages();
Label1->Caption=i;
}
}
//--------------------------------------------------------------------------- void __fastcall TForm2::FormClose(TObject *Sender, TCloseAction &Action)
{
bf2=false;
Label1->Caption="";
} //---------------------------------------------------------------------------
void __fastcall TForm2::FormDeactivate(TObject *Sender)
{
if(bf2) SetFocus();
}
//---------------------------------------------------------------------------
void __fastcall TForm2::FormShow(TObject *Sender)
{
bf2=true;
Label1->Caption="";
}
//--------------------------------------------------------------------------- 備註:
如果覺得這個 solution 不錯的話
不妨跟網友們一起分享你的辦法喔 --
------
-- 若您已經得到滿意的答覆,請適時結案!! -- -- 欲知前世因,今生受者是;欲知來世果,今生做者是 -- -- 一切有為法,如夢幻泡影,如露亦如電,應作如是觀 -- |
RaynorPao
版主 發表:139 回覆:3622 積分:7025 註冊:2002-08-12 發送簡訊給我 |
引言: RaynorPao大大 ... 好像有點問題 loop 裡的 if(bf2) break; ... 應該是 if (!bf2) break; 吧? 不好意思 ... >>< face="Verdana, Arial, Helvetica"> FrederickPau 你好: 謝謝你的提醒~~打太快錯了< > 上面的程式碼已經修正 如果還有錯要告訴我喔< > -- Enjoy Researching & Developing --
------
-- 若您已經得到滿意的答覆,請適時結案!! -- -- 欲知前世因,今生受者是;欲知來世果,今生做者是 -- -- 一切有為法,如夢幻泡影,如露亦如電,應作如是觀 -- |
jessechan
版主 發表:109 回覆:394 積分:254 註冊:2002-04-05 發送簡訊給我 |
|
ccchen
版主 發表:61 回覆:940 積分:1394 註冊:2002-04-15 發送簡訊給我 |
application->processmessage只是在loop中可接受其他message並不會幫你結束loop. 如下試試吧 unit2.h
class TForm2 : public TForm { __published: // IDE-managed Components TButton *Button1; TLabel *Label1; TButton *Button2; void __fastcall Button1Click(TObject *Sender); void __fastcall FormActivate(TObject *Sender); private: // User declarations public: // User declarations __fastcall TForm2(TComponent* Owner); void __fastcall MyWndProc(Messages::TMessage &Message); };unit2.cpp bool canclose; TForm2 *Form2; //--------------------------------------------------------------------------- __fastcall TForm2::TForm2(TComponent* Owner) : TForm(Owner) { } //--------------------------------------------------------------------------- void __fastcall TForm2::Button1Click(TObject *Sender) { for(int i=0;i<100;i ) { if (canclose) break; Sleep(100); Application->ProcessMessages(); Label1->Caption=i; } } //--------------------------------------------------------------------------- void __fastcall TForm2::MyWndProc(Messages::TMessage &Message) { if (Message.Msg == WM_CLOSE) { canclose=True; Close(); } else WndProc(Message); } void __fastcall TForm2::FormActivate(TObject *Sender) { this->WindowProc=MyWndProc; } //--------------------------------------------------------------------------- |
davidchang
一般會員 發表:4 回覆:10 積分:2 註冊:2002-08-08 發送簡訊給我 |
|
davidchang
一般會員 發表:4 回覆:10 積分:2 註冊:2002-08-08 發送簡訊給我 |
試出另一個方法, 原理是一樣的, 給大家參考 unit2.h
//--------------------------------------------------------------------------- class TForm2 : public TForm { __published: // IDE-managed Components TLabel *Label1; TButton *Button1; TButton *Button2; TTimer *Timer1; void __fastcall Button1Click(TObject *Sender); void __fastcall Button2Click(TObject *Sender); void __fastcall FormClose(TObject *Sender, TCloseAction &Action); void __fastcall Timer1Timer(TObject *Sender); void __fastcall FormActivate(TObject *Sender); private: // User declarations public: // User declarations __fastcall TForm2(TComponent* Owner); void __fastcall MyClose(TMessage &Message); BEGIN_MESSAGE_MAP MESSAGE_HANDLER(WM_CLOSE,TMessage,MyClose) END_MESSAGE_MAP(inherited) }; unit2.cpp bool canclose=false; //--------------------------------------------------------------------------- __fastcall TForm2::TForm2(TComponent* Owner) : TForm(Owner) { } //--------------------------------------------------------------------------- void __fastcall TForm2::Button1Click(TObject *Sender) { for(int i=0;i<100;i ) { Sleep(100); if(canclose) break; Application->ProcessMessages(); Label1->Caption=i; } } //--------------------------------------------------------------------------- void __fastcall TForm2::MyClose(TMessage &Message) { canclose=true; Close(); } //--------------------------------------------------------------------------- |
RaynorPao
版主 發表:139 回覆:3622 積分:7025 註冊:2002-08-12 發送簡訊給我 |
引言: 用ccchen的方法就可以達到我要的功能 所以分數就給他囉 不過還是要謝謝各位的幫忙... 尤其是RaynorPao... >>< face="Verdana, Arial, Helvetica"> davidchang 你好: 放心~~我不會介意的< > 也很感謝你的熱心分享< > -- Enjoy Researching & Developing --
------
-- 若您已經得到滿意的答覆,請適時結案!! -- -- 欲知前世因,今生受者是;欲知來世果,今生做者是 -- -- 一切有為法,如夢幻泡影,如露亦如電,應作如是觀 -- |
本站聲明 |
1. 本論壇為無營利行為之開放平台,所有文章都是由網友自行張貼,如牽涉到法律糾紛一切與本站無關。 2. 假如網友發表之內容涉及侵權,而損及您的利益,請立即通知版主刪除。 3. 請勿批評中華民國元首及政府或批評各政黨,是藍是綠本站無權干涉,但這裡不是政治性論壇! |