MFC从资源加载文本

时间:2020-12-09 05:04:01
bool CWizardSheet::GetTextResource(UINT uID, CString& csContent)  
{  
    HMODULE hModule=GetModuleHandle(NULL);     
    HRSRC hRes = FindResource(hModule,MAKEINTRESOURCE(uID),_T("txt"));  
    if(hRes == NULL)  
    {  
        FreeResource(hRes);  
    }  
    else  
    {  
        HGLOBAL hglobal = LoadResource(hModule,hRes);  
        if(hglobal == NULL)  
        {  
            FreeResource(hglobal);  
            return false;  
        }  
        else  
        {  
            //get text  
            csContent.Format(_T("%s"),(LPVOID)hglobal);  
        }  
    }  
    return true;  
}  

  

bool CWizardSheet::GetResource(UINT uID, CString csType, CString csOutputPath)  
{  
    CFile file;  
    HMODULE hModule=GetModuleHandle(NULL);     
    HRSRC hRes = FindResource(hModule,MAKEINTRESOURCE(uID),csType);  
    if(hRes == NULL)  
    {  
        FreeResource(hRes);  
    }  
    else  
    {  
        HGLOBAL hglobal = LoadResource(hModule,hRes);  
        if(hglobal == NULL)  
        {  
            FreeResource(hglobal);  
            return false;  
        }  
        else  
        {  
            //释放文件  
            LPBYTE lpByte=(LPBYTE)LockResource(hglobal);     
            DWORD dwRcSize=SizeofResource(hModule,hRes);  
            file.Open(csOutputPath,CFile::modeCreate | CFile::modeWrite);  
            file.Write(lpByte,dwRcSize);  
            file.Close();  
        }  
    }  
    return true;  
}