1. 程式人生 > >vc 簡單遮蔽alt+F4鍵、ESC和ENTER鍵

vc 簡單遮蔽alt+F4鍵、ESC和ENTER鍵

void   CMainFrame::OnSysCommand(UINT   nID,   LPARAM   lParam)    
  {  
  if(nID==SC_CLOSE&&lParam==0)  
  {  
  return;  
  }  
  CFrameWnd::OnSysCommand(nID,   lParam);  
  }

按下ESC和ENTER鍵時禁止關閉對話方塊 
    方法一:

     (1) 過載OnCancel和OnOk,遮蔽其中的CDialog::OnCancel()和CDialog::OnOk();
     (2) 新增以下程式碼
     void CTest6Dlg::OnSysCommand(UINT nID, LPARAM lParam)
     {
         if ((nID & 0xFFF0) == IDM_ABOUTBOX)
          {
             CAboutDlg dlgAbout;   //if you have an about dialog
              dlgAbout.DoModal();
          }
          else if ((nID & 0xFFF0) == SC_CLOSE)
          {
              //使用者點選右上角"X"
              EndDialog(IDOK);
     
          }
          else
          {
              CDialog::OnSysCommand(nID, lParam);
          }
     }

    方法二:

     BOOL CTest6Dlg::PreTranslateMessage(MSG* pMsg)
     {
          if ( pMsg->message == WM_KEYDOWN )
          {
               switch(pMsg->wParam)
               {
               case VK_ESCAPE:
                return TRUE; //直接返回TRUE
                break;
               case VK_RETURN:
                return TRUE;
                break;
               }
          }
          return CDialog::PreTranslateMessage(pMsg);
     }