• 0

[C#] Getting a file's large icon


Question

Hello,

I'm trying to get file icons using SHGetFileInfo (See attched image), but when I try to get the large icon of the file using SHGFI_LARGEICON, it returnes the 32x32 icon :( while I want to get the 48x48 icon.

I've figured out that if I set Windows to show large icons, I can use SHGFI_SHELLICONSIZE, but it doesn't work when Windows is set to show small icons :blink:

How can I always get the large 48x48 icon of a file? What do I need to use?

Thanks in advance for your help :)

post-62656-1115302870_thumb.jpg

Link to comment
https://www.neowin.net/forum/topic/316840-c-getting-a-files-large-icon/
Share on other sites

14 answers to this question

Recommended Posts

  • 0

Thanks for your answer :) However, I have no idea how to use SHDefExtractIcon with C#. Maybe you can tell me how?

I just want to get the large icon of any real file on the computer. I just want it to work like the Windows toolbar - it shows the icons of the files in a certain directory.

Any idea how I can use SHDefExtractIcon ?

  • 0

Setting the icons sizes just for this seems quite weird to me :blink: I don't think it's the way to do it but thanks for the creative idea :)

I have the same problem with IExtractIcon::Extract - I have no idea how to use it :( Can anybody show me an example? How do I use it in an application?

BTW, thanks for your suggestions :)

  • 0

Here's how I do it:

HRESULT CPidl::GetParentShellFolder(IShellFolder** folder,LPCITEMIDLIST*

relativePidl, IShellFolder2** folder2)const
{
	ATLASSERT(m_Pidl != NULL);
	ATLASSERT(folder != NULL);

	HRESULT _hRes;

	if(folder == NULL)
	{
 ?_hRes = E_INVALIDARG;
	}
	else if(NULL == m_Pidl)
	{
 ?_hRes = E_FAIL;
	}
	else
	{
 ?*folder = NULL;

 ?LPCITEMIDLIST pidlChild = {0};

 ?HRESULT _hRes = ?SHBindToParent(m_Pidl,
 ? ? ? ? ? ? ? ? ? ? ? ? ? ?IID_IShellFolder, 
 ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?(void**) folder, 
 ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?const_cast<LPCITEMIDLIST*>(&pidlChild));

 ?if(relativePidl != NULL)
 ?{
 ?	*relativePidl = pidlChild;
 ?}

 ?if(folder2 != NULL)
 ?{
 ?	*folder2 = NULL;

 ?	if(SUCCEEDED(_hRes) && *folder != NULL)
 ? ?(*folder)->QueryInterface(IID_IShellFolder2,reinterpret_cast<void**>(folder2));
 ?}
	}

	return _hRes;
}


HICON CPidl::ExtractIcon(UINT size, UINT flags) const
{
	ATLASSERT(m_Pidl != NULL);

	if(NULL == m_Pidl)
 ?return NULL;

	CComPtr<IShellFolder> parentFolder;
	LPCITEMIDLIST relativePidl = NULL;

	HRESULT _hRes = GetParentShellFolder(&parentFolder,&relativePidl);

	if( FAILED(_hRes) )
 ?return NULL;

	CComPtr<IExtractIcon> extractIcon;
	_hRes = parentFolder->GetUIObjectOf(NULL,1,
const_cast<LPCITEMIDLIST*>(&relativePidl),
IID_IExtractIcon,NULL,reinterpret_cast<void**>(&extractIcon) );

	if( FAILED(_hRes) || NULL == extractIcon.p)
 ?return NULL;

	TCHAR location[MAX_PATH+2] = {0};
 // +2 fixes bogus shellextension off-by-one errors.
	int ? index(0);
	UINT outFlags(GIL_FORSHELL);

	_hRes = extractIcon->GetIconLocation(GIL_FORSHELL,
location,MAX_PATH,
&index,&outFlags);

	if( FAILED(_hRes) )
 ?return NULL;

	HICON smallIcon = {0};
	HICON largeIcon = {0};

	size = MAKELONG (size,size);

	_hRes = extractIcon->Extract(location,index,&largeIcon,&smallIcon,size);

	if( FAILED(_hRes) )
 ?return NULL;

	if(largeIcon != NULL)
	{
 ?DestroyIcon(smallIcon);
 ?return largeIcon;
	}
	else
	{
 ?return smallIcon;
	}

}

  • 0

1) Get a PIDL to the file you want the icon of.

2) Get the IShellFolder parent of it and the relative PIDL.

3) Query that IShellFolder for an IExtractIcon (GetUIObjectOf) for the relative PIDL you have obtained in step2.

4) get the location of the object with IExtractIcon::GetIconLocation

5) Use that location to extract the icon with IExtractIcon::Extract(...) and give up the size as a long with the high word and low word of the long set as the size ( MAKELONG(size, size) ).

This topic is now closed to further replies.
  • Recently Browsing   0 members

    • No registered users viewing this page.