프로그램에서 가끔 사용자에게 메세지를 보여준후 일정시간후 자동으로 닫게 하는 기능이 필요할때가 있습니다.
개인적으로 예전에 CreateMessageDialog 를 이용하는 방법을 생각하지 못해
TForm으로 하나 만들었었었죠
그런데 사용자 정의 메세지 박스를 이용하여 간단히 구현할 수 있더군요
[메세지 박스 자동 닫기]
TForm *frm;
bool bShow;
int iTime;
bool bAutoClose;
void __fastcall TForm1::Button1Click(TObject *Sender)
{
// Create the MessageDialog
frm= CreateMessageDialog("HELLOWORLD",mtInformation , mbOKCancel);
frm->Caption="확인";
iTime=0;
bShow=true;
bAutoClose=false;
int rslt=frm->ShowModal();
bShow=false;
delete frm;
if(bAutoClose)
ShowMessage("Auto Closed");
else
{
if( rslt == mrOk )
ShowMessage("OK Pressed!");
else
ShowMessage("Cancel pressed!");
}
bShow=false;
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Timer1Timer(TObject *Sender)
{
if(bShow)
{
iTime++;
if(iTime>2)
{
frm->Close();
bAutoClose=true;
bShow=false;
}
}
}
//--------------------------------------------------------------------------
//============================================================================
[자동 Close 메세지 박스 클래스]
위 내용을 간단히 클래스로 만들어 봣습니다.
//---------------------------------------------------------------------------
class TMsgForm
{
private: // User declarations
TForm *frm;
TTimer *timer;
int iTimeCnt,iTime;
bool bShow;
void __fastcall TMsgForm::TimerTimer(TObject *Sender);
public: // User declarations
__fastcall TMsgForm::TMsgForm(TComponent* Owner);
__fastcall TMsgForm::~TMsgForm();
//------------------------------
int __fastcall TMsgForm::ShowMessage(String cap,String msg, TMsgDlgType DlgType, TMsgDlgButtons Buttons,int iShowTime);
int __fastcall TMsgForm::ShowMessage(String cap,String msg, TMsgDlgType DlgType, TMsgDlgButtons Buttons);
};
//---------------------------------------------------------------------------
__fastcall TMsgForm::TMsgForm(TComponent* Owner)
{
timer=new TTimer(Owner);
timer->OnTimer=TimerTimer;
timer->Enabled=true;
bShow=false;
}
//--------------------------------------------------------------------------
__fastcall TMsgForm::~TMsgForm()
{
delete timer;
}
//--------------------------------------------------------------------------
void __fastcall TMsgForm::TimerTimer(TObject *Sender)
{
if(bShow)
{
iTimeCnt++;
if(iTimeCnt>=iTime)
{
frm->Close();
bShow=false;
}
}
}
//--------------------------------------------------------------------------
int __fastcall TMsgForm::ShowMessage(String cap,String msg, TMsgDlgType DlgType, TMsgDlgButtons Buttons,int iShowTime)
{
// Create the MessageDialog
frm = CreateMessageDialog(msg,DlgType , Buttons);
frm->Caption=cap;
iTime=iShowTime;
iTimeCnt=0;
bShow=true;
int rslt=frm->ShowModal();
bShow=false;
delete frm;
return rslt;
}
//--------------------------------------------------------------------------
int __fastcall TMsgForm::ShowMessage(String cap,String msg, TMsgDlgType DlgType, TMsgDlgButtons Buttons)
{
frm = CreateMessageDialog(msg,DlgType , Buttons);
frm->Caption=cap;
bShow=false;
int rslt=frm->ShowModal();
bShow=false;
delete frm;
return rslt;
}
//---------------------------------------------------------------------------
//============================================================================
사용예
void __fastcall TForm1::Button2Click(TObject *Sender)
{
//
TMsgForm *msg=new TMsgForm(this);
msg->ShowMessage("확 인","메세지를 확인하세요",mtInformation , mbOKCancel,2); //2초후 자동 close
delete msg;
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Button3Click(TObject *Sender)
{
TMsgForm *msg=new TMsgForm(this);
msg->ShowMessage("확 인","메세지를 확인하세요",mtInformation , mbOKCancel);
delete msg;
}
//---------------------------------------------------------------------------
그럼 즐거운 하루 되세요
수정-09-03-06
댓글에 있는 글은 검색이 안되어서 댓글의 중요한 내용을 본문으로 옮깁니다.
MessagBoxTimeOut
MessagBoxTimeOut 이라는 함수가 User32.dll에 있습니다.
Undocument 함수이구요
함수 이름 그래도 MessageBox 를 띄워두고 일정시간 지나면
자동으로 Close됩니다.
MessagBoxTimeOutA , MessagBoxTimeOutW
참조
http://edn.embarcadero.com/article/32736
http://www.codeproject.com/KB/cpp/MessageBoxTimeout.aspx
http://www.howto.pe.kr/zboard/zboard.php?id=delphi_tiptrick&page=1&page_num=40&select_arrange=vote&desc=&sn=off&ss=on&sc=on&keyword=&category=&no=1009
그럼...
심각한 문제가 있었네요
CreateMessageDialog를 통해 생성된 TMessageForm이 Close된후
반드시 delete를 해줘야 하네요
그 코드가 빠졌네요 쩝쩝 !
삭제하는 코드를 한줄씩 넣어서 수정했습니다.