{"id":6393,"date":"2012-10-05T07:00:00","date_gmt":"2012-10-05T07:00:00","guid":{"rendered":"https:\/\/blogs.msdn.microsoft.com\/oldnewthing\/2012\/10\/05\/how-do-i-override-the-default-icon-selection-algorithm\/"},"modified":"2012-10-05T07:00:00","modified_gmt":"2012-10-05T07:00:00","slug":"how-do-i-override-the-default-icon-selection-algorithm","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/oldnewthing\/20121005-00\/?p=6393","title":{"rendered":"How do I override the default icon selection algorithm?"},"content":{"rendered":"<p>\nThe usual way of loading an icon from a resource is to use the\n<code>Load&shy;Icon<\/code>\nor\n<code>Load&shy;Image<\/code> function\nand letting the window manager&#8217;s icon selection algorithm\ndecide which image to use\nbased on the images available in the icon,\nthe desired icon size, and the current color depth.\nA customer wanted to override that algorithm,\nbecause the window manager uses the current display\ncolor depth to select an image,\nbut they were obtaining the icon for printing purposes,\nso they wanted to get the highest-color-quality icon\nrather than the one that matched the screen&#8217;s color depth.\nHow do you override the default algorithm?\n<\/p>\n<p>\nYou basically do the same thing the window manager does.\nAs we saw earlier,\n<a HREF=\"http:\/\/blogs.msdn.com\/b\/oldnewthing\/archive\/2012\/07\/20\/10331787.aspx\">\nicon resources are actually stored in multiple pieces<\/a>.\nThe thing you use to talk about icons is actually the\n<i>icon directory<\/i>,\nwhich in turn points to a set of images.\nThe first step, then, is to obtain the icon directory.\n<\/p>\n<pre>\nHRSRC hrsrcIcon = FindResource(hResources,\n                     MAKEINTRESOURCE(IDI_MY_ICON), RT_GROUP_ICON);\nHGLOBAL hIcon = LoadResource(hResources, hrsrcIcon);\nauto lpIcon = static_cast&lt;GRPICONDIR *&gt;(LockResource(hIcon));\n<\/pre>\n<p>\nYou then take the images listed in the <code>GRPICONDIR<\/code> and\napply your custom algorithm to decide which one you like best.\n(If you want to use the default algorithm, you can call\n<code>Lookup&shy;Icon&shy;Id&shy;From&shy;Directory<\/code>\nor\n<code>Lookup&shy;Icon&shy;Id&shy;From&shy;Directory&shy;Ex<\/code>.\nBut if you want to use the default algorithm, then just use\n<code>Load&shy;Image<\/code> already!)\n<\/p>\n<p>\nWhen you&#8217;ve found the image you like, take the <code>nId<\/code>,\nand that&#8217;s the resource ID for the <code>RT_ICON<\/code>.\n<\/p>\n<pre>\nHRSRC hrsrcImage = FindResource(hResources,\n                     MAKEINTRESOURCE(nId), RT_ICON);\nHGLOBAL hImage = LoadResource(hResources, hrsrcImage);\nauto lpImage = static_cast&lt;PBYTE&gt;(LockResource(hImage));\n<\/pre>\n<p>\nYou can then convert the icon image data into an icon by\nusing the\n<code>Create&shy;Icon&shy;From&shy;Resource<\/code> or\n<code>Create&shy;Icon&shy;From&shy;Resource&shy;Ex<\/code> function.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>The usual way of loading an icon from a resource is to use the Load&shy;Icon or Load&shy;Image function and letting the window manager&#8217;s icon selection algorithm decide which image to use based on the images available in the icon, the desired icon size, and the current color depth. A customer wanted to override that algorithm, [&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-6393","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-oldnewthing","tag-code"],"acf":[],"blog_post_summary":"<p>The usual way of loading an icon from a resource is to use the Load&shy;Icon or Load&shy;Image function and letting the window manager&#8217;s icon selection algorithm decide which image to use based on the images available in the icon, the desired icon size, and the current color depth. A customer wanted to override that algorithm, [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/posts\/6393","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=6393"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/posts\/6393\/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=6393"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/categories?post=6393"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/tags?post=6393"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}