11.04.2014 Views

Advanced MFC Programming

Advanced MFC Programming

Advanced MFC Programming

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

Chapter 14. Views<br />

used to distinguish between different type of files. However, some file types do not have registered icons<br />

and some files contain icons within themselves (such as files with “.exe” or “.dll” extension).<br />

Which Icon to Use<br />

Windows always try to display a file using the appropriate icons. If a file contains icon itself, this<br />

icon will be used. If a file doesn’t contain any icon but has registered icons (such as some special document<br />

files like “*.bmp”, “*.doc”), the registered icons will be used. If no registered icons are found, a default<br />

icon will be assigned to the file.<br />

There is a shell function that can be used to retrieve the icon information for a file:<br />

WINSHELLAPI DWORD WINAPI SHGetFileInfo<br />

(<br />

LPCTSTR pszPath, DWORD dwFileAttributes, SHFILEINFO FAR *psfi,<br />

UINT cbFileInfo, UINT uFlags<br />

);<br />

Here parameter pszPath is a pointer to a string specifying the file path; dwFileAttributes specifies<br />

the file attributes, and the file information can be retrieved into a SHFILEINFO type object which is pointed<br />

by pointer psfi; cbFileInfo specifies the size of SHFILEINFO structure; uFlags specifies what information<br />

is being retrieved. In our case, we can combine SHGFI_ICON with one of the following flags and pass the<br />

result to parameter uFlags:<br />

Flag<br />

SHGFI_LARGEICON<br />

SHGFI_SMALLICON<br />

SHGFI_SHELLICONSIZE<br />

SHGFI_SHELLICONSIZE | SHGFI_SMALLICON<br />

Meaning<br />

Retrieve large icon contained in the file<br />

Retrieve small icon contained in the file<br />

Retrieve shell large icon<br />

Retrieve shell small icon<br />

To display each file with embedded or registered icons, before adding an item to the list control, we<br />

need to first customize the image list. If any icon is found by calling function ::SHGetFileInfo(), we will<br />

add it to the image list. If we could not find an icon using this method, the default icon will be associated<br />

with the corresponding file.<br />

Sample<br />

Sample 14.7\Explorer is based on sample 14.6\Explorer. In this sample, the embedded and registered<br />

icons are retrieved for displaying files in the list view.<br />

In the sample, a new member function is added for retrieving icons for a file:<br />

HICON CExplorerView::GetIconFromFile(CString szFileName, UINT uFlags);<br />

The returned value is an icon handle. Within this function, ::SHGetFileInfo(…) is called to get the<br />

icon information of a file. The following is the implementation of this function:<br />

HICON CExplorerView::GetIconFromFile(CString szFileName, UINT uFlags)<br />

{<br />

SHFILEINFO shfi;<br />

::SHGetFileInfo(szFileName, 0, &shfi, sizeof(SHFILEINFO), SHGFI_ICON | uFlags);<br />

}<br />

return shfi.hIcon;<br />

Function CExplorerView::ChangeDir() is modified as follows: after a file is found, function<br />

CExplorerView::GetIconFromFile(…) is called to find its embedded or registered icons; if this is<br />

successful, the newly obtained icons will be added to the image list and associated with the file; otherwise<br />

the default images will be used. The following portion of function CExplorerView::ChangeDir() shows<br />

how we try to find the embedded icons of a file:<br />

446

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!