让程序在Windows启动时自动运行并自动隐藏

 
CString strAppName  =  _T( " AppName " );                 //  应用程序名
CString strAppPath  =  _T( " c://AppName.exe " );         //  应用程序存储路径
CString strRegPath  =  _T( " Software//Microsoft//Windows//CurrentVersion//Run " );
HKEY hKey;

RegOpenKeyEx(HKEY_LOCAL_MACHINE, strRegPath, 
0 , KEY_WRITE,  & hKey);
RegDeleteValue(hKey, strAppName);
RegSetValueEx(hKey, strAppName, 
0 , REG_SZ, (unsigned  char * )strAppPath.GetBuffer( 0 ), strAppPath.GetLength());

以上程序适用于Windows NT/2000/XP。

如果想要在启动时同时隐藏窗体,可以捕获WM_WINDOWPOSCHANGING消息:

void  CMainFrame::OnWindowPosChanging(WINDOWPOS FAR *  lpwndpos) 
{
    
if  (m_bAutoHide  ==  FALSE)
    {
        CDialog::OnWindowPosChanging(lpwndpos);    
//  显示
    }
    
else
    {
        
if  (lpwndpos -> flags  &  SWP_SHOWWINDOW)     //  隐藏
        {
            lpwndpos
-> flags  &=   ~ SWP_SHOWWINDOW;
            PostMessage(WM_WINDOWPOSCHANGING, 
0 , (LPARAM)lpwndpos);
            ShowWindow(SW_HIDE);

            m_bAutoHide 
=  FALSE;
        }
        
else
        {
            CDialog::OnWindowPosChanging(lpwndpos);    
//  显示
        }
    }
}

其中的m_bAutoHide是BOOL型成员变量,用于记录是否在启动时隐藏窗体。

你可能感兴趣的:(C/C++)