{"id":244,"date":"2009-05-27T14:19:41","date_gmt":"2009-05-27T13:19:41","guid":{"rendered":"http:\/\/www.nuonsoft.com\/blog\/?p=244"},"modified":"2009-05-27T14:19:41","modified_gmt":"2009-05-27T13:19:41","slug":"how-to-use-updatelayeredwindow","status":"publish","type":"post","link":"https:\/\/www.nuonsoft.com\/blog\/2009\/05\/27\/how-to-use-updatelayeredwindow\/","title":{"rendered":"How To Use UpdateLayeredWindow"},"content":{"rendered":"<p>In this post I will briefly explain how to use layered windows and specifically how to use <a href=\"http:\/\/msdn.microsoft.com\/en-us\/library\/ms633556(VS.85).aspx\" target=\"_blank\">UpdateLayeredWindow<\/a>.<\/p>\n<p>The first thing you need to do is add the WS_EX_LAYERED style to your window. This can for example be done with a call to CreateWindowEx:<\/p>\n<pre>hWnd = CreateWindowEx(WS_EX_LAYERED, szWindowClass, szTitle, 0,\r\n         CW_USEDEFAULT, 0, CW_USEDEFAULT, 0, NULL, NULL, hInstance, NULL);<\/pre>\n<p>After your window is created we will load a PNG file with an alpha channel and use UpdateLayeredWindow to render the PNG on the window using the alpha channel of the PNG file as the transparency level for the window. This is done as follows:<\/p>\n<pre>\/\/ Load our PNG image\r\nCImage img;\r\nimg.Load(\"circle.png\");\r\n\/\/ Get dimensions\r\nint iWidth = img.GetWidth();\r\nint iHeight = img.GetHeight();\r\n\/\/ Make mem DC + mem  bitmap\r\nHDC hdcScreen = GetDC(NULL);\r\nHDC hDC = CreateCompatibleDC(hdcScreen);\r\nHBITMAP hBmp = CreateCompatibleBitmap(hdcScreen, iWidth, iHeight);\r\nHBITMAP hBmpOld = (HBITMAP)SelectObject(hDC, hBmp);\r\n\/\/ Draw image to memory DC\r\nimg.Draw(hDC, 0, 0, iWidth, iHeight, 0, 0, iWidth, iHeight);\r\n\r\n\/\/ Call UpdateLayeredWindow\r\nBLENDFUNCTION blend = {0};\r\nblend.BlendOp = AC_SRC_OVER;\r\nblend.SourceConstantAlpha = 255;\r\nblend.AlphaFormat = AC_SRC_ALPHA;\r\nPOINT ptPos = {0, 0};\r\nSIZE sizeWnd = {iWidth, iHeight};\r\nPOINT ptSrc = {0, 0};\r\nUpdateLayeredWindow(hWnd, hdcScreen, &amp;ptPos, &amp;sizeWnd, hDC, &amp;ptSrc, 0, &amp;blend, ULW_ALPHA);\r\n\r\nSelectObject(hDC, hBmpOld);\r\nDeleteObject(hBmp);\r\nDeleteDC(hDC);\r\nReleaseDC(NULL, hdcScreen);<\/pre>\n<p>Because I&#8217;m using CImage, you need to include the atlimage.h header.<\/p>\n<p>That&#8217;s all that is required for the basics of UpdateLayeredWindow.<\/p>\n<p><strong>NOTE: <\/strong>The example above does not include any error checking. That is left for the reader as an excercise.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>In this post I will briefly explain how to use layered windows and specifically how to use UpdateLayeredWindow. The first thing you need to do is add the WS_EX_LAYERED style to your window. This can for example be done with a call to CreateWindowEx: hWnd = CreateWindowEx(WS_EX_LAYERED, szWindowClass, szTitle, 0, CW_USEDEFAULT, 0, CW_USEDEFAULT, 0, NULL, [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[6,17,8],"tags":[42,43],"class_list":["post-244","post","type-post","status-publish","format-standard","hentry","category-c","category-software-development","category-win32-api","tag-layered-windows","tag-updatelayeredwindow"],"_links":{"self":[{"href":"https:\/\/www.nuonsoft.com\/blog\/wp-json\/wp\/v2\/posts\/244","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.nuonsoft.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.nuonsoft.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.nuonsoft.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.nuonsoft.com\/blog\/wp-json\/wp\/v2\/comments?post=244"}],"version-history":[{"count":3,"href":"https:\/\/www.nuonsoft.com\/blog\/wp-json\/wp\/v2\/posts\/244\/revisions"}],"predecessor-version":[{"id":247,"href":"https:\/\/www.nuonsoft.com\/blog\/wp-json\/wp\/v2\/posts\/244\/revisions\/247"}],"wp:attachment":[{"href":"https:\/\/www.nuonsoft.com\/blog\/wp-json\/wp\/v2\/media?parent=244"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.nuonsoft.com\/blog\/wp-json\/wp\/v2\/categories?post=244"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.nuonsoft.com\/blog\/wp-json\/wp\/v2\/tags?post=244"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}