title: Partially transparent window OpenGL/Win32
body: <p>I have read every existing question on this tricky (niche) subject, but I am stuck. I have a Win32 window with an OpenGL context. I want my window to be partially transparent.</p>
  
  <p>My result so far is that the entirety of the window is transparent. I want only the black area to be transparent, so that I can draw some 3D objects and they will look like they are coming of the window. </p>
  
  <p><a href="https://i.stack.imgur.com/KT4BG.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/KT4BG.png" alt="enter image description here"></a></p>
  
  <p>First, in my window class, I have set hbrBackground to black. </p>
  
  <pre><code>Windows::WindowClass windowClass;
  windowClass.cbSize = sizeof(Windows::WindowClass);
  windowClass.style = Windows::CS_VREDRAW | Windows::CS_HREDRAW;
  windowClass.lpfnWndProc = WndProc;
  windowClass.cbClsExtra = 0;
  windowClass.cbWndExtra = 0;
  windowClass.hInstance = moduleHandle;
  windowClass.hIcon = Windows::LoadIcon(0u, (X*)Windows::IDI_QUESTION);
  windowClass.hCursor = Windows::LoadCursor(0u, (X*)Windows::IDC_ARROW);
  windowClass.hbrBackground = Windows::CreateSolidBrush(0x00000000);
  windowClass.lpszMenuName = nullptr;
  windowClass.lpszClassName = (X*)name;
  windowClass.hIconSm = Windows::LoadIcon(0u, (X*)Windows::IDI_QUESTION);
  </code></pre>
  
  <p>I have created my window with the WS_EX_LAYERED flag. </p>
  
  <pre><code>windowHandle = Windows::CreateWindow(Windows::WS_EX_LAYERED, (X*)name, "", Windows::WS_POPUP, w / 4, h / 4, w / 2, h / 2, 0u, 0u, moduleHandle, 0u);
  </code></pre>
  
  <p>In my pixel format, I have enabled alpha and composition.</p>
  
  <pre><code>PixelFormatDescriptor format;
  format.nSize = sizeof(PixelFormatDescriptor);
  format.nVersion = 1;
  format.dwFlags = PFD_SUPPORT_OPENGL | PFD_DRAW_TO_WINDOW | PFD_DOUBLEBUFFER | PFD_SUPPORT_COMPOSITION;
  format.iPixelType = PFD_TYPE_RGBA;
  format.cColorBits = 32;
  format.cRedBits = 0;
  format.cRedShift = 0;
  format.cGreenBits = 0;
  format.cGreenShift = 0;
  format.cBlueBits = 0;
  format.cBlueShift = 0;
  format.cAlphaBits = 8;
  format.cAlphaShift = 0; 
  format.cAccumBits = 0; 
  format.cAccumRedBits = 0;
  format.cAccumGreenBits = 0;
  format.cAccumBlueBits = 0;
  format.cAccumAlphaBits = 0;
  format.cDepthBits = 24;
  format.cStencilBits = 8; 
  format.cAuxBuffers = 0; 
  format.iLayerType = PFD_MAIN_PLANE;
  format.bReserved = 0;
  format.dwLayerMask = 0;
  format.dwVisibleMask = 0; 
  format.dwDamageMask = 0;
  </code></pre>
  
  <p>I have tried the blur region "trick", but it has no effect. My result is not related to this piece of code.</p>
  
  <pre><code>struct DwmBlurBehind
  {
      U4 dwFlags;
      S4 fEnable;
      X* blurRegionHandle;
      S4 fTransitionOnMaximized;
  };
  
  DwmBlurBehind blurBehind;
  blurBehind.dwFlags = Windows::DWM_BB_ENABLE | Windows::DWM_BB_BLURREGION;
  blurBehind.fEnable = true;
  blurBehind.blurRegionHandle = Windows::CreateRectRgn(0, 0, -1, -1);
  blurBehind.fTransitionOnMaximized = false;
  Windows::DwmEnableBlurBehindWindow(windowHandle, &amp;blurBehind);
  </code></pre>
  
  <p>Finally, I have set the LWA_COLORKEY and LWA_ALPHA attributes. This is what gave me the effect displayed. However, the color key does not seem to be taken into account (I have tried non-zero values as well). </p>
  
  <pre><code>Windows::SetLayeredWindowAttributes(windowHandle, 0, 170, Windows::LWA_COLORKEY | Windows::LWA_ALPHA);
  </code></pre>
  
  <p>I did not forget to enable blending. </p>
  
  <pre><code>GL::Enable(GL::BLEND);
  GL::BlendFunc(GL::SRC_ALPHA, GL::ONE_MINUS_SRC_ALPHA);
  </code></pre>