MFC:
双击窗口左上角的图标可以关闭窗口,怎样才能截获它?

解决方案 »

  1.   

    重载OnClose函数,
    添加
    if(CanClose)注意OnOK和OnClose函数,也要加这句代码
      

  2.   

    让关闭按钮不可用
    CMenu* pmenu=GetSystemMenu(false);
    ASSERT(pmenu!=NULL);
    pmenu->DeleteMenu(SC_CLOSE,MF_BYCOMMAND);加在
    对话框程序: OnInitDialog中
    文档视图  : OnCreate 中
      

  3.   

    或者重载对话框的消息处理函数,类似这样:
     PreTranslateMessage(MSG* pMsg)
    {
             if (pMsg->message == WM_KEYDOWN)
    {
    if(pMsg->wParam == VK_RETURN)
    return TRUE;
    }
             return CDialog::PreTranslateMessage(pMsg);
    }
      

  4.   

    void CMainFrame::OnClose() 
    {
    if(IDOK != MessageBox("你要退出吗?\n点确定退出.","你要退出吗?",MB_OKCANCEL))
    return  ;
    CFrameWnd::OnClose();
    }
    同时可以载获最大化按钮从菜单中退出没有截获
      

  5.   

    在应用的InitInstance中
    .....
    m_pMainWnd->ShowWindow(SW_SHOW);
    m_pMainWnd->UpdateWindow();
    ///加在这后面
    CMenu *pMenu=AfxGetMainWnd()->GetSystemMenu(FALSE);
    if(pMenu)
        pMenu->EnableMenuItem(SC_CLOSE,MF_BYCOMMAND|MF_GRAYED);