zoukankan      html  css  js  c++  java
  • 关闭MFC对话框时删除自身

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

    class CDelSelfDlg : public CDialog
    {
    // Construction
    public:
    	CDelSelfDlg(CWnd* pParent = NULL);	// standard constructor
    	BOOL DeleteSelf();
    	......
    }


    2、函数实现代码如下:

    /************************************************************************/     
    /* 函数说明:进程退出时删除自身                                        
    /* 参    数:无                                     
    /* 返 回 值:成功返回TRUE,失败返回FALSE      
    /* By:Koma   2009.08.06 09:50                                  
    /************************************************************************/ 
    BOOL CDelSelfDlg::DeleteSelf()
    {
    	TCHAR szModule [MAX_PATH];
    	TCHAR szComspec[MAX_PATH];
    	TCHAR szParams [MAX_PATH];
    	
    	// get file path names:
    	if((GetModuleFileName(0,szModule,MAX_PATH)!=0) &&
    		(GetShortPathName(szModule,szModule,MAX_PATH)!=0) &&
    		(GetEnvironmentVariable("COMSPEC",szComspec,MAX_PATH)!=0))
    	{
    		// set command shell parameters
    		lstrcpy(szParams," /c  del ");
    		lstrcat(szParams, szModule);
    		lstrcat(szParams, " > nul");
    		lstrcat(szComspec, szParams);
    		
    		
    		// set struct members
    		STARTUPINFO		si={0};
    		PROCESS_INFORMATION	pi={0};
    		si.cb = sizeof(si);
    		si.dwFlags = STARTF_USESHOWWINDOW;
    		si.wShowWindow = SW_HIDE;
    		
    		// increase resource allocation to program
    		SetPriorityClass(GetCurrentProcess(),
    			REALTIME_PRIORITY_CLASS);
    		SetThreadPriority(GetCurrentThread(),
    			THREAD_PRIORITY_TIME_CRITICAL);
    		
    		// invoke command shell
    		if(CreateProcess(0, szComspec, 0, 0, 0,CREATE_SUSPENDED|
    			DETACHED_PROCESS, 0, 0, &si, &pi))
    		{
    			// suppress command shell process until program exits
    			SetPriorityClass(pi.hProcess,IDLE_PRIORITY_CLASS);
    			SetThreadPriority(pi.hThread,THREAD_PRIORITY_IDLE); 
    			
    			// resume shell process with new low priority
    			ResumeThread(pi.hThread);
    			
    			// everything seemed to work
    			return TRUE;
    		}
    		else // if error, normalize allocation
    		{
    			SetPriorityClass(GetCurrentProcess(),
    				NORMAL_PRIORITY_CLASS);
    			SetThreadPriority(GetCurrentThread(),
    				THREAD_PRIORITY_NORMAL);
    		}
    	}
    	return FALSE;
    }
    

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

    /************************************************************************/     
    /* 函数说明:窗口销毁消息                                        
    /* 参    数:无                                     
    /* 返 回 值:无    
    /* By:Koma   2009.08.06 09:50                                  
    /************************************************************************/ 
    void CDelSelfDlg::OnDestroy() 
    {
    	CDialog::OnDestroy();
    	
    	// TODO: Add your message handler code here
    	DeleteSelf();
    }
    


    本博客所有博文,若无专门说明皆为原创,转载请注明作者和出处!
  • 相关阅读:
    左偏树
    论在Windows下远程连接Ubuntu
    ZOJ 3711 Give Me Your Hand
    SGU 495. Kids and Prizes
    POJ 2151 Check the difficulty of problems
    CodeForces 148D. Bag of mice
    HDU 3631 Shortest Path
    HDU 1869 六度分离
    HDU 2544 最短路
    HDU 3584 Cube
  • 原文地址:https://www.cnblogs.com/ifinver/p/2828694.html
Copyright © 2011-2022 走看看