19,468
社区成员
发帖
与我相关
我的任务
分享
void SaveIcon()
{
LPBYTE lpBits=NULL;
long size=0;
HICON hicon=LoadIcon(AfxGetInstanceHandle(),MAKEINTRESOURCE(IDR_MAINFRAME));
PICTDESC pdiconsrc;
pdiconsrc.cbSizeofstruct=sizeof(PICTDESC);
pdiconsrc.icon.hicon=hicon;
pdiconsrc.picType=PICTYPE_ICON;
IPicture* pIPicture=NULL;
HRESULT hr;
IDispatch* pDisp=NULL;
hr = OleCreatePictureIndirect(&pdiconsrc,
IID_IDispatch,
TRUE,
(void**)&pDisp);
if(SUCCEEDED(hr))
{
hr = pDisp->QueryInterface(&pIPicture);
if(SUCCEEDED(hr))
{
lpBits = (LPBYTE) GlobalAlloc(GMEM_MOVEABLE|GMEM_NODISCARD, 64*1024);
IStream* pStream;
CreateStreamOnHGlobal(lpBits,false,&pStream);
//下面的第二个参数为false时hr为E_FAIL,是True时倒是S_OK,但是size一直是766,结果也不对。
hr=pIPicture->SaveAsFile(pStream,true,&size);
if(pStream)
pStream->Release();
if(SUCCEEDED(hr)&&size>0)
{
//you can use
char* pchar=(char*)lpBits;
//icon is stored in pchar, length is size
//to create another hicon
//use OleLoadPicture(pStream,...)
//IPicture::get_Handle to get the icon handle
}
pIPicture->Release();
if(lpBits)
GlobalFree((HGLOBAL)lpBits);
}
pDisp->Release();
}
}
SHFILEINFO sfi;
SHGetFileInfo(docfileName,0,&sfi,sizeof(sfi),SHGFI_ICON|SHGFI_LARGEICON|SHGFI_USEFILEATTRIBUTES);
PICTDESC pdiconsrc;
pdiconsrc.cbSizeofstruct=sizeof(PICTDESC);
pdiconsrc.icon.hicon=sfi.hIcon;
pdiconsrc.picType=PICTYPE_ICON;
IPicture* pIPicture=NULL;
HRESULT hr;
LPBYTE lpBits=NULL;
long size=0;
hr = OleCreatePictureIndirect(&pdiconsrc, IID_IPicture, TRUE, (VOID**)&pIPicture);
if(SUCCEEDED(hr))
{
IStream* pStream;
lpBits = new BYTE[100*1024];
hr = CreateStreamOnHGlobal(lpBits,true,&pStream);
if(SUCCEEDED(hr))
{
hr=pIPicture->SaveAsFile(pStream,TRUE,&size);
if(pStream)
{
writeToFile(lpBits, size, L"D:\\a.ico");
pStream->Release();
}
pIPicture->Release();
if(lpBits)
{
delete[] lpBits;
}
}
}
char pathbuf[1024];
strcpy(pathbuf,sIconFileName);
CFile iconfile;
iconfile.Open(pathbuf, CFile::modeCreate|CFile::modeWrite);
LARGE_INTEGER li;
li.HighPart =0;
li.LowPart =0;
ULARGE_INTEGER ulnewpos;
pStream->Seek( li,STREAM_SEEK_SET,&ulnewpos);
ULONG uReadCount = 1;
while(uReadCount>0)
{
pStream->Read(pathbuf,sizeof(pathbuf),&uReadCount);
if(uReadCount>0)
iconfile.Write(pathbuf,uReadCount);
}
pStream->Release();
iconfile.Close();