0

I want to make only certain(rectangular) parts of the window transparent. I have set the window as WS_EX_LAYERED and the WM_PAINT function is as follows:

    case WM_PAINT:;
        RECT rect;
        GetWindowRect(hwnd, &rect);
        HDC hdc = GetDC(hwnd);
        HDC hdc1 = CreateCompatibleDC(hdc);
        HBITMAP hBitmap = CreateCompatibleBitmap(hdc, rect.right - rect.left, rect.bottom - rect.top);
        SelectObject(hdc, hBitmap);
        SIZE size = {rect.right - rect.left, rect.bottom - rect.top};
        BLENDFUNCTION blendFunc = {AC_SRC_OVER, 0, 0, AC_SRC_ALPHA};
        UpdateLayeredWindow(hwnd, hdc1, NULL, &size, hdc, NULL, RGB(0, 0, 0), &blendFunc, ULW_ALPHA);
        DeleteObject(hBitmap);
        DeleteDC(hdc);
        ReleaseDC(hwnd, hdc1);
        break;

I tried creating a child window, but that doesn't seem to work if I change it's opacity through SetLayeredWindowAttributes, possibly because it mimics the parent windows opacity. I am currently trying to use UpdateLayeredWindow to make certain parts transparent, but I can't even make the entire part transparent using UpdateLayeredWindow. What is the general way to make certain parts of the window transparent?

work work
  • 51
  • 5
  • 4
    `WM_PAINT` messages are no longer generated when you use `UpdateLayeredWindow`. Either you provide a full 32bpp bitmap with alpha and call `UpdateLayeredWindow` whenever you want to change it, or you use color keying via `SetLayeredWindowAttributes` and paint the areas you want transparent in the color you selected. – Jonathan Potter Nov 16 '22 at 18:57

1 Answers1

1

I suggest you could refer to the Doc:Using Layered Windows

To have a dialog box come up as a translucent window, first create the dialog as usual. Then, on WM_INITDIALOG, set the layered bit of the window's extended style and call SetLayeredWindowAttributes with the desired alpha value.

The third parameter of SetLayeredWindowAttributes is a value that ranges from 0 to 255, with 0 making the window completely transparent and 255 making it completely opaque.

In order to use layered child windows, the application has to declare itself Windows 8-aware in the manifest. Refer to the thread: How to use WS_EX_LAYERED on child controls

Jeaninez - MSFT
  • 3,210
  • 1
  • 5
  • 20
  • Is a manifest file absolutely required for this? Also this works on windows other than Dialog boxes right? – work work Nov 18 '22 at 08:12