none
How To Set Icon To File

    Question

  • Hi All,



    I would like to set simple Icon to a file created by CFile class.

    Please suggest a way how to set icon to file created through CFile.


    Regards

    -Prakash Joshi

    Wednesday, May 09, 2007 8:53 AM

Answers

  • I presuming you are generating a custom document. If that's so, Explorer will use the default icon for that document type (that's registered). If not, you should register an icon for that specific document type.

    For an example, open HKEY_CLASSES_ROOT\chm.file\DefaultIcon using regedit and you'll see what you need to do.
    Wednesday, May 09, 2007 1:44 PM

All replies

  • I presuming you are generating a custom document. If that's so, Explorer will use the default icon for that document type (that's registered). If not, you should register an icon for that specific document type.

    For an example, open HKEY_CLASSES_ROOT\chm.file\DefaultIcon using regedit and you'll see what you need to do.
    Wednesday, May 09, 2007 1:44 PM
  • Hi Nishant,


    Thanks for Reply.

    I have set a Icon with the help help of your reply.

    i have created "DefaultIcon" Key under my file extension i.e. *.bpt and assigned       value i.e. path of .ico file.

    it is working fine, but now this is a hardcoded path and i have to supply .ico file      separately.

    it would be great if I can attach an ICON in dll resources and use the same.

    please suggest how this "DefaultIcon" key can point to ICON resource from dll.



    i have seen some other file's "DefaultIcon" which have value something like

    "C:\PROGRA~1\COMMON~1\MICROS~1\OFFICE11\MSOICONS.EXE,6"

    what does this 6 mean?



    Thanks!
    Prakash Joshi

    Tuesday, May 15, 2007 1:50 PM
  • Hi Prakash,
    I m creating a file in vc++ and i want to change  its icon. Please give me brief idea to set file icon.
    thanks
    Manish Agrahari

    Wednesday, April 01, 2009 7:05 AM
  • Hello Manish,

    Check this article - Replacing the Default MFC Icon. Its very eazy! ;)

    Regards,
    Jijo.
    _____________________________________________________

    http://weseetips.com [^ ] Visual C++ tips and tricks. Updated daily.
    Wednesday, April 01, 2009 1:44 PM
  • Hi Jijo,
    thanks for ur reply.
    But i dont want to change my Application's exe icon. In my application we are generating a file that is "*.itv". I want to change icon of these file. Just like our exe icon.

    Thanks!!!!!
    Manish Agrahari
    Friday, April 03, 2009 6:40 AM
  • Hi Manish,

    Nishant already explained how to do it. For your case you've to add a registry key -
    "HKEY_CLASSES_ROOT\itv.file\DefaultIcon" and specify your icon there.

    You can use CRegKey class for doing it programmatically.


    Regards,
    Jijo.
    _____________________________________________________

    http://weseetips.com [^ ] Visual C++ tips and tricks. Updated daily.
    Friday, April 03, 2009 11:01 AM
  • Hi Jijo,

    i have done this manually "HKEY_CLASSES_ROOT\itv.file\DefaultIcon" Then i did right click on "Default" (REG_SZ) and choose "modify" option and write C:\App1.exe (my application's exe location).

    but there is no any change occur in my .itv files loaction.

    please help me.

    Thanks!!!

    Manish Ag
    Tuesday, April 07, 2009 10:05 AM
  • Hi Manish,

    I apologize that, there is a correction in my answer. The correct values are as follows

    RegKey: HKEY_CLASSES_ROOT\.itv\DefaultIcon
    Default Value: C:\App1.exe,0

    Please take care that, as default value, you should provide "Exepath,IconIndex ". Now, set these values and restart pc[ or kill & restart explorer.exe ]. Now the file will reflect the icon change.

    And once again, Sincerely regretting for the inconvenience caused.

    Regards,
    Jijo.
    _____________________________________________________

    http://weseetips.com [^ ] Visual C++ tips and tricks. Updated daily.
    • Edited by Jijo Raj Thursday, April 09, 2009 7:49 AM updated path. Missed a dot infront of file extension.
    Tuesday, April 07, 2009 12:19 PM
  • Manish,

    Please have the code snippet. Just change the exename in test() function and execute. You can polish it by adding extra error checking. Hope this will solve your problem.

    // Associate file name.
    void AssosiateFileTypeIcon( CString FileType, CString ExePath )
    {
       // Create Key.
       HKEY Key = 0;
       RegCreateKey( HKEY_CLASSES_ROOT, FileType + "\\DefaultIcon", &Key );
    
       // Set default value.
       RegSetValue( Key, "", REG_SZ, ExePath, ExePath.GetLength());
    
       // Close key.
       RegCloseKey( Key );
    }
    
    
    // Test routine.
    void Test() 
    {
       // Associate file icon for .itv extension
       AssosiateFileTypeIcon( ".itv", // File name extension.
                              "C:\\MyFolder\\MyExe.exe,0" );  // Icon path.
    }
    

    Now restart explorer.exe to see changes in effect.

    NB: I think i made one more mistake in my previous post. I've updated it.
    I seriously need a cup of coffee. ZZzzzz...


    Regards,
    Jijo.
    _____________________________________________________

    http://weseetips.com [^ ] Visual C++ tips and tricks. Updated daily.
    Thursday, April 09, 2009 7:48 AM
  • Hi Jijo,

    Thanx a lot.. i really solved my problem...

    its working......

    Again thanx
    byeeeeeee
    Thursday, April 09, 2009 8:00 AM
  • Hello Jijo,

    I am trying to do the same thing as Manish.  I created a simple app with 4 icons and copied your code snippet.  When I ran the app, it always used the first icon, both for the file I created and app exe file when displaying in explorer, no matter what value I used in the icon path.  I then deleted the first icon from my project, rebulit it and ran it again.  Now it displays the new first icon for the executable file in explorer, but it still displays the file with the old first icon that I deleted frm my project.  Do you have any idea what might be going on?  How can it use an icon to display the file (I create a file with an .itv extension just like Manish did) that doesn't even exist in the app anymore? And why doesn't it use the icon number in the path - I set the icon path as "MyExe.exe,1" to try to get it to use the second icon. Thanks.

    Ragards,

    genush


    genush
    Monday, June 13, 2011 5:27 PM