CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 11 of 11
  1. #1
    Join Date
    Jan 2005
    Posts
    8

    MFC change file icon

    I am using MFC 6.0 to program and want to replace the default document file icon ( such as .doc). I tried to replace the default "res\\ProjectDoc.ico" with the new "res\\NewIcon.ico" on the project .rc file , but it does not work . Is there any solution I can try now?

  2. #2
    Join Date
    May 2000
    Location
    KY, USA
    Posts
    18,652

    Re: MFC change file icon

    [ Moved thread ]

  3. #3
    Join Date
    May 1999
    Location
    ALABAMA, USA
    Posts
    9,917

    Re: MFC change file icon

    Quote Originally Posted by zan yan
    but it does not work
    What exactly does not work?
    There are only 10 types of people in the world:
    Those who understand binary and those who do not.

  4. #4
    Join Date
    Jan 2005
    Posts
    8

    Re: MFC change file icon

    I make a MFC application, and its contents is stored to file , such as filename1.abc. when I look at the filename1.abc in the windows explorer, the .abc file displays with the icon of default mfc icon, such as the "res\\ProjectDoc.ico". I want to change the old icon with the new icon, i.e "res\\NewIcon.ico", makes the file displayed with the icon of "res\\NewIcon.ico" in the windows explorer. so I edits the project .rc file, replaces the old statement
    { IDR_DEVICETYPE ICON DISCARDABLE "res\\ProjectDoc.ico" }
    with the new statement
    { IDR_MAINFRAME ICON DISCARDABLE "res\\NewIcon.ico" }
    , the program compile , execute, save the contents to filename2.abc.when I looks at filename1.abc, filename2.abc in the windows explorer, these files (.abc file) still displays with the icon of "res\\ProjectDoc.ico", not I wanted icon of "res\\NewIcon.ico". how do I do that ?

  5. #5
    Join Date
    May 1999
    Location
    ALABAMA, USA
    Posts
    9,917

    Re: MFC change file icon

    Icons have two different device images: standard and small.
    Explorer is using small that you probably did not change. In resource editor choose appropriate device image and edit small too.
    If you want to use different set of icons from different file make sure both device images are present and after importing icon to resources you would have to assign ID that has the lowest value for this icon in resource header.
    Change original icon ID and assign IDR_MAINFRAME to a new icon.
    There are only 10 types of people in the world:
    Those who understand binary and those who do not.

  6. #6
    Join Date
    Jan 2005
    Posts
    8

    Re: MFC change file icon

    take the above methods, run the application, the window icon has been changed. look at the project\\debug directory , the .exe file icon has been changed .
    further more, I also want to change the document file icon displayed in the Explorer. below I attach a simple MFC project (aaa) developed in the standard document/view way , list the programming steps:

    first , in the CDocument-Derived class, declare member variable (CString m_displaystr), initialize m_displaystr in the CDocument-Derived class constructor.

    secong, in the CView-Derived class OnDraw(CDC* pDC) function , display the CDocument member m_displaystr, and call CDocument::SetModifiedFlag().

    third, copy two icon file to the project\\res\\ directory(computer.ico and doc.ico). in the .rc file , replace the old statments
    {
    IDR_MAINFRAME ICON DISCARDABLE "res\\aaa.ico"
    IDR_AAATYPE ICON DISCARDABLE "res\\aaaDoc.ico"
    }
    withe the following statements
    {
    IDR_MAINFRAME ICON DISCARDABLE "res\\computer.ico"
    IDR_AAATYPE ICON DISCARDABLE "res\\doc.ico"
    }.
    that's all.

    then , compile and execute, the text stored in the CDocument-Derived class member variable m_displaystr shows in the client area, the mainframewnd icon has been changed to the computer.ico, close the application , the Prompting saving dialog be poped up , select Yes, save the results to file (such as test.aaa).

    looking at the aaa.exe file in the Explorer, aaa.exe file has the icon of computer.ico. looking at the test.aaa file in the Explorer, test.aaa doesn't has the icon of doc.ico.

    what methods can I take to let the file test.aaa has the icon of doc.ico in the Explorer?

  7. #7
    Join Date
    May 1999
    Location
    ALABAMA, USA
    Posts
    9,917

    Re: MFC change file icon

    I don’t know why are you doing it a hard way. changing icon can be done in resource editor without directly editing rc file. There is no need for following steps you have posted.

    Anyway now I see your problem. Windows after document is registered by calling RegisterShellFileTypes caches icon and display it from cache.
    Depending on Windows version this is handled differently. Delete cache file or cache for your app and everything will show correctly.
    There are only 10 types of people in the world:
    Those who understand binary and those who do not.

  8. #8
    Join Date
    Jan 2005
    Posts
    8

    Re: MFC change file icon

    I am using windows 2000, don't know how to delete cache file. what action I should take to get the desired results?

  9. #9
    Join Date
    May 1999
    Location
    ALABAMA, USA
    Posts
    9,917

    Re: MFC change file icon

    I do not know, I don’t run Windows 2000.
    I think any search engine (Google) would find it for you if you specify right keywords. For example: icon cache windows 2000. Try it.
    There are only 10 types of people in the world:
    Those who understand binary and those who do not.

  10. #10
    Join Date
    Jan 2005
    Posts
    8

    Re: MFC change file icon

    following your advice , I begin search with the google. A lot of articles, especial a VC project to change the icon . below is the core code to delete cache file.

    void CShellIconChangerDlg::RefreshIcons()
    {
    CString val;
    HKEY hKey;

    LONG result = ::RegOpenKeyEx(HKEY_CURRENT_USER,
    "Control Panel\\Desktop\\WindowMetrics",
    0,KEY_READ,&hKey);
    BYTE buff[256];
    ZeroMemory(buff,255);
    DWORD sz = sizeof buff;
    DWORD typ = REG_SZ;
    RegQueryValueEx(hKey,"Shell Icon Size",0,&typ,buff,&sz);
    RegCloseKey(hKey);

    val = buff;

    int i = atoi(val);
    i++;
    val.Format("%d",i);

    result = ::RegOpenKeyEx(HKEY_CURRENT_USER,
    "Control Panel\\Desktop\\WindowMetrics",
    0,KEY_WRITE,&hKey);
    RegSetValueEx(hKey,"Shell Icon Size",0,REG_SZ,
    (const BYTE*)val.GetBuffer(0),val.GetLength());
    val.ReleaseBuffer();
    RegCloseKey(hKey);

    ::SendMessage(HWND_BROADCAST ,
    WM_SETTINGCHANGE,SPI_SETNONCLIENTMETRICS,NULL);


    i = atoi(val);
    i--;
    val.Format("%d",i);

    result = ::RegOpenKeyEx(HKEY_CURRENT_USER,
    "Control Panel\\Desktop\\WindowMetrics",
    0,KEY_WRITE,&hKey);
    RegSetValueEx(hKey,"Shell Icon Size",0,REG_SZ,
    (const BYTE*)val.GetBuffer(0),val.GetLength());
    val.ReleaseBuffer();
    RegCloseKey(hKey);

    ::SendMessage(HWND_BROADCAST ,
    WM_SETTINGCHANGE,SPI_SETNONCLIENTMETRICS,NULL);
    }

    putting the above code under the RegisterShellFileTypes(), executing the program, test.aaa file icon has been changed in the explorer. but it is the icon of "computer.ico", not "doc.ico". looking at the registry, under the key of Aaa.Document , there isn't the DefaultIcon subkey. Which function in MFC set the DefaultIcon subkey in the registry ?

    thanks.

  11. #11
    Join Date
    Sep 2003
    Location
    M...N
    Posts
    220

    Re: MFC change file icon

    I have spent two days on find the solutions and why.
    And code project has a discussion of "Replacing the Default MFC Icon" at:
    http://www.codeproject.com/gdi/replaceicon.asp


    My case is:

    It works on Application, but while browing it by Windows Explorer, it doesn't show correct one.
    Attached Images Attached Images  

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured