1

I know there are a lot of answers related with this question, but I read them all and still don't understand.

My target is to draw RGBA image on the top of all windows using WinAPI.

For this I make TOPMOST LAYERED TRANSPARENT window:

    hWin = CreateWindowEx(WS_EX_TOPMOST | WS_EX_LAYERED | WS_EX_TRANSPARENT,
                          "WindowClass1",
                          "Title",
                          WS_POPUP,
                          posX,
                          posY,
                          width,
                          height,
                          NULL,
                          NULL,
                          hInstance,
                          NULL);

To load image from file I use stb_image.h.

And to draw the image I tried different functions, right now it is:

    SetDIBitsToDevice(hdc,
                      0,
                      0,
                      width,
                      height,
                      0,
                      0,
                      0,
                      height,
                      pixels.data(),
                      &bmi,
                      DIB_RGB_COLORS);

Everything is draws, but without the alpha channel, I can only set the alpha for the whole window using:

    SetLayeredWindowAttributes(hWin, RGB(200, 200, 200), /*alpha=*/255, LWA_ALPHA);

And also I tried: UpdateLayeredWindow()

Is it possible at all?

Simon Mourier
  • 132,049
  • 21
  • 248
  • 298
Anaph
  • 21
  • 3
  • Why is this question tagged C++? Isn't the Windows API in C, and cannot everything relevant be done in C? – pts Aug 02 '23 at 22:23
  • 5
    `SetDIBitsToDevice` does not support alpha channels, use the [AlphaBlend](https://learn.microsoft.com/en-us/windows/win32/api/wingdi/nf-wingdi-alphablend) function instead. – 500 - Internal Server Error Aug 02 '23 at 22:42
  • 2
    @pts - winapi itself is C, but if the person asking is using C++ that affects what they're looking for in example code and help. Same as if they were using python or C# to call winapi – Dave S Aug 02 '23 at 23:53
  • Also see the [Alpha Blending a Bitmap](https://learn.microsoft.com/en-us/windows/win32/gdi/alpha-blending-a-bitmap) example. – YangXiaoPo-MSFT Aug 03 '23 at 03:02
  • I tried this function, but it seems like it blend bitmap with window background. And I want to blend it with other windows (for example with desktop or browser) – Anaph Aug 03 '23 at 13:12
  • And also as I understand now I can't draw just one time? Now I should redraw in cycle to blend with changing background? – Anaph Aug 03 '23 at 13:46
  • For per pixel alpha blending, you should use `UpdateLayeredWindow` but you've not showed how you're using it. How did you try `UpdateLayeredWindow`? – jtxkopt Aug 03 '23 at 14:38
  • @jtxkopt, UpdateLayeredWindow() will work the same as AlphaBlend()? I mean blend bitmap with background of window. I need to blend bitmap with another windows (like window with transparent in some places background) – Anaph Aug 03 '23 at 19:39
  • nvm, UpdateLayeredWindow() works fine. I'm just stupid. ty all – Anaph Aug 03 '23 at 20:09
  • Would taking a screnshot, drawing the image "into" the screenshot and displaying the result "full-screen" as (non-transparent) image an option? – Martin Rosenau Aug 04 '23 at 15:18
  • @Martin Rosenau, as for me it's bad idea in my case, because I want to interact with other windows (to be more precise: I make sight for any game) – Anaph Aug 04 '23 at 15:22

0 Answers0