{"id":311,"date":"2009-04-12T23:39:01","date_gmt":"2009-04-12T18:09:01","guid":{"rendered":"http:\/\/techtwaddle.net\/?p=311"},"modified":"2011-04-12T23:39:15","modified_gmt":"2011-04-12T18:09:15","slug":"applications-adding-image-to-your-application","status":"publish","type":"post","link":"https:\/\/techtwaddle.co.in\/blog\/2009\/04\/12\/applications-adding-image-to-your-application\/","title":{"rendered":"Applications: Adding Image to your application"},"content":{"rendered":"<div style=\"text-align: justify; font-family: Comic Sans MS;\">Well, back from work and I was sitting idle. I thought the first screen of my application looks a bit stale really, so, I decided to add a small picture to the main screen.<\/div>\n<p>&nbsp;<\/p>\n<div style=\"text-align: justify; font-family: Comic Sans MS;\"><span style=\"font-weight: bold;\">Adding an image resource to your project:<\/span><\/div>\n<div style=\"text-align: justify; font-family: Comic Sans MS;\">This is really easy.<\/div>\n<p>&nbsp;<\/p>\n<div style=\"text-align: justify; font-family: Comic Sans MS;\">1. Copy the bitmap file into you project directory.<\/div>\n<div style=\"text-align: justify; font-family: Comic Sans MS;\">2. Go to Resource View, right click and select &quot;<span style=\"font-family: Tahoma; color: rgb(0, 0, 128);\">Add Resource..<\/span>&quot;<\/div>\n<div style=\"text-align: justify; font-family: Comic Sans MS;\">3. Select &quot;<span style=\"font-family: Tahoma; color: rgb(0, 0, 128);\">Bitmap<\/span>&quot; and click on &quot;<span style=\"font-family: Tahoma; color: rgb(0, 0, 128);\">Import..<\/span>&quot;<\/div>\n<div style=\"text-align: justify; font-family: Comic Sans MS;\">4. Point it to the file you copied in step 1<\/div>\n<div style=\"text-align: justify; font-family: Comic Sans MS;\">5. Note the ID assigned to your bitmap. In my case it was <span style=\"font-family: Tahoma; color: rgb(0, 0, 128);\">IDB_BITMAP1<\/span><\/div>\n<p>&nbsp;<\/p>\n<div style=\"text-align: justify; font-family: Comic Sans MS;\">And thats it! The resource is now present within your project, you just need to write code to use it.<\/div>\n<p>&nbsp;<\/p>\n<div style=\"text-align: justify;\"><span style=\"font-family: Comic Sans MS;\">In my <span style=\"font-family: Tahoma;\"><span style=\"color: rgb(0, 0, 128);\">WndProc<\/span> <\/span>function, just before breaking under <span style=\"font-family: Tahoma; color: rgb(0, 0, 128);\">WM_CREATE<\/span>, I make a call to this function <span style=\"font-family: Tahoma; color: rgb(0, 0, 128);\">AlignComponents()<\/span>, which looks like this:<\/span><\/div>\n<p>\n<span style=\"font-family: Tahoma; color: rgb(0, 0, 128);\">void AlignComponents(HWND hWnd)<\/span><br style=\"font-family: Tahoma; color: rgb(0, 0, 128);\" \/><br \/>\n<span style=\"font-family: Tahoma; color: rgb(0, 0, 128);\">{<\/span><br style=\"font-family: Tahoma; color: rgb(0, 0, 128);\" \/><br \/>\n<span style=\"font-family: Tahoma; color: rgb(0, 0, 128);\">&nbsp;&nbsp;&nbsp; RECT rect;<\/span><br style=\"font-family: Tahoma; color: rgb(0, 0, 128);\" \/><br \/>\n<span style=\"font-family: Tahoma; color: rgb(0, 0, 128);\">&nbsp;&nbsp;&nbsp; int imageWidth = 32, imageHeight = 32;<\/span><br style=\"font-family: Tahoma; color: rgb(0, 0, 128);\" \/><br \/>\n<span style=\"font-family: Tahoma; color: rgb(0, 0, 128);\">&nbsp;&nbsp;&nbsp; int imageX = 0, imageY = 0;<\/span><br style=\"font-family: Tahoma; color: rgb(0, 0, 128);\" \/><br \/>\n<span style=\"font-family: Tahoma; color: rgb(0, 0, 128);\">&nbsp;&nbsp;&nbsp; int captionX = 0, captionY = 0;<\/span><br style=\"font-family: Tahoma; color: rgb(0, 0, 128);\" \/><br \/>\n<span style=\"font-family: Tahoma; color: rgb(0, 0, 128);\">&nbsp;&nbsp;&nbsp; const int offset = 25;<\/span><br style=\"font-family: Tahoma; color: rgb(0, 0, 128);\" \/><br \/>\n<span style=\"font-family: Tahoma; color: rgb(0, 0, 128);\">&nbsp;&nbsp;&nbsp; TCHAR captionText[] = TEXT(&quot;TechTwaddle\\n Copyright (c) 2009&quot;);<\/span><br style=\"font-family: Tahoma; color: rgb(0, 0, 128);\" \/><br \/>\n<br style=\"font-family: Tahoma; color: rgb(0, 0, 128);\" \/><br \/>\n<span style=\"font-family: Tahoma; color: rgb(0, 0, 128);\">&nbsp;&nbsp;&nbsp; GetClientRect(hWnd, &amp;rect);<\/span><br style=\"font-family: Tahoma; color: rgb(0, 0, 128);\" \/><br \/>\n<br style=\"font-family: Tahoma; color: rgb(0, 0, 128);\" \/><br \/>\n<span style=\"font-family: Tahoma; color: rgb(0, 0, 128);\">&nbsp;&nbsp;&nbsp; imageX = rect.right\/2 &#8211; imageWidth\/2;<\/span><br style=\"font-family: Tahoma; color: rgb(0, 0, 128);\" \/><br \/>\n<span style=\"font-family: Tahoma; color: rgb(0, 0, 128);\">&nbsp;&nbsp;&nbsp; imageY = rect.bottom\/2 &#8211; imageHeight*2;<\/span><br style=\"font-family: Tahoma; color: rgb(0, 0, 128);\" \/><br \/>\n<span style=\"font-family: Tahoma; color: rgb(0, 0, 128);\">&nbsp;&nbsp;&nbsp; <\/span><br style=\"font-family: Tahoma; color: rgb(0, 0, 128);\" \/><br \/>\n<span style=\"font-family: Tahoma; color: rgb(0, 0, 128);\">&nbsp;&nbsp;&nbsp; HWND hStatic = CreateWindowEx (0, L&quot;STATIC&quot;, NULL, WS_CHILD | WS_VISIBLE | SS_BITMAP, <\/span><br style=\"font-family: Tahoma; color: rgb(0, 0, 128);\" \/><br \/>\n<span style=\"font-family: Tahoma; color: rgb(0, 0, 128);\">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; imageX, imageY, imageWidth, imageHeight, hWnd, 0, g_hInst, NULL);<\/span><br style=\"font-family: Tahoma; color: rgb(0, 0, 128);\" \/><br \/>\n<br style=\"font-family: Tahoma; color: rgb(0, 0, 128);\" \/><br \/>\n<span style=\"font-family: Tahoma; color: rgb(0, 0, 128);\">&nbsp;&nbsp;&nbsp; if (hStatic)<\/span><br style=\"color: rgb(0, 0, 128);\" \/><br \/>\n<span style=\"font-family: Tahoma; color: rgb(0, 0, 128);\">&nbsp;&nbsp;&nbsp; {<\/span><br style=\"font-family: Tahoma; color: rgb(0, 0, 128);\" \/><br \/>\n<span style=\"font-family: Tahoma; color: rgb(0, 0, 128);\">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; HBITMAP hImage = LoadBitmap(g_hInst, MAKEINTRESOURCE(IDB_BITMAP1));<\/span><br style=\"font-family: Tahoma; color: rgb(0, 0, 128);\" \/><br \/>\n<span style=\"font-family: Tahoma; color: rgb(0, 0, 128);\">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; SendMessage(hStatic, STM_SETIMAGE, IMAGE_BITMAP, (LPARAM)hImage);<\/span><br style=\"font-family: Tahoma; color: rgb(0, 0, 128);\" \/><br \/>\n<span style=\"font-family: Tahoma; color: rgb(0, 0, 128);\">&nbsp;&nbsp;&nbsp; }<\/span><br style=\"font-family: Tahoma; color: rgb(0, 0, 128);\" \/><br \/>\n<br style=\"font-family: Tahoma; color: rgb(0, 0, 128);\" \/><br \/>\n<span style=\"font-family: Tahoma; color: rgb(0, 0, 128);\">&nbsp;&nbsp;&nbsp; captionX = rect.left + offset;<\/span><br style=\"font-family: Tahoma; color: rgb(0, 0, 128);\" \/><br \/>\n<span style=\"font-family: Tahoma; color: rgb(0, 0, 128);\">&nbsp;&nbsp;&nbsp; captionY = imageY + imageHeight + 3;<\/span><br style=\"font-family: Tahoma; color: rgb(0, 0, 128);\" \/><br \/>\n<br style=\"font-family: Tahoma; color: rgb(0, 0, 128);\" \/><br \/>\n<span style=\"font-family: Tahoma; color: rgb(0, 0, 128);\">&nbsp;&nbsp;&nbsp; HWND hStaticCaption = CreateWindowEx(0, L&quot;STATIC&quot;, NULL, WS_CHILD | WS_VISIBLE | SS_CENTER,<\/span><br style=\"font-family: Tahoma; color: rgb(0, 0, 128);\" \/><br \/>\n<span style=\"font-family: Tahoma; color: rgb(0, 0, 128);\">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; captionX, captionY, rect.right &#8211; offset*2, 30, hWnd, 0, g_hInst, NULL);<\/span><br style=\"font-family: Tahoma; color: rgb(0, 0, 128);\" \/><br \/>\n<br style=\"font-family: Tahoma; color: rgb(0, 0, 128);\" \/><br \/>\n<span style=\"font-family: Tahoma; color: rgb(0, 0, 128);\">&nbsp;&nbsp;&nbsp; if(hStaticCaption)<\/span><br style=\"font-family: Tahoma; color: rgb(0, 0, 128);\" \/><br \/>\n<span style=\"font-family: Tahoma; color: rgb(0, 0, 128);\">&nbsp;&nbsp;&nbsp; {<\/span><br style=\"font-family: Tahoma; color: rgb(0, 0, 128);\" \/><br \/>\n<span style=\"font-family: Tahoma; color: rgb(0, 0, 128);\">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; SendMessage(hStaticCaption, WM_SETTEXT, 0, (LPARAM)captionText);<\/span><br style=\"font-family: Tahoma; color: rgb(0, 0, 128);\" \/><br \/>\n<span style=\"font-family: Tahoma; color: rgb(0, 0, 128);\">&nbsp;&nbsp;&nbsp; }<\/span><br style=\"font-family: Tahoma; color: rgb(0, 0, 128);\" \/><br \/>\n<span style=\"font-family: Tahoma;\"><span style=\"font-family: Tahoma; color: rgb(0, 0, 128);\">}<\/span><\/p>\n<div style=\"text-align: justify; font-family: Comic Sans MS;\">The above code gets the client rect of the main window and calculates the X and Y position of the image based on the rect. I then use <span style=\"font-family: Tahoma;\"><span style=\"color: rgb(0, 0, 128);\">CreateWindowEx<\/span> <\/span>to create a static control which will show my bitmap. Use <span style=\"font-family: Tahoma;\"><span style=\"color: rgb(0, 0, 128);\">LoadBitmap<\/span> <\/span>to load the bitmap and get a handle to it and then send <span style=\"font-family: Tahoma; color: rgb(0, 0, 128);\">STM_SETIMAGE<\/span> message, passing the bitmap handle so that the image can be shown in the control. Note that the static control was created using <span style=\"font-family: Tahoma; color: rgb(0, 0, 128);\">SS_BITMAP<\/span> as one of its styles. Without this style the static control will not show the image.<\/div>\n<p><br style=\"font-family: Comic Sans MS;\" \/><\/p>\n<div style=\"text-align: justify; font-family: Comic Sans MS;\">Then I create another static control positionally relative to the image. And send <span style=\"font-family: Tahoma; color: rgb(0, 0, 128);\">WM_SETTEXT<\/span> message to it to set the text. Note that the <span style=\"font-family: Tahoma; color: rgb(0, 0, 128);\">SS_BITMAP<\/span> style is not mentioned in this static control. I had just copy pasted the code from above with <span style=\"font-family: Tahoma; color: rgb(0, 0, 128);\">SS_BITMAP<\/span> set and sat wondering for a few minutes why the text was not showing up.<\/div>\n<div style=\"text-align: justify; font-family: Comic Sans MS;\">&nbsp;<\/div>\n<p><span style=\"font-family: Comic Sans MS;\">Anyways, my main screen now looks like:<\/span><\/p>\n<p><img decoding=\"async\" loading=\"lazy\" width=\"348\" height=\"547\" alt=\"\" src=\"\/images\/geekswithblogs_net\/TechTwaddle\/HelloWorld\/simpson_logo.JPG\" \/><\/p>\n<p>\n<span style=\"font-family: Comic Sans MS;\">A lot better than before. I thought I&#8217;ll change the font of the text but was too lazy. Maybe tomorrow.<\/span><br \/>\n<\/span><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Well, back from work and I was sitting idle. I thought the first screen of my application looks a bit stale really, so, I decided to add a small picture to the main screen. &nbsp; Adding an image resource to your project: This is really easy. &nbsp; 1. Copy the bitmap file into you project &hellip; <a href=\"https:\/\/techtwaddle.co.in\/blog\/2009\/04\/12\/applications-adding-image-to-your-application\/\" class=\"more-link\">Continue reading <span class=\"screen-reader-text\">Applications: Adding Image to your application<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"spay_email":"","jetpack_publicize_message":"","jetpack_is_tweetstorm":false},"categories":[1],"tags":[],"jetpack_featured_media_url":"","jetpack_publicize_connections":[],"jetpack_sharing_enabled":true,"jetpack_shortlink":"https:\/\/wp.me\/p1ktFF-51","_links":{"self":[{"href":"https:\/\/techtwaddle.co.in\/blog\/wp-json\/wp\/v2\/posts\/311"}],"collection":[{"href":"https:\/\/techtwaddle.co.in\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/techtwaddle.co.in\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/techtwaddle.co.in\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/techtwaddle.co.in\/blog\/wp-json\/wp\/v2\/comments?post=311"}],"version-history":[{"count":1,"href":"https:\/\/techtwaddle.co.in\/blog\/wp-json\/wp\/v2\/posts\/311\/revisions"}],"predecessor-version":[{"id":312,"href":"https:\/\/techtwaddle.co.in\/blog\/wp-json\/wp\/v2\/posts\/311\/revisions\/312"}],"wp:attachment":[{"href":"https:\/\/techtwaddle.co.in\/blog\/wp-json\/wp\/v2\/media?parent=311"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/techtwaddle.co.in\/blog\/wp-json\/wp\/v2\/categories?post=311"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/techtwaddle.co.in\/blog\/wp-json\/wp\/v2\/tags?post=311"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}