{"id":494,"date":"2012-02-29T19:54:04","date_gmt":"2012-03-01T02:54:04","guid":{"rendered":"http:\/\/mattfife.net\/wordpress\/?p=494"},"modified":"2012-08-12T19:13:41","modified_gmt":"2012-08-13T02:13:41","slug":"loading-an-icon-from-a-file-win32","status":"publish","type":"post","link":"https:\/\/mattfife.com\/?p=494","title":{"rendered":"Loading an icon from a file (Win32)"},"content":{"rendered":"<p>I&#8217;m always perturbed by how difficult it is to load an icon in Win32 at times.\u00a0 So many HICON\/HANDLE and whatnot special data members and folks on forums doing strange stuff.\u00a0 Here&#8217;s a snippet of code that just loads a simple icon file and displays it as your executable icon.\u00a0 I&#8217;m sure there is a more correct\/newer\/snazzier way to do this &#8211; but this works fine for me&#8230;<\/p>\n<p><code>LPCTSTR iconPathName= L\"..\/MyIcon.ico\";<br \/>\nUINT icon_flags = LR_LOADFROMFILE | LR_DEFAULTSIZE;<br \/>\nHANDLE hIcon = LoadImage(hInstance, iconPathName, IMAGE_ICON, 0, 0, icon_flags);<\/code><\/p>\n<p><code>\/\/ set up RegisterClass struct<br \/>\nwcex.style\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0= CS_OWNDC | CS_HREDRAW | CS_VREDRAW;<br \/>\nwcex.lpfnWndProc\u00a0\u00a0 \u00a0= WndProc;<br \/>\nwcex.cbClsExtra\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0= 0;<br \/>\nwcex.cbWndExtra\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0= 0;<br \/>\nwcex.hInstance\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0= hInstance;<br \/>\nwcex.hIcon\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0= (HICON) hIcon;<br \/>\nwcex.hCursor\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0= LoadCursor(NULL, IDC_ARROW);<br \/>\nwcex.hbrBackground\u00a0\u00a0 \u00a0= (HBRUSH)(COLOR_WINDOW+1);<br \/>\nwcex.lpszMenuName\u00a0\u00a0 \u00a0= NULL;<br \/>\nwcex.lpszClassName\u00a0\u00a0 \u00a0= m_AppTitle.c_str();<code><\/code><\/p>\n<p><code>\/\/ register the window class<br \/>\nreturn RegisterClassEx(&amp;wcex);<\/code><\/p>\n<p><\/code><\/p>\n","protected":false},"excerpt":{"rendered":"<p>I&#8217;m always perturbed by how difficult it is to load an icon in Win32 at times.\u00a0 So many HICON\/HANDLE and whatnot special data members and folks on forums doing strange stuff.\u00a0 Here&#8217;s a snippet of code that just loads a simple icon file and displays it as your executable icon.\u00a0 I&#8217;m sure there is a more correct\/newer\/snazzier way to do this &#8211; but this works fine for me&#8230; LPCTSTR iconPathName= L&#8221;..\/MyIcon.ico&#8221;; UINT icon_flags = LR_LOADFROMFILE | LR_DEFAULTSIZE; HANDLE hIcon =&#8230;<\/p>\n<p class=\"read-more\"><a class=\"btn btn-default\" href=\"https:\/\/mattfife.com\/?p=494\"> Read More<span class=\"screen-reader-text\">  Read More<\/span><\/a><\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_feature_clip_id":0,"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":false,"jetpack_social_options":{"image_generator_settings":{"template":"highway","default_image_id":0,"font":"","enabled":false},"version":2},"jetpack_post_was_ever_published":false},"categories":[5],"tags":[],"class_list":["post-494","post","type-post","status-publish","format-standard","hentry","category-technical"],"jetpack_publicize_connections":[],"jetpack_sharing_enabled":true,"jetpack_shortlink":"https:\/\/wp.me\/p4WECr-7Y","jetpack-related-posts":[],"jetpack_featured_media_url":"","_links":{"self":[{"href":"https:\/\/mattfife.com\/index.php?rest_route=\/wp\/v2\/posts\/494","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/mattfife.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/mattfife.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/mattfife.com\/index.php?rest_route=\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/mattfife.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=494"}],"version-history":[{"count":1,"href":"https:\/\/mattfife.com\/index.php?rest_route=\/wp\/v2\/posts\/494\/revisions"}],"predecessor-version":[{"id":558,"href":"https:\/\/mattfife.com\/index.php?rest_route=\/wp\/v2\/posts\/494\/revisions\/558"}],"wp:attachment":[{"href":"https:\/\/mattfife.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=494"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/mattfife.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=494"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/mattfife.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=494"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}