MFC从资源加载文本

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;  
}  

  

posted @ 2017-05-15 00:22  一样菜  阅读(971)  评论(0编辑  收藏  举报