2

I recently ported my app from using an ID2D1HwndRenderTarget to using an ID2D1DeviceContext with a dxgi swap chain. With an hwnd render target I can have a transparent background by calling pRT->Clear({});(not a transparent window, only the render target itself is transparent, so I can see whatever content underneath it). That's not the case with a dxgi swap chain, the background is always black, even if no drawing is done on that area. How can I change this behavior and have a see through render target?

mbolp
  • 21
  • 1
  • 4
  • 2
    Please provide enough code so others can better understand or reproduce the problem. – Community Jun 27 '22 at 13:39
  • 1
    Clear is not specific to ID2D1HwndRenderTarget since it's defined by ID2D1DeviceContext . Otherwise, if you have an IDXGISwapChain COM reference, you can QueryInterface from IDXGISwapChain1 and call IDXGISwapChain1::SetBackgroundColor https://learn.microsoft.com/en-us/windows/win32/api/dxgi1_2/nf-dxgi1_2-idxgiswapchain1-setbackgroundcolor but all this heavily depends on your exact code. – Simon Mourier Jun 27 '22 at 16:24
  • @SimonMourier I'm aware Clear is not specific to ID2D1HwndRenderTarget as ID2D1DeviceContext also inherits from ID2D1RenderTarget, but calling it in this case does not in fact clear the window. I don't think this relates to direct2d, maybe it has to do with how the back buffer is dealt with (I used CreateBitmapFromDxgiSurface to create an ID2D1Bitmap1 and SetTarget it on the device context). I'm using CreateSwapChainForHwnd so I dont't SetBackgroundColor with 0 alpha works for me because it requires an alpha blended swap chain which can not be created CreateSwapChainForHwnd – mbolp Jun 27 '22 at 17:53
  • 1
    Ok, you don't seem to want to "change the background color of the DXGI swap chain", it sounds like you want a transparent window. This is different, how you create and setup all window, devices, contexts, etc. is important. Difficult to say more w/o a sample reproducing code. – Simon Mourier Jun 27 '22 at 18:04
  • @SimonMourier To be more specific, my window is created with a custom non client area, and the client area basically extends to the entire window. This means the minimize/maximize/close buttons overlap the render target. This wouldn't be a problem if the render target itself is transparent (the window is not transparent, it's filled by the system accent color), but when using a dxgi swap chain the buttons are obfuscated. The window is created following this guide https://learn.microsoft.com/en-us/windows/win32/dwm/customframe and the device is set up following this article – mbolp Jun 27 '22 at 18:14
  • @SimonMourier Link to kenny kerr's article on using direct2d with dxgi swap chain https://learn.microsoft.com/en-us/archive/msdn-magazine/2013/may/windows-with-c-introducing-direct2d-1-1 – mbolp Jun 27 '22 at 18:29
  • Yes, the difference is Kenny Kerr code works, my code works too. If you're here, I guess it's because your code doesn't. – Simon Mourier Jun 27 '22 at 19:00
  • @SimonMourier Well yes because the window in the article is a standard win32 window so it doesn't need to concern about painting over the non client area, if the window procedure is changed to handle dwm related stuff in the msdn guide then the code stops working - do you have a suggested solution to this problem? – mbolp Jun 27 '22 at 19:14

0 Answers0