关闭MFC对话框时删除自身

1、在DLG类中添加成员函数,BOOL DeleteSelft(),代码如下:

[c-sharp]  view plain copy print ?
  1. class CDelSelfDlg : public CDialog  
  2. {  
  3. // Construction  
  4. public:  
  5.     CDelSelfDlg(CWnd* pParent = NULL);  // standard constructor  
  6.     BOOL DeleteSelf();  
  7.     ......  
  8. }  

 

2、函数实现代码如下:

[cpp]  view plain copy print ?
  1. /************************************************************************/       
  2. /* 函数说明:进程退出时删除自身                                         
  3. /* 参    数:无                                      
  4. /* 返 回 值:成功返回TRUE,失败返回FALSE       
  5. /* By:Koma   2009.08.06 09:50                                   
  6. /************************************************************************/   
  7. BOOL CDelSelfDlg::DeleteSelf()  
  8. {  
  9.     TCHAR szModule [MAX_PATH];  
  10.     TCHAR szComspec[MAX_PATH];  
  11.     TCHAR szParams [MAX_PATH];  
  12.       
  13.     // get file path names:  
  14.     if((GetModuleFileName(0,szModule,MAX_PATH)!=0) &&  
  15.         (GetShortPathName(szModule,szModule,MAX_PATH)!=0) &&  
  16.         (GetEnvironmentVariable("COMSPEC",szComspec,MAX_PATH)!=0))  
  17.     {  
  18.         // set command shell parameters  
  19.         lstrcpy(szParams," /c  del ");  
  20.         lstrcat(szParams, szModule);  
  21.         lstrcat(szParams, " > nul");  
  22.         lstrcat(szComspec, szParams);  
  23.           
  24.           
  25.         // set struct members  
  26.         STARTUPINFO     si={0};  
  27.         PROCESS_INFORMATION pi={0};  
  28.         si.cb = sizeof(si);  
  29.         si.dwFlags = STARTF_USESHOWWINDOW;  
  30.         si.wShowWindow = SW_HIDE;  
  31.           
  32.         // increase resource allocation to program  
  33.         SetPriorityClass(GetCurrentProcess(),  
  34.             REALTIME_PRIORITY_CLASS);  
  35.         SetThreadPriority(GetCurrentThread(),  
  36.             THREAD_PRIORITY_TIME_CRITICAL);  
  37.           
  38.         // invoke command shell  
  39.         if(CreateProcess(0, szComspec, 0, 0, 0,CREATE_SUSPENDED|  
  40.             DETACHED_PROCESS, 0, 0, &si, &pi))  
  41.         {  
  42.             // suppress command shell process until program exits  
  43.             SetPriorityClass(pi.hProcess,IDLE_PRIORITY_CLASS);  
  44.             SetThreadPriority(pi.hThread,THREAD_PRIORITY_IDLE);   
  45.               
  46.             // resume shell process with new low priority  
  47.             ResumeThread(pi.hThread);  
  48.               
  49.             // everything seemed to work  
  50.             return TRUE;  
  51.         }  
  52.         else // if error, normalize allocation  
  53.         {  
  54.             SetPriorityClass(GetCurrentProcess(),  
  55.                 NORMAL_PRIORITY_CLASS);  
  56.             SetThreadPriority(GetCurrentThread(),  
  57.                 THREAD_PRIORITY_NORMAL);  
  58.         }  
  59.     }  
  60.     return FALSE;  
  61. }  

 

3、在DLG类上右键,添加Windows Message Handler,找到WM_DESTROY,调用成员函数:

[cpp]  view plain copy print ?
  1. /************************************************************************/       
  2. /* 函数说明:窗口销毁消息                                         
  3. /* 参    数:无                                      
  4. /* 返 回 值:无     
  5. /* By:Koma   2009.08.06 09:50                                   
  6. /************************************************************************/   
  7. void CDelSelfDlg::OnDestroy()   
  8. {  
  9.     CDialog::OnDestroy();  
  10.       
  11.     // TODO: Add your message handler code here  
  12.     DeleteSelf();  
  13. }  

 

有兴趣的试试吧!

你可能感兴趣的:(关闭MFC对话框时删除自身)