September 3rd, 2015

When I change the icon in my shortcut, why doesn’t it update on the screen?

A customer was having trouble updating the icon in one of their shortcuts. Here’s what they shared with us:

    i_shell_link->SetIconLocation(icon_file.value().c_str(), 0);

“Changing the icon from the shortcut property sheet works, but it’s not working from our code. Is the shortcut property sheet using a different API from IShell­Link::Set­Icon­Location? In desperation, we added

    SHChangeNotify(SHCNE_ASSOCCHANGED, SHCNF_IDLIST, NULL, NULL);

but that didn’t help. Did we get the flags to SHChange­Notification wrong?”

The property sheet does use the IShell­Link::Set­Icon­Location method to change the shortcut icon. What the customer forgot was to save their changes!

    i_persist_file->Save(NULL, TRUE);

where i_persist_file is the IPersist­File that they used to load the shortcut, or they can use Query­Interface to get a new pointer.

The SHCNE_ASSOC­CHANGED notification is unnecessary, and in fact it’s overkill. That’s like saying, “I want to change the color of my sofa, so I’m going to demolish my house, rebuild it, and then refurnish it with a new sofa.”

If you want to send a notification to say, “Hey, I updated this file, please go refresh any data you have cached about it,” you can do a

    SHChangeNotify(SHCNE_UPDATEITEM, SHCNF_PATH, fullpath, NULL);
Topics
Code

Author

Raymond has been involved in the evolution of Windows for more than 30 years. In 2003, he began a Web site known as The Old New Thing which has grown in popularity far beyond his wildest imagination, a development which still gives him the heebie-jeebies. The Web site spawned a book, coincidentally also titled The Old New Thing (Addison Wesley 2007). He occasionally appears on the Windows Dev Docs Twitter account to tell stories which convey no useful information.

0 comments

Discussion are closed.