Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to show other apps icon in my app

Status
Not open for further replies.

BlueTube

Technical User
Aug 4, 2006
24
GB
Hello everyone,
In my application, users can select other applications to be added as short cut in my application. When users click on Brows, I open the open file dialogue box and users are then allowed to select the applications they want to add (i.e. Winword.exe). I want to be able to bring the icon of this application (or whatever application they choose) to my application in a picture or something so that they can see the icon of each application they selected for shortcut (similar to when you create a shortcut on your desktop and then the application icon shows up).
 
Take a look at the image & image list controls....

Outside of a dog, a book is man's best friend. Inside of a dog it's too dark to read.
 
I think the OPs problem is less the display (which the image and image list idea would help to address) and more how to get hold of the correct icon in the first place.

 
You can use ExtractIcon function to obtain the icon handle of the EXE file. You can use DrawIcon function to draw that icon on a picturebox or form, or use OleCreatePictureIndirect function to convert the icon handle to StdPicture object and assign it to the picture property of a picture box or image control.

Searching the forums will reveal examples.
 
As they are extracting icons from applications (EXE files) the result will be pretty much the same.

By the way, do you have any idea how to get this function fill in lpIconPath and lpiIcon members with the actual icon source on return?

The documentation says:
MSDN said:
If the function succeeds, the return value is an icon handle. If the icon is extracted from an associated executable file, the function stores the full path and file name of the executable file in the string pointed to by lpIconPath, and stores the icon's identifier in the WORD pointed to by lpiIcon.
However, I never got this function to work this way. Only the extracted icon handle is returned.
 
>do you have any idea how to get this function fill in lpIconPath and lpiIcon

Sadly only if the source file doesn't exist. Even more strange, I've never really found any reports on the internet that the function is broken or incorrectly documented. I normally use FindExecutable to get around that

>the result will be pretty much the same

Which is why I'd only be tempted, rather than definitely using it. The reason I'd be tempted is that if in the future the code needed to work with documents we'd still be able to get the correct icons without having to make any changes.
 
>Sadly only if the source file doesn't exist.

Which means you cannot find the correct icon for file classes where each file gets a unique icon depending upon the data inside the file. For example, the icon of html files depends upon the software in which the page was created. Same applies to .URL and .HT (HyperTerminal) files.

>I normally use FindExecutable to get around that

Find executable is close, but does not always work. First, it does not retrieve the icon index. Second, the "executable" file does not always contain the icon for the file. For example, FindExecutable retrieves Notepad.exe for .TXT, .INI and .INF files, but icons are actually located in shell32.dll.

I also have tried a lot to get this info correctly, but to no avail. MSDN claims that SHGetFileInfo function can also retrieve this information using SHGFI_ICONLOCATION flag, but it simply does not work.

I remember, I also tried IExtractIcon::GetIconLocation without success.

Getting icon handle is easy, but till now, I cannot find a way to retrieve the actual icon resource.
 
Assuming that FindAssociatedIcon actually worked as documented I would expect it to return the same executable as FindExecutable; both functions predate all the special shell handling stuff

As you say, ShGetFileInfo is supposed to work with that functionality, but doesn't work reliably.

So look what you've made me write:
Code:
[blue]Option Explicit

Private Declare Function ExtractIcon Lib "shell32.dll" Alias "ExtractIconA" (ByVal hInst As Long, ByVal lpszExeFileName As String, ByVal nIconIndex As Long) As Long
Private Declare Function DrawIcon Lib "user32" (ByVal hdc As Long, ByVal x As Long, ByVal y As Long, ByVal hIcon As Long) As Long
Private Declare Function DestroyIcon Lib "user32" (ByVal hIcon As Long) As Long

' requires reference to Windows Script Host Object Model
Public Sub vbDemoIcon(strFile As String)
    Dim Info() As String
    Dim hIcon As Long
    Dim extension As String
    
    With New FileSystemObject
        extension = .GetExtensionName(strFile)
    End With

    With New WshShell
        Info = Split(.RegRead("HKCR\" & .RegRead("HKCR\." & extension & "\") & "\defaulticon\"), ",")
    End With
    hIcon = ExtractIcon(App.hInstance, Info(0), CLng(Info(1)))
    DrawIcon Me.hdc, 10, 10, hIcon
    DestroyIcon hIcon
    Text1.Text = Join(Info, ", ")
End Sub
[/blue]
 
>Assuming that FindAssociatedIcon actually worked as documented I would expect it to return the same executable as FindExecutable

Can you elaborate further? As I described above, for a text file, I expect FindExecutable to return (just) notepad.exe and ExtractAssociatedIcon (I assume this is what you meant) to return shell32.dll, plus the icon index.

Yes, registry method works, but again, it only returns the 'DefaultIcon', similarly as ExtractAssociatedIcon works with nonexistent files. Files with instance-specific icons don't work with this function. For EXE and HT file classes, the default value of DefaultIcon key is set to %1.

I remember yet another unsuccessful attempt to get this information. I made Windows create a shortcut to file of interest using SHAddToRecentDocs function temperorily. I examined the contents of .LNK file using WshShortcut class, and also in Notepad presuming it would contain the icon location. But there was no such info inside. The icon location is not stored unless you explicitly change the icon from the property pages of the shortcut or specify it programatically using WshShortcut class.
 
I wouldn't. I'd expect ExtractAssociatedIcon - if it worked - to find notepad.exe just like FindExecutable (mainly because I'm pretty certain that ExtractAssociatedIcon actually calls FindExecutable). They don't really know anything too much about the shell. I wouldn't expect them to properly interrogate custom shell extensions (which is what handle things such as htm files and shortcuts in Recent Documents) for info.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top