{"id":11653,"date":"2011-01-27T07:00:00","date_gmt":"2011-01-27T07:00:00","guid":{"rendered":"https:\/\/blogs.msdn.microsoft.com\/oldnewthing\/2011\/01\/27\/how-do-you-obtain-the-icon-for-a-shortcut-without-the-shortcut-overlay\/"},"modified":"2011-01-27T07:00:00","modified_gmt":"2011-01-27T07:00:00","slug":"how-do-you-obtain-the-icon-for-a-shortcut-without-the-shortcut-overlay","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/oldnewthing\/20110127-00\/?p=11653","title":{"rendered":"How do you obtain the icon for a shortcut without the shortcut overlay?"},"content":{"rendered":"<p>\nThe easy one-stop-shopping\nway to get the icon for a file is to use the\n<code>SHGet&shy;File&shy;Info<\/code>\nfunction with the <code>SHGFI_ICON<\/code> flag.\nOne quirk of the <code>SHGet&shy;File&shy;Info<\/code> function is that\nif you pass the path to a shortcut file,\nit will always place the shortcut overlay\non the icon,\nregardless of whether you passed the\n<code>SHGFI_ADD&shy;OVERLAYS<\/code> flag.\n(Exercise: What is so special about the\nshortcut overlay that makes it\nexempt from the powers of the\n<code>SHGFI_ADD&shy;OVERLAYS<\/code> flag?\nThe information you need is on the MSDN page for\n<code>SHGet&shy;File&shy;Info<\/code>,\nthough you&#8217;ll have to apply some logic to the sitaution.)\n<\/p>\n<blockquote CLASS=\"q\"><p>\nI&#8217;m using SHGet&shy;File&shy;Info to get the icon of a file to\ndisplay in my application.\nWhen the file is a shortcut,\nrather than displaying the exe icon with a link overlay\n(as in SHGFI_LINK&shy;OVERLAY)\nI&#8217;d like to display the original exe icon.\nIs there a way to do this with SHGet&shy;File&shy;Info? Thanks,\n<\/p><\/blockquote>\n<p>\nFirst, correcting a minor error in the question:\nThe icon for a shortcut is, by default, the icon for the shortcut\ntarget,\nbut it doesn&#8217;t have to be.\nThe <code>IShell&shy;Link::Set&shy;Icon&shy;Location<\/code> method\nlets you set the icon for a shortcut to anything you like.\n(This is the method used when you click <i>Change Icon<\/i>\non the shortcut property page.)\n<\/p>\n<p>\nAnyway,\nthe <code>SHGet&shy;File&shy;Info<\/code> function gets the icon first\nby asking the shell namespace for the icon index in the system\nimagelist,\nand then converting that imagelist\/icon index into a <code>HICON<\/code>.\nIf you want to change the conversion, you can just ask\n<code>SHGet&shy;File&shy;Info<\/code> to stop halfway and then finish\nthe process the way you like.\n<\/p>\n<pre>\nHICON GetIconWithoutShortcutOverlay(PCTSTR pszFile)\n{\n SHFILEINFO sfi;\n HIMAGELIST himl = reinterpret_cast&lt;HIMAGELIST&gt;(\n  SHGetFileInfo(pszFile, 0, &amp;sfi, sizeof(sfi),\n                SHGFI_SYSICONINDEX));\n if (himl) {\n  return ImageList_GetIcon(himl, sfi.iIcon, ILD_NORMAL);\n } else {\n  return NULL;\n }\n}\n<\/pre>\n<p>\nOf course,\nif you&#8217;re going to be doing this for a lot of files,\nyou may want to just stop once you have the imagelist and the index,\nusing <code>Image&shy;List_Draw<\/code> to draw the image when necessary,\ninstead of creating thousands of little icons.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>The easy one-stop-shopping way to get the icon for a file is to use the SHGet&shy;File&shy;Info function with the SHGFI_ICON flag. One quirk of the SHGet&shy;File&shy;Info function is that if you pass the path to a shortcut file, it will always place the shortcut overlay on the icon, regardless of whether you passed the SHGFI_ADD&shy;OVERLAYS [&hellip;]<\/p>\n","protected":false},"author":1069,"featured_media":111744,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[1],"tags":[25],"class_list":["post-11653","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-oldnewthing","tag-code"],"acf":[],"blog_post_summary":"<p>The easy one-stop-shopping way to get the icon for a file is to use the SHGet&shy;File&shy;Info function with the SHGFI_ICON flag. One quirk of the SHGet&shy;File&shy;Info function is that if you pass the path to a shortcut file, it will always place the shortcut overlay on the icon, regardless of whether you passed the SHGFI_ADD&shy;OVERLAYS [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/posts\/11653","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/users\/1069"}],"replies":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/comments?post=11653"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/posts\/11653\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/media\/111744"}],"wp:attachment":[{"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/media?parent=11653"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/categories?post=11653"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/tags?post=11653"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}