title: Drawing a partially transparent GDI+ Bitmap to a borderless window using UpdateLayeredWindow
body: <p>I'm trying to create a non-rectangular window with semi-transparent pixels. The image does not come from a PNG but is drawn on-the-fly using GDI+ calls.</p>
  
  <p>I create the window as follows:</p>
  
  <pre><code>WNDCLASSEX wc = WNDCLASSEX();
  wc.cbSize = sizeof(wc);
  HINSTANCE instance = GetModuleHandle(nullptr);
  std::wstring classname(L"gditest ui window class");
  
  if (!GetClassInfoEx(instance, classname.c_str(), &amp;wc)) {
      //wc.cbSize;
      //wc.style = CS_DROPSHADOW;
      wc.lpfnWndProc = process_messages;
      //wc.cbClsExtra;
      //wc.cbWndExtra;
      wc.hInstance = instance;
      wc.hIcon;
      wc.hCursor = LoadCursor(0, IDC_ARROW);
      //wc.hbrBackground;
      //wc.lpszMenuName;
      wc.lpszClassName = classname.c_str();
      wc.hIconSm;
  
      if (!RegisterClassEx(&amp;wc))
          throw GetLastError();
  }
  
  m_window = CreateWindowEx(WS_EX_APPWINDOW | WS_EX_LAYERED,
      classname.c_str(), L"User Interface",
      WS_VISIBLE,
      CW_USEDEFAULT, CW_USEDEFAULT, 640, 480,
      HWND_DESKTOP, 0, instance, this);
  
  if (!m_window)
      throw GetLastError();
  
  update_window();
  </code></pre>
  
  <p>The update_window() function looks like this:</p>
  
  <pre><code>void user_interface::update_window()
  {
      RECT r;
      GetClientRect(m_window, &amp;r);
  
      Bitmap buf(r.right - r.left, r.bottom - r.top, PixelFormat32bppARGB);
  
      Graphics gfx(&amp;buf);
      Rect rect(r.left, r.top, r.right - r.left, r.bottom - r.top);
      SolidBrush b(Color(0x7f00ff00));
      gfx.FillRectangle(&amp;b, rect);
  
  /*  CLSID clsid;
      UINT numbytes = 0, numenc = 0;
      GetImageEncodersSize(&amp;numenc, &amp;numbytes);
  
      std::vector&lt;char&gt; encoders(numbytes, 0);
      ImageCodecInfo *encoderptr = (ImageCodecInfo *)&amp;encoders[0];
      GetImageEncoders(numenc, numbytes, encoderptr);
  
      clsid = encoderptr[4].Clsid;
  
      buf.Save(L"test.png", &amp;clsid);
  */
      HDC gfxdc = gfx.GetHDC();
      HDC scrndc = GetDC(HWND_DESKTOP);
  
      BLENDFUNCTION blend;
      blend.BlendOp = AC_SRC_OVER;
      blend.BlendFlags = 0;
      blend.SourceConstantAlpha = 255;
      blend.AlphaFormat = AC_SRC_ALPHA;
  
      POINT src = POINT(), dst;
      SIZE size;
  
      GetWindowRect(m_window, &amp;r);
      dst.x = r.left;
      dst.y = r.top;
      size.cx = buf.GetWidth();
      size.cy = buf.GetHeight();
  
      if (!UpdateLayeredWindow(m_window, scrndc, &amp;dst, &amp;size, gfxdc, &amp;src, 0, &amp;blend, ULW_ALPHA)) {
          throw GetLastError();
      }
  
      ReleaseDC(HWND_DESKTOP, scrndc);
      gfx.ReleaseHDC(gfxdc);
  }
  </code></pre>
  
  <p>The commented piece of code saves the Bitmap object to a PNG, which I wrote just to confirm the bitmap is drawn properly.</p>
  
  <p>No errors occur, however the result on screen is not what I intended. Instead of a nice 50% transparent green square I get a barely visible white square: <img src="https://i.imgur.com/h11ve.png" alt="screenshot">.</p>
  
  <p>Another weird thing is that clicks on the window fall through to whatever is underneath, eventhough it is slightly visible...</p>
  
  <p>What am I doing wrong here?</p>