2007年12月12日星期三

通过OnSysCommand关闭程序控制

在MainFrame利用WINDOWS的消息循环机制,拦截关闭窗口消息:
1:
BEGIN_MESSAGE_MAP(CMainFrame, CFrameWnd)
//{{AFX_MSG_MAP(CMainFrame)
//手动添加:
ON_MESSAGE(WM_SYSCOMMAND,OnSysCommand)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()

2:
void CMainFrame::OnSysCommand(UINT nID,LPARAM lParam)
{
if((nID & 0xFFFFFFF0) ==SC_CLOSE)
{
if(AfxMessageBox("Warning: Wollen Sie die Pozess wicklich abbrechen?",MB_OKCANCEL)!=IDOK)
return;
//如果关闭程序 do something:..........
}
CWnd::OnSysCommand(nID,lParam);
}


3.在CDialog中:

void CDBinfoSmall::OnSysCommand(UINT nID, LPARAM lParam)
{
//AfxMessageBox("mouse call");
if((nID & 0xFFFFFFF0) == SC_CLOSE)
{

if(AfxMessageBox("Warning: Wollen Sie die Pozess wicklich abbrechen?",MB_YESNO) == IDNO)
{

//AfxMessageBox( GetCommandLine() );
return;

}
else
{
//---- use the View pointobject, close the m_DaoDatabase;
CMDIFrameWnd *pFrame = (CMDIFrameWnd*)AfxGetApp()->m_pMainWnd;

// Get the active MDI child window.
CMDIChildWnd *pChild = (CMDIChildWnd *) pFrame->GetActiveFrame();

// or CMDIChildWnd *pChild = pFrame->MDIGetActive();

// Get the active view attached to the active MDI child window.
CDbinfoView *pView = (CDbinfoView *) pChild->GetActiveView();

//close the source db m_DaoDatabase by using pView
if( pView->m_DaoDatabase.IsOpen())
{
pView->m_DaoDatabase.Close();
//
//AfxMessageBox("m_DaoDatabase.Close();");

}
if (plc.existFile(plc.InstallDir + "\\Database\\apollo.ldb"))
DeleteFile(plc.InstallDir + "\\Database\\apollo.ldb");
//close the target db in cdbinfolistct,
//pView->m_pListCt.DB_Close(); //runtime error!!
//----
if( !TerminateProcess(GetCurrentProcess(),0) )
AfxMessageBox("Fehlermeldung: Programm kann nicht terminiert werden, bitte versuchen Sie Task-Manager!");

}

}

/////////////////////////////////////////////////
//
// * Making other messages be answered, e.SC_MOVE
//
/////////////////////////////////////////////////
CDialog::OnSysCommand(nID,lParam);

}


4.OnSysCommand() 与 OnLButtonDown()在CDialog中响应区别:

OnSysCommand 响应了对对话框标题栏的鼠标点击
OnLButtonDown 响应了对对话框框体的鼠标点击(不包括标题栏)

没有评论: