C++Builder Programming Forum
C++Builder  |  Delphi  |  FireMonkey  |  C/C++  |  Free Pascal  |  Firebird
볼랜드포럼 BorlandForum
 경고! 게시물 작성자의 사전 허락없는 메일주소 추출행위 절대 금지
C++빌더 포럼
Q & A
FAQ
팁&트릭
강좌/문서
자료실
컴포넌트/라이브러리
메신저 프로젝트
볼랜드포럼 홈
헤드라인 뉴스
IT 뉴스
공지사항
자유게시판
해피 브레이크
공동 프로젝트
구인/구직
회원 장터
건의사항
운영진 게시판
회원 메뉴
북마크
볼랜드포럼 광고 모집

C++빌더 팁&트릭
C++Builder Programming Tip&Tricks
[648] [MessageBox] 메세지 박스 일정시간후 자동 close 하기
장성호 [nasilso] 10179 읽음    2007-05-25 13:28
프로그램에서 가끔 사용자에게 메세지를 보여준후 일정시간후 자동으로  닫게 하는 기능이 필요할때가 있습니다.
개인적으로 예전에 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

그럼...
장성호 [nasilso]   2007-05-28 01:59 X
문제를 지적해 주셔서 감사합니다.

심각한 문제가 있었네요
CreateMessageDialog를 통해 생성된 TMessageForm이 Close된후
반드시 delete를 해줘야 하네요
그 코드가 빠졌네요  쩝쩝 !

삭제하는 코드를 한줄씩 넣어서 수정했습니다.
장성호 [nasilso]   2007-06-14 01:21 X
헐~~!
MessageBoxTimeOut   Window API에서 지원하는 군요
쩝쩝 오늘에서야 알았습니다.

http://www.codeproject.com/cpp/MessageBoxTimeout.asp

+ -

관련 글 리스트
648 [MessageBox] 메세지 박스 일정시간후 자동 close 하기 장성호 10179 2007/05/25
Google
Copyright © 1999-2015, borlandforum.com. All right reserved.