One of the flags you can pass to the SHGetFileInfo function is SHGFI_USEFILEATTRIBUTES. What does this flag mean? It means, “Do not access the disk. Pretend that the file/directory exists, and that its file attributes are what I passed as the dwFileAttributes parameter. Do this regardless of whether it actually exists or not.” You can use this flag to get the icon for a file type. For example, to get the icon for text files, pass a filename of “x.txt”, pass SHGFI_USEFILEATTRIBUTES in the uFlags, and specify FILE_ATTRIBUTE_NORMAL as the file attributes. Why is the filename “x.txt”? Because you want it to have the extension “.txt”, and you want it to be a valid filename. (Don’t pass illegal filenames like “???.txt”.) Why are the file attributes FILE_ATTRIBUTE_NORMAL? Because you want SHGetFileInfo to pretend that it is a normal file. (If you passed, say, FILE_ATTRIBUTE_DIRECTORY, then you would have gotten the folder icon back, since you told the function to pretend that “x.txt” was a directory.) Note that since you are telling SHGetFileInfo to “pretend”, there are some things you lose by the nature of make-believe.
For example, if the extension is a filetype whose icon changes depending on the contents of the file, then the dynamic icon will not be returned since there is no file. You told the function to “pretend”, after all, and a pretend file has no contents.
0 comments